MySQL Backup VBScript

Published 9/20/2007 by Andy in MySQL | VBScript | Windows

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


The following VBScript will retrieve the current hotfix for a specified SQL 2005 Server, and return that hotfix as a the return code. Useful in batch files for selectively applying unattended service packs.

Option Explicit
Dim sServer, sInstance, nVersion, bVerbose, sSQLServer

If (ParseCommandLine()) Then
	nVersion = GetSQLServerHotfix(sServer, sInstance)
	WScript.Quit(nVersion)
Else
	ShowUsage()
	WScript.Quit(-1)
End If


'*******************************************************************
'* GetSQLServerHotfix - Retrieves SQL 2005 server hotfix version #
'*******************************************************************
Function GetSQLServerHotfix(strServer, strInstance)
Dim oWMI, oRst, oRow, nHotfix, sSQLName, sVersion, sTemp, nPos

	nHotfix = 0

	If (UCase(sInstance) <> "MSSQLSERVER") Then
		sSQLName = "MSSQL$" & sInstance
	Else
		sSQLName = "MSSQLSERVER"
	End If

	Set oWMI = GetObject("winmgmts:\\" & strServer & "\root\Microsoft\SqlServer\ComputerManagement")
	Set oRst = oWMI.ExecQuery("SELECT PropertyStrValue FROM SQLServiceAdvancedProperty " _
					& " WHERE PropertyName='Version'" _
					& " AND ServiceName='" & sSQLName & "'",,48)

	For Each oRow in oRst
		sVersion = oRow.PropertyStrValue
		If (bVerbose) Then
			WScript.Echo("Server: " & sSQLServer)
			WScript.Echo("Version: " & sVersion)
		End If
		If (Len(sVersion) > 2) Then
			sTemp = sVersion
			nPos = InStr(3,sTemp,".")
			If (nPos > 0) Then
				sTemp = Mid(sTemp,nPos + 1)
				nPos = InStr(1,sTemp,".")
				If (nPos > 0) Then
					nHotfix = Left(sTemp,nPos-1)
					If (bVerbose) Then
						WScript.Echo("Hotfix: " & nHotfix)
					End If
				End If
			End If
		End If
	Next

	Set oRst = Nothing
	Set oWMI = Nothing

	GetSQLServerHotfix = nHotfix

End Function



'*******************************************************************
'* ParseCommandLine - Parses command line options into global vars
'*******************************************************************
Function ParseCommandLine()
Dim objArgs, i, sNextArg

	
	Set objArgs = WScript.Arguments
	For i = 0 To objArgs.Count - 1
		If (i < objArgs.Count - 1) Then
			sNextArg = objArgs(i+1)
		Else
			sNextArg = ""
		End If
		Select Case (UCase(objArgs(i)))
			Case "/SERVER":
				sSQLServer = sNextArg
				i = i + 1
			Case "/VERBOSE":
				bVerbose = True
			Case "/HELP":
				ParseCommandLine = False
				Exit Function
			Case "/H":
				ParseCommandLine = False
				Exit Function
		End Select
	Next

	' Check for valid arguments
	If (Len(sSQLServer) > 0) Then
		' Parse the server name/instance
		Dim nSlash
		nSlash = InStr(1,sSQLServer,"\")
		If (nSlash > 0) Then
			sInstance = Mid(sSQLServer,nSlash+1)
			sServer = Left(sSQLServer,nSlash-1)
		Else
			sInstance = "MSSQLSERVER"
			sServer = sSQLServer
		End If
		ParseCommandLine = True
	Else
		ParseCommandLine = False
	End If

End Function