备份完了数据库要备份文件,下面这个脚本就是将文件备份到远程目录。
前提远程目录开启共享,并备份主机可以访问。

PS: 自动删除功能还没加,抽空加一个,同时使用使用mailsend-go 发送 SMTP 邮件进行消息通知,由于消息可以能会乱码,还需要 iconv 进行 GB2312 到 UTF8 转换。如果需要拷贝备份到其他主机,通过SMB挂载网络驱动器形式进行拷贝。

@REM backupfile.bat
@ECHO OFF&PUSHD %~DP0
setlocal EnableDelayedExpansion&color 3e & cd /d "%~dp0"
TITLE  File Backup Script
set FILEPATH1="D:\path1"
set FILEPATH2="D:\path2"
set REMOTEPATH1="\\172.16.172.222\backup\files"
set REMOTEPATH2="\\172.16.172.231\backup\files"
set REMOTEPATH3="\\172.16.172.223\backup\files"
@REM 备份日志设置
@REM 2021.4.15 修正日期无法正常使用的问题,参考https://www.cnblogs.com/daysme/p/6571926.html
for /f "tokens=2 delims==" %%a in ('wmic path win32_operatingsystem get LocalDateTime /value') do (set t=%%a)
set NEWTIME=%t:~0,4%-%t:~4,2%-%t:~6,2%-%t:~8,2%-%t:~10,2%-%t:~12,2%
set LOGNAME="D:\backup\logs\%NEWTIME%-file-backup-log.txt"

@REM 自动删除文件过期备份文件和日志设置,设置为0则不删除
set DAYS=30
@REM 发送邮件提醒消息设置
set title="%date%-%time% Files Backup Log"
set smtpserver="smtp.163.com"
set smtpport=25
set user="test@163.com"
set token="xxxx"
set sendto="test@163.com"
@REM 请勿更改下面的代码 
del sendlog.txt /f /q /a
del send.old /f /q /a
del send.txt /f /q /a

if exist  %FILEPATH1% (
    echo. %date% - %time% Now start to backup file 1.  >>  %LOGNAME%
    XCOPY %FILEPATH1% %REMOTEPATH1%\ /S /E /Y  >>  %LOGNAME%
    XCOPY %FILEPATH1% %REMOTEPATH2%\ /S /E /Y  >>  %LOGNAME%
    XCOPY %FILEPATH1% %REMOTEPATH3%\ /S /E /Y  >>  %LOGNAME%
    echo. %date% - %time% Backup file 1 end.>>  %LOGNAME%
) else (
    echo. %date% - %time% Can not Found Backup File Path1,Backup failed.    >>  %LOGNAME%
)

if exist  %FILEPATH2% (
    echo. %date% - %time% Now start to backup file 2.  >>  %LOGNAME%
    XCOPY %FILEPATH2% %REMOTEPATH1%\ /S /E /Y  >>  %LOGNAME%
    XCOPY %FILEPATH2% %REMOTEPATH2%\ /S /E /Y  >>  %LOGNAME%
    XCOPY %FILEPATH2% %REMOTEPATH3%\ /S /E /Y  >>  %LOGNAME%
    echo. %date% - %time% Backup file 2 end.>>  %LOGNAME%
) else (
    echo. %date% - %time% Can not Found Backup File Path2,Backup failed.    >>  %LOGNAME%
)

@REM 文件编码转换
cd /d "%~dp0"
echo. %date% - %time% Convert file encoding.
echo. %date% - %time% Convert file encoding. >> %LOGNAME%
copy %LOGNAME% sendlog.txt /Y 
echo ----------------------------------- >> sendlog.txt
rem echo "备份SQL执行结果:" >> sendlog.txt
type sendlog.txt > send.txt
ren send.txt send.old
iconv -f GB2312 -t UTF-8 < send.old > send.txt

@REM 发送日志
cd /d "%~dp0"
echo. %date% - %time% Send log file to mail.
echo. %date% - %time% Send log file to mail. >> %LOGNAME%
mailsend-go -sub %title% -smtp %smtpserver% -port %smtpport% auth  -user  %user% -pass %token% -to %sendto% -from %user% -subject %title% -cs "utf8" body -file send.txt

@REM 删除产生文件
echo. %date% - %time% Delete all send temp files.
echo. %date% - %time% Delete all send temp files. >> %LOGNAME%
del sendlog.txt /f /q /a
del send.old /f /q /a
del send.txt /f /q /a
echo. %date% - %time% Thanks for your use. Press any key to exit.>>  %LOGNAME%
rem pause > nul
exit

创建定时任务:

@ECHO OFF&PUSHD %~DP0
setlocal EnableDelayedExpansion&color 3e & cd /d "%~dp0"
echo ----------------------------------- 
echo 创建自动定时任务
schtasks  /create  /tn  backupfile /tr  D:\backup\backupfile.bat  /sc  DAILY /st  02:00:00
echo ----------------------------------- 
echo 查看自动定时任务
schtasks  /Query  /tn backupfile 
echo ----------------------------------- 
rem 删除自动定时任务
rem schtasks /Delete /tn backupfile 
pause > nul
exit