Menü schliessen
Created: February 1st 2015
Last updated: May 1st 2020
Categories: Databases,  MySQL
Author: Marcus Fleuti

Create a MySQL-Dump (Backup) in Windows with mysqldump.exe

Tags:  backup,  Batch,  cmd,  database,  MySQL,  mysqldump,  script,  Windows
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

You can use the following CMD file for this. It's quite simple and does some basic error testing as well. Please note that we do NOT take any responsibility for the code. Please test it intensively before going productive with it:

@echo off
echo Backing up all MySQL databases to:
echo C:\MySQLBackup\MySQL_DB_Dump_all-databases.sql. Pleae wait...
rem Check if mysqldump.exe program is here
if not exist "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" goto error
    rem Delete old (previous) dump and make a clean/new one
    if exist "C:\Datatrack\MySQL\Backup\MySQL_DB_Dump_all-databases.sql" del /f /q "C:\Datatrack\MySQL\Backup\MySQL_DB_Dump_all-databases.sql"
    rem the parameter "2>nul" below suppresses the information message from mysqldump informing about insecure passwords in CMD files.
    "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" -uRootUserName -pRootPassword -hlocalhost --all-databases > "C:\MySQLBackup\MySQL_DB_Dump_all-databases.sql" 2>nul
    if %ERRORLEVEL% NEQ 0 (goto error)
goto ende
:error
echo Attention! There was an error backing up the MySQL databases!
echo Please check the backup script and the backup itself for errors:
echo c:\path-to-this-script.cmd
goto ende
:ende