Backup MYSQL databases on Windows 10 free Script


We needed a simple way to automatically Backup MySQL on windows 10 for free. Our development and testing environments required:

  • The convenience of backing up all DBs (such as This Script does).
  • Ability to exclude some of the DBs in each machine.
  • Date stamped backups, so changes are backed up each day.
  • Free and preferably open-source.
  • Easy to integrate into our existing backup scripts.

A quick search showed up nothing. We decided to expand the above mentioned script functionality to include what we require. Provided below is the important parts of the script:

:: allows for skipping of particular databases
set SkipThis=0
for /d %%f in (*) do (
    set /A Counterf=!Counterf!+1
    :: remove echo here if you like
    echo processing folder "%%f"
    pushd "%~dp0"
        set Countera=0
        for %%a in (*.exclude) do (
            rem increment the counter, so we know how many files we have read.
            set /A Countera=!Countera!+1
            if %Debug% == 1 echo DEBUG - Exclude file found: "%%~na"
            if %%a == %%f.exclude set SkipThis=1
            if !SkipThis! == 1 if %Debug% == 1 echo DEBUG - Skipping Backup of "%%f"
            )
        If %Debug% == 1 echo DEBUG - !Countera! exclude files checked
    popd

    if !SkipThis! == 0 %mysqldump% --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > %backupDir%\%dirName%\%%f.sql
    if !SkipThis! == 0 %zip% a -tzip %backupDir%\%dirName%\%fileSuffix%_%%f.sql.zip %backupDir%\%dirName%\%%f.sql
    if !SkipThis! == 0 set /A Counterb=!Counterb!+1

    :: make sure to set this back to normal, so we don't skip the next DB backup as well
    set SkipThis=0
)
echo DONE - !Counterf! DBs found, !Counterb! DBs backed up

We then just need to create some blank files with “.exclude” extension. The “.exclude” files have the same name as the DB to exclude. Example such as “sys.exclude” will exclude the “sys” DB from backups

Licensing is as usual, this script is Open Source and we provide a download for your easy of use.

Troubleshooting

  1. If backups are not created, check all your location variables are set correctly

How to Use

  1. Download the MySQL Backup Script and extract into a suitable location.
  2. Open up “MySQLBackup.bat” in notepad++ (or similar).
  3. You will need to change the dbUser, dbPassword, backupDir, mysqldump, mysqlDataDir and zip file/app locations on lines 4-9.
  4. Save the file and run (you can open a command prompt by typing cmd into the title bar of explorer)
  5. This script will now be executable. Go to your command prompt and run this to backup your databases.
  6. If backups are not created, check all your location variables are set correctly

Next time you need to Backup MySQL on windows 10 for free, use this script to make it easy