here's the code:
Option Explicit
Dim backup_dir, num_days, user, password, arguments, backup_file, backup_dir_daily, backup_dir_weekly, backup_dir_monthly
Dim oShell, oFS, nResults
'SET HERE BACKUP PATH, DAYS OF MYSQL BACKUP, USER AND PWD
backup_dir = "C:\Backup\MySQLBK"
num_days = 7
user = "root"
password = "root"
'#####################################################
Set oFS = CreateObject("Scripting.FileSystemObject")
backup_dir_daily = backup_dir & "Daily"
backup_dir_weekly = backup_dir & "Weekly"
backup_dir_monthly = backup_dir & "Monthly"
If oFS.FolderExists(backup_dir_daily) = False Then
oFS.CreateFolder(backup_dir_daily)
End If
If oFS.FolderExists(backup_dir_weekly) = False Then
oFS.CreateFolder(backup_dir_weekly)
End If
If oFS.FolderExists(backup_dir_monthly) = False Then
oFS.CreateFolder(backup_dir_monthly)
End If
backup_file = backup_dir_daily & Year(Date) & PadZero(Month(Date)) & PadZero(Day(Date)) & "_all_databases.bak"
arguments = "--user=" & user & " --password=" & password & " --all-databases --result-file=" & backup_file
Set oShell = CreateObject("WScript.Shell")
nResults = oShell.Run("mysqldump.exe " & arguments, 1, TRUE)
Set oShell = Nothing
Dim folder, files, file, regex, Matches, Match, counter, backups, i, cur
Set regex = New regexp
regex.Pattern = "[0-9]{8}_all_databases.bak"
regex.Global = True
regex.IgnoreCase = True
Set folder = oFS.GetFolder(backup_dir_daily)
Set files = folder.Files
counter = 0
ReDim backups(0)
For Each file In files
Set Matches = regex.Execute(file.Name)
For Each Match in Matches
Redim Preserve backups(counter)
backups(counter) = Match.Value
counter = counter + 1
Next
Next
cur = 0
For i = counter - 1 To 0 Step -1
If (cur >= num_days) Then
oFS.DeleteFile(backup_dir_daily & backups(i))
Else
End If
cur = cur + 1
Next
'################WEEKLY BACKUP#####################
if WeekDay(Now()) = 6 then
oFS.CopyFile backup_file, backup_dir_weekly
else: end if
'######################################################
'################MONTHLY BACKUP#########################
if Day(Now()) = 1 then
oFS.CopyFile backup_file, backup_dir_monthly
else: end if
'######################################################
Set oFS = Nothing
' PadZero - add "0" to values < 10
Function PadZero(val)
If (val < 10) Then
PadZero = "0" & val
Else
PadZero = val
End If
End Function
No comments:
Post a Comment