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

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

[cc lang=”vb”]
‘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
[/cc]

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *