Windows下自动备份Mysql数据库脚本

暂时未添加自动删除历史备份文件的功能。欢迎交流意见。

'MySQL Database Backup script under MS Windows

Dim str_DBNames,mysqlpath,backuppath
str_DBNames=WScript.Arguments.Named.Item("db")
mysqlpath=WScript.Arguments.Named.Item("mysql_bin")
backuppath=WScript.Arguments.Named.Item("backupdir")

Dim db
db = Split(str_DBNames, ",", -1, 1)

IF UBOUND(db)>0 THEN
Dim fName,tNow
tNow = Now()

'Backup file name XXXX_YYYYMMDDHHMMSS.sql
fName = Year(tNow) & FormatNumber(Month(tNow)/100,2,false) & FormatNumber(Day(tNow)/100,2,false) & FormatDateTime(tNow,3)
fName = Replace(Replace(fName,".",""),":","")

Dim WshShell,i,cmd
Set WshShell = CreateObject("Wscript.Shell")
For i=0 To Ubound(db)
cmd="%ComSpec% /q /c " & mysqlpath & "mysqldump.exe --opt -u root " & db(i) & " >" & backuppath & "" & db(i) & "_" & fName & ".sql"
WshShell.Run cmd,0,true
Next
Set WshShell = Nothing
END IF

 

使用方法:保存以上代码为mysqlbak.vbs,新建计划任务,命令行为

cscript.exe mysqlbak.vbs /db:逗号分割的数据库名列表 /mysql_bin:MySql的bin目录路径 /backupdir:备份文件路径

设置好执行时间就可以了。

发表评论

电子邮件地址不会被公开。 必填项已用*标注