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

0
(0)

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

'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:备份文件路径

设置好执行时间就可以了。[:en]

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

[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]

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

[:]

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

发表回复

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