Request a quote 801.545.0410
Web SEO Branding Marketing Mobile App Email Marketing Social Paid Search Video
Matt Moeller

How to automatically backup all MySQL databases zip them and delete backups older than n days on windows with a batch file

How to automate MySQL backups with a .bat file on a Windows  server. Typically we have used the backup facility built into MySQL administrator, which unfortunately has now reached end of life.  While it worked well it had some major shortcomings. For example once you created a scheduled backup it would only include the databases you told it to at the time of creation.  Well, if you are like us and you constantly add new databases  the new ones simply wouldn’t be backed up, Annoying. What replaced MySQL administrator?  Enter the new MySQL Workbench.  Great tool save one missing feature, they decided to no longer support scheduled MySQL backups.

Obviously there are lots of scripts out there and easier solutions for linux based systems. The only windows solutions seemed to use php which seemed overly complex, relied on unnecessary systems that could fail, and introduced security concerns when you could use a simple batch file with a few lines of code to accomplish the same task.  I had little luck Googling solutions on how to backup all the databases individually, zip them into one file, and delete the older archives so I sat down and wrote my own batch file.  It was so useful to us that I feel compelled to share it with others. It should work on any version of windows x86 or x64 and any version of MySQL, but it was only tested on 2008R2.

Platforms Tested: Windows 2008 R2 x64 En with MySQL 5.5.x   AND Windows 2012 Server with MySQL 5.6.x

Updated 3.7.2015

[highlight] Features: [/highlight]

[list style=”tick”]

  • Backup all MySQl databases, including all newly created ones automatically
  • Create an individual .sql file for each database  (God send when restoring)
  • ZIP all the .sql files into one zip file and date/timestamp the file name to save space
  • Automatically delete MySQL backups older than n days (set to however many days you like)
  • FTP your backup zip to a remote location
  • Highly suggest you also setup a scheduled task to backup your MySQL directory and your new backup folder to an off site location


[highlight] Setup Instructions: [/highlight]

  1. Right click and edit  mysqlbackup.bat file in notepad
  2. Set the backupdate format, whatever your preference,  mine is yyyy-mm-dd-m-s
  3. Set the root u/p
  4. Set the MySQL data directory to match your install
  5. Set the path to mysqldump.exe to match your install path
  6. Set the destination of the backups should go, make sure there are write permissions obviously
  7. Set the path to your zip application with it’s flags/commands to zip an item, I am using the command line version of 7zip which is free.
  8. Update the path where your backups will be saved and then deleted once zipped
  9. Set the number of days to keep backups, using the win program  “Forfiles” for this,   mine is set to 30 days  “-30”
  10. Test your batch file on a dummy directory.  You’ll see the backup directory fill up with .sql files, then a timestamped zip file is made, and the directory is cleared.  Put some files older than 30 days in there and they will be wiped at the end.
  11. Finally create a scheduled task in windows to run the batch file on a schedule, remember to choose “Run whether user is logged on or not” otherwise it will fail.
  12. IMPORTANT NOTE:  updated 3.14.2013  if you get an error in the command prompt stating “mysqldump: unkown option ‘–no-beep’ this is due to your my.ini file having an invalid option under [client].  Open your my.ini file find the [client] section and comment  out #no-beep with a hash, re-run the bat file and it will work. This error has nothing to do with this script, you’d get the same error if you ran mysqldump.exe directly. I believe that MySQL Admin adds that line to the ini file when installed, thanks Oracle.
  13. 80% of the “Didn’t work for me” comments tend to be resolved simply by double checking that your directory paths are correct.

To save you some time I zipped up all the necessary files including the 7zip.exe app with the folder structure to match the batch file.  Simply unzip and update your specific paths in the .bat file and you’re done.

[download] DOWNLOAD  MySQL-BACKUP-WIN-v1.5[/download]

Here is a quick look at the .bat file:


:: Auto MySQL Backup For Windows Servers By Matt Moeller  v.1.5

:: Follow us on twitter for updates to this script
:: coming soon:  email admin a synopsis of the backup with total file size(s) and time it took to execute

:: FILE HISTORY ———————————————-

:: UPDATE 11.7.2012  Added setup all folder paths into variables at the top of the script to ease deployment added debug notes in various areas
:: UPDATE 7.16.2012  Added –routines, fix for dashes in filename, and fix for regional time settings
:: UPDATE 3.30.2012  Added error logging to help troubleshoot databases backup errors.   –log-error=”c:MySQLBackupsbackupfilesdumperrors.txt”
:: UPDATE 12.29.2011 Added time bug fix and remote FTP options – Thanks to Kamil Tomas
:: UPDATE 5.09.2011  v 1.0

:: If the time is less than two digits insert a zero so there is no space to break the filename

:: If you have any regional date/time issues call this include: getdate.cmd  credit: Simon Sheppard for this cmd – untested
:: call getdate.cmd

set year=%DATE:~10,4%
set day=%DATE:~7,2%
set mnt=%DATE:~4,2%
set hr=%TIME:~0,2%
set min=%TIME:~3,2%

IF %day% LSS 10 SET day=0%day:~1,1%
IF %mnt% LSS 10 SET mnt=0%mnt:~1,1%
IF %hr% LSS 10 SET hr=0%hr:~1,1%
IF %min% LSS 10 SET min=0%min:~1,1%

set backuptime=%year%-%day%-%mnt%-%hr%-%min%
echo %backuptime%

:: Note: Do not put spaces before the equal signs or variables will fail

:: Name of the database user with rights to all tables
set dbuser=root

:: Password for the database user
set dbpass=youradminpassword

:: Error log path – Important in debugging your issues
set errorLogPath=”c:MySQLBackupsbackupfilesdumperrors.txt”

:: MySQL EXE Path
set mysqldumpexe=”C:Program FilesMySQLMySQL Server 5.5binmysqldump.exe”

:: Error log path
set backupfldr=c:MySQLBackupsbackupfiles

:: Path to data folder which may differ from install dir
set datafldr=”C:ProgramDataMySQLMySQL Server 5.5data”

:: Path to zip executable
set zipper=”c:MySQLBackupszip7za.exe”

:: Number of days to retain .zip backup files
set retaindays=30



:: Switch to the data directory to enumerate the folders
pushd %datafldr%

echo “Pass each name to mysqldump.exe and output an individual .sql file for each”

:: Thanks to Radek Dolezel for adding the support for dashes in the db name
:: Added –routines thanks for the suggestion Angel

:: turn on if you are debugging
@echo off

FOR /D %%F IN (*) DO (

IF NOT [%%F]==[performance_schema] (
SET %%F=!%%F:@002d=-!
%mysqldumpexe% –user=%dbuser% –password=%dbpass% –databases –routines –log-error=%errorLogPath% %%F > “%backupfldr%%%F.%backuptime%.sql”
) ELSE (
echo Skipping DB backup for performance_schema

echo “Zipping all files ending in .sql in the folder”

:: .zip option clean but not as compressed
%zipper% a -tzip “” “%backupfldr%*.sql”

echo “Deleting all the files ending in .sql only”

del “%backupfldr%*.sql”

echo “Deleting zip files older than x days now”
Forfiles -p %backupfldr% -s -m *.* -d -%retaindays% -c “cmd /c del /q @path”


::cd[path to directory where your file is saved]
::@echo off
::echo user [here comes your ftp username]>ftpup.dat
::echo [here comes ftp password]>>ftpup.dat
::echo [optional line; you can put “cd” command to navigate through the folders on the ftp server; eg. cdfolder1folder2]>>ftpup.dat
::echo binary>>ftpup.dat
::echo put [file name comes here; eg.]>>ftpup.dat
::echo quit>>ftpup.dat
::ftp -n -s:ftpup.dat [insert ftp server here; eg.]
::del ftpup.dat

echo “done”

::return to the main script dir on end







This script is free to use and update as needed. All we ask is that you like us on Facebook as a thanks.