Automate MySQL Backups on Windows Server 2012

Hosting MySQL and WordPress on your Windows Server 20008 R2 or Windows Server 2012 R2  has never been easier thanks to Microsoft Web Platform Installer (WPI).  However, backing up the MySQL databases is another story. Running mysqldump is certainly easy enough but manually taking a backup once in a while won’t be useful for disaster reovery so something automated is needed.  I was hoping to find an equivalent tool to the very capable SQL Scheduler which automates backups for SQL Server Express.  Fortunately while searching an easy solution for automated backups of MySQL I stumbled across a blog post by Mathew Moeller at who created the script I’m going to cover here.\r\n\r\nThe backup solution runs from a .bat file which you then schedule using Windows Task Scheduler. Each MySQL database is backed up to an individual SQL file using a file name of the database and the date and time of the backup.  A zip file is then created containing all of the individual SQL backup files.  The script even includes a feature to delete historical backups after a specified period of time.  Errors that ocurr during the backup process are logged in a dumperrors.txt file.\r\n

Setting up the Batch File

\r\nThe first step to automate your MySQL backups is to download the script to your server. Edit the batch file using Notepad. In the file you’ll see a section called SETTINGS AND PATHS.  This will contain the username and password of the user backing up the databases and as well as the specific paths the script needs to run:\r\n

  • Error log path
  • MySQL EXE Path
  • Path to data folder
  • Path to zip executable
  • Number of days to retain .zip files

\r\nThe download package also includes a copy of the 7zip standalone console which is easy to use.\r\n

Setting the Backup User

image

\r\nThe script can just as easily run with the MySQL root user and password however following security best practices you should use a different user with the least permissions necessary to run the backups. The previous hyperlink has the example below to set those permissions for a user called mysqlbackup.\r\n\r\n\r\n\r\n \r\n

Setting the Paths

image

\r\nOnce you complete filling in the SETTINGS and PATHS section your script should look something similar to this:\r\n\r\n\r\n\r\nBe sure to double check all the paths are correct. You don’t want come back sometime in the future in need a backup during an emergency only to discover your script had a typo and your data was never archived.\r\n\r\n \r\n

Setting the Scheduled Task

image
image

\r\nOnce the script configuation is complete the only remaining step to automating your MySQL backups is to create the scheduled task in Task Scheduler. Simply step through the Basic Task Wizard and browse to the path of batch file you setup earlier.\r\n\r\n\r\n\r\n \r\n\r\nOn the job properies be sure to set the user account to run as a user with the necessary permissions and set Run whether the user is logged on or not.\r\n\r\n\r\n\r\nRun the job manually from the console to ensure everything runs properly. Double check the output path you specified earlier and ensure the MySQL databases were created.\r\n\r\n \r\n

In Summary

\r\nHaving an automated solution to backup databases is critical for disaster recovery. Today I covered a free script that you can leverage to automate backing up your MySQL databases on Windows Server 2012 R2. With any backup solution be sure to always test your procedures as well as periodically testing restoring the backups. Thanks for reading!

Leave a Reply