本着不将鸡蛋放在一个篮子里的思想,我在远程做了数据的备份,在本地又做了一个备份

@echo off
set hour=%time:~,2%
if "%time:~,1%"==" " set hour=0%time:~1,1%

set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%%hour%%time:~3,2%%time:~6,2%"
D:\java_workspace\mysql-5.7.28\bin\mysqldump --opt -u username --password=xxx -h远程IP database > D:\java_workspace\db.bak\backup_database_%Ymd%.sql
"D:\software\7-Zip\7z.exe" a "D:\java_workspace\db.bak\backup_database_%Ymd%.sql.zip" "D:\java_workspace\db.bak\backup_database_%Ymd%.sql"
del /F "D:\java_workspace\db.bak\backup_database_%Ymd%.sql"
forfiles /p "D:\java_workspace\db.bak"  /s /m *.zip /d -30 /c "cmd /c del @path && echo %Ymd% delete @file success!" >> del_bak.log
@echo on

下面的一行具体的脚本意思是备份远程数据库到D盘的db.bak文件夹下,以backup_database_20200112030000.sql的格式输出

D:\java_workspace\mysql-5.7.28\bin\mysqldump --opt -u username --password=xxx -h远程IP database > D:\java_workspace\db.bak\backup_database_%Ymd%.sql

然后是压缩.sql文件

"D:\software\7-Zip\7z.exe" a "D:\java_workspace\db.bak\backup_database_%Ymd%.sql.zip" "D:\java_workspace\db.bak\backup_database_%Ymd%.sql"

然后再将.sql文件删除掉

del /F "D:\java_workspace\db.bak\backup_database_%Ymd%.sql"

最后将30天前的文件删除

forfiles /p "D:\java_workspace\db.bak"  /s /m *.zip /d -30 /c "cmd /c del @path && echo %Ymd% delete @file success!" >> del_bak.log

OK,脚本已经写好了,之后使用Windows的定时计划任务,添加一个备份计划即可。