vbs实现MySQL备份与还原(mysqldump命令)
Class MysqlSync
Private StrSqlFileName
Private StrUserName,StrPassword,StrPort,StrHostIP,StrHostIP1
Public Function Init(ObjConfig)
StrSqlFileName = ObjConfig.StrSqlFileName
StrUserName = ObjConfig.StrUserName
StrPassword = ObjConfig.StrPassword
StrPort = ObjConfig.StrPort
StrHostIP = ObjConfig.StrHostIP1
StrHostIP1 = ObjConfig.StrHostIP2
Dim ObjFileSys
Set ObjFileSys=CreateObject("Scripting.FileSystemObject")
IF Not ObjFileSys.FolderExists(StrSqlFileName) Then
ObjFileSys.CreateFolder(StrSqlFileName)
End IF
Set ObjFileSys = nothing
End Function
Public Function DelBackup(StrBackupName)
Dim ObjFileSys
Set ObjFileSys=CreateObject("Scripting.FileSystemObject")
Dim StrDelFile
StrDelFile = StrSqlFileName&"\"&StrBackupName&".sql"
IF ObjFileSys.FileExists(StrDelFile) Then
msgbox ObjFileSys.DeleteFile(StrDelFile,True)
DelBackup = True
Else
DelBackup = True
End IF
End Function
Public Function AddBackup(StrBackupName,StsErrorMsg)
Dim StrFileLoc
StrFileLoc = StrSqlFileName&"\"&StrBackupName&".sql"
Dim ObjFileSys
Set ObjFileSys=CreateObject("Scripting.FileSystemObject")
IF ObjFileSys.FileExists(StrFileLoc) Then
StsErrorMsg = "文件已存在"
AddBackup = False
Exit Function
End IF
Set ObjFileSys = Nothing
On Error Resume Next
Dim IntRes
IntRes = 1
IntRes = CreateObject("WScript.Shell").Run ("cmd.exe /c mysqldump -u"&StrUserName&" -p"&StrPassword&" --host="&StrHostIP&" -P"&StrPort&" -A --debug-info >"&StrFileLoc,0,True)
IF IntRes <> 0 Then
IntRes = CreateObject("WScript.Shell").Run ("cmd.exe /c mysqldump -u"&StrUserName&" -p"&StrPassword&" --host="&StrHostIP1&" -P"&StrPort&" -A --debug-info >"&StrFileLoc,0,True)
IF IntRes <> 0 Then
AddBackup = False
Exit Function
End IF
End IF
AddBackup = True
End Function
Public Function RestoreBackup(StrBackupName,StsErrorMsg)
Dim StrFileLoc
StrFileLoc = StrSqlFileName&"\"&StrBackupName&".sql"
Dim ObjFileSys
Set ObjFileSys=CreateObject("Scripting.FileSystemObject")
IF not ObjFileSys.FileExists(StrFileLoc) Then
StsErrorMsg = "文件不存在"
RestoreBackup = False
Exit Function
End IF
Set ObjFileSys = Nothing
On Error Resume Next
Dim IntRes
IntRes = 1
IntRes = CreateObject("WScript.Shell").Run ("cmd.exe /c mysql -u"&StrUserName&" -p"&StrPassword&" --host="&StrHostIP&" -P"&StrPort&" foms<"&StrFileLoc,0,True)
IF IntRes <> 0 Then
IntRes = CreateObject("WScript.Shell").Run ("cmd.exe /c mysql -u"&StrUserName&" -p"&StrPassword&" --host="&StrHostIP&" -P"&StrPort&" foms<"&StrFileLoc,0,True)
IF IntRes <> 0 Then
RestoreBackup = False
Exit Function
End IF
End IF
RestoreBackup = True
End Function
End Class