MySQL Backup VBScript
Submitted by Andy on Thu, 09/20/2007 - 10:30.
The following VBScript will backup all MySQL databases on the localhost to a directory and maintain 10 days worth of backups
'* Copyright (C) 2007 Andrew Loree
'****************************************************************************
'* mysqlbackup.vbs - Backup all mysql databases to a daily backup file
'* doing a rotation, limiting maximum number of backups retained
'****************************************************************************
'* This program is free software; you can redistribute it and/or
'* modify it under the terms of the GNU General Public License
'* as published by the Free Software Foundation; either version 2
'* of the License, or (at your option) any later version.
'*
'* This program is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'* GNU General Public License for more details.
'*
'* You should have received a copy of the GNU General Public License
'* along with this program; if not, write to the Free Software
'* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
'****************************************************************************
'* Version History:
'* 1.0 - Initial Release
'****************************************************************************
Option Explicit
Dim backup_dir, num_days, user, password, arguments, backup_file
Dim oShell, oFS, oDrive, nResults
backup_dir = "C:\some\path\here\"
num_days = 10
user = "XXXXXX"
password = "YYYYYYY"
backup_file = backup_dir & Year(Date) & PadZero(Month(Date)) & PadZero(Day(Date)) & "_all_databases.bak"
arguments = "--user=" & user & " --password=" & password & " --all-databases --quick --result-file=" & backup_file
Set oShell = CreateObject("WScript.Shell")
WScript.Echo("Creating backup file " & backup_file)
nResults = oShell.Run("mysqldump.exe " & arguments, 1, TRUE)
Set oShell = Nothing
Dim folder, files, file, regex, Matches, Match, counter, backups, i, cur
Set oFS = CreateObject("Scripting.FileSystemObject")
Set regex = New regexp
regex.Pattern = "[0-9]{8}_all_databases\.bak"
regex.Global = True
regex.IgnoreCase = True
Set folder = oFS.GetFolder(backup_dir)
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
WScript.Echo("Deleting " & backups(i))
oFS.DeleteFile(backup_dir & backups(i))
Else
WScript.Echo("Keeping " & backups(i))
End If
cur = cur + 1
Next
Set oFS = Nothing
' PadZero - adds preceding zero to values less than 10
Function PadZero(val)
If (val < 10) Then
PadZero = "0" & val
Else
PadZero = val
End If
End Function
| Attachment | Size |
|---|---|
| mysqlbackup.vbs | 2.86 KB |
