Bat script to backup MySQL on a Windows Server

Use the script below to backup your MySQL server running on a Windows server. Just create the bat file then create a scheduled task to run it. This file will append the date to the file name and keeps files up to 7 days. Adjust as needed.

*Backs up the files to C:\MySQLBackup. Make sure to create that directory.
*Make sure to change the username and password.
*Edit the lines below for your version of MySQL.
*PUSHD “C:\ProgramData\MySQL\MySQL Server 5.6\data”
*”C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe”


:: Check to see if files older than 7 day exist
FORFILES /P "C:\MySQLBackup" /M * /D -7 /C "CMD /C DEL @path"

:: MySQl DB user
SET dbuser=root

:: MySQl DB users password
SET dbpass=************

:: Switch to the MySQL data directory and collect the folder names
PUSHD "C:\ProgramData\MySQL\MySQL Server 5.6\data"

:: Loop through the folders and use the fnames for the sql filenames, collects all databases automatically this way

ECHO "Pass each name to mysqldump.exe and output an individual .sql file for each"

FOR /D %%F IN (*) DO (
"C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -u %dbuser% -p%dbpass% -P 3306 %%F > "C:\MySQLBackup\%%F_%date:~-4,4%%date:~-7,2%%date:~-10,2%.sql"
)-

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.