Backup MySQL using VBS scripts
Monday, January 12th, 2009 | Author: Martin

I have seen so many people searching ASP or ASP .NET scripts to backup their MySQL databases. Not only this but my Blog has been getting to many hits for the search term “backup mysql asp”. After seeing the need of so many people for the script I decided to write a VBS script to backup all or single MySQL database on Windows server. I am not very good at ASP so I didn’t write the script in ASP or ASP .NET but one can convert this script in either ASP or ASP .NET, it should not be a huge problem I suppose.

backup mysql asp Backup MySQL using VBS scripts

backup mysql asp 1 Backup MySQL using VBS scripts

Please make sure of the following before you configure the script:

  • The user that is running the script must have execute permissions on cscript in system32
  • The user that is running the script must have write permissions folder that is going to store the backup file.
  • The user that is running the script must have execute permissions on mysqldump.exe in Mysqlbin
  • If you running this script in browser then your web user will need the above permissions.

So here is the script:

backup-mysql-database.vbs

Option Explicit
Dim backup_dir, num_days, user, password, database, arguments, backup_file
Dim oShell, oFS, oDrive, nResults, sqldump

backup_dir = "C:MysqlBackups"
user = "xxxxxxxx"
password = "yyyyyyyy"
database = "zzzzzzzz"
sqldump = "C:Mysqlbinmysqldump.exe"

backup_file = backup_dir & database & ".sql"
arguments = "--user=" & user & " --password=" & password & " " & database & " > " & backup_file
arguments = sqldump & " " & arguments
WScript.Echo(arguments1)

Set oShell = CreateObject("WScript.Shell")

WScript.Echo("Creating backup file " & backup_file)
nResults = oShell.Run ("cmd /C" & arguments, 1, TRUE)
WScript.Echo(nResults)

Set oShell = Nothing

The following variable will be needed to be defined as per your need:

backup_dir = Should have the path to the directory that you want to save you backup file in.
user = Database user name that has full privileges over the database you want to backup.
password = Password of the database user.
database = Name of the database that you want to backup
sqldump = Path to mysqldump.exe, normally this is stored in MySQLbin directory.

To backup all database you can ignore the database variable and replace the first argument command with:

arguments = "--user=" & user & " --password=" & password & " --all-databases --quick
--result-file=" & backup_file

Now you can run the command below to execute the script on Windows server:

cscript c:myscriptsbackup-mysql-database.vbs

You can also schedule the above command to run on sceduled timining to backup the MySQL databases.