Automatically Backup MySQL Databases on Windows

Schedule automatic MySQL backups on Windows with mysqldump and Task Scheduler, updated for MySQL 8.4: routines, events and safer password handling.

Automatically Backup MySQL Databases on Windows

If MySQL runs on a Windows server and nobody remembers to export the databases, the backup you need on a bad day is the one that does not exist. In 2017 I solved this with one mysqldump line in a .bat file and Windows Task Scheduler. This way to automatically back up MySQL databases on Windows still works. MySQL has changed since then, though, so I have added Update 2026 notes wherever the original needs correcting.

Why this approach

mysqldump writes your databases to a plain SQL file that you can restore almost anywhere. Task Scheduler runs that command on a timetable without anyone logging in. Together they give you a dependable daily backup with no extra software.

Step 1: Write the backup command

My original command for all databases (username root, password 123456) was:

mysqldump -uroot -p123456 --all-databases > "D:/mysql_backup_%date:~-4,4%-%date:~-10,2%-%date:~-7,2%-%time:~0,2%_%time:~3,2%_%time:~6,2%.sql"

This produced a file such as mysql_backup_2017-10-21-20_29_54.sql. For one database, replace --all-databases with --databases mydatabasename.

The date parts meant:

  • %date:~-4,4%: year, 4 digits
  • %date:~-10,2%: month, 2 digits
  • %date:~-7,2%: day, 2 digits
  • %time:~0,2%: hour, 24-hour format
  • %time:~3,2%: minutes
  • %time:~6,2%: seconds

Update 2026: change several things before you use this today.

  1. Add --routines --events. The MySQL 8.4 mysqldump manual says that from MySQL 8.4 the mysql.proc and mysql.event tables are no longer used. As a result, --all-databases alone no longer carries stored routines and events. Without these flags, a restore can quietly miss them.
  2. Add --single-transaction for a consistent InnoDB snapshot without locking the server.
  3. Take the password off the command line. MySQL's password security guidelines say this should be considered insecure. They recommend mysql_config_editor, which stores credentials in a login path file. That file belongs to one Windows user, so the scheduled task must run as the account that created it. Lock that account down as well.
  4. Stop slicing %date% and %time%. Their output depends on regional settings, so day and month can swap, and before 10:00 the hour has a leading space. A PowerShell timestamp is the same on any locale.
  5. Use --result-file instead of >. The manual recommends this on Windows to avoid newline conversion.
  6. Use a dedicated backup user, not root, with only the privileges mysqldump needs. Also call mysqldump.exe by its full path, as it is often not on PATH.

The updated script, as an example to test on your own system:

@echo off
setlocal
rem One-time setup, run as the task's account:
rem   mysql_config_editor set --login-path=backup --host=localhost --user=backup_user --password
for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyy-MM-dd_HH-mm-ss"') do set TS=%%i
"C:\Program Files\MySQL\MySQL Server 8.4\bin\mysqldump.exe" --login-path=backup ^
  --all-databases --single-transaction --routines --events ^
  --result-file="D:\Backups\mysql_backup_%TS%.sql"

Step 2: Save it as a .bat file

Save the command as mysql-backup.bat in a folder that ordinary users cannot modify or delete, for example a protected folder on C:.

Step 3: Schedule it with Task Scheduler

  1. Press Windows key + R and type taskschd.msc. Windows Run
  2. Click Create Task in the right sidebar, or use the Action menu. Task Scheduler
  3. On the General tab, name the task, for example MySQL Database Backup - Daily. Task Scheduler General Tab
  4. Open Triggers, click New… and set your schedule. Task Scheduler Trigger
  5. Click OK, then open the Actions tab. Task Scheduler New Action
  6. Click New… and browse to mysql-backup.bat. Task Scheduler New Action
  7. Click OK, then OK again to save.

Update 2026: on the General tab, choose Run whether user is logged on or not. Run the task under a dedicated account, the same one that created the login path.

Check that it worked

  • Right-click the task and choose Run.
  • Confirm Last Run Result shows success (0x0).
  • Open the backup folder and check the new file has a sensible size.
  • Look at the end of the file for the -- Dump completed line.
  • Most importantly, restore it on a test server now and then. A backup you have never restored is only a hope.

Cautions

  • Back up before you change anything. Take a manual dump before editing users, privileges or the script.
  • Same server is not a backup. A file on the server's own D: drive will not survive disk failure or ransomware. Copy it to another machine or storage, encrypt it, and delete old files on a schedule. If you would rather not run this yourself, my company, Standing, offers cloud hosting and managed services that can take this off your plate.
  • Restoring under another name: --databases adds a USE statement to the dump, which overrides the database you name when restoring.
  • Larger databases: mysqlpump has been removed in MySQL 8.4. mysqldump suits small and medium databases. For parallelism and compression, look at MySQL Shell's dump utilities.
  • Versions: MySQL 5.7, current when I first wrote this, has reached end of life. Plan your move to 8.4 LTS.

The core idea is unchanged: one script, one scheduled task. Add --routines --events, keep the password out of the script, and prove your backups by restoring them.

Sources & further reading

  1. MySQL 8.4 Reference Manual: mysqldumpdev.mysql.com
  2. MySQL 8.4: What Is New since MySQL 8.0dev.mysql.com
  3. MySQL 8.0: End-User Guidelines for Password Securitydev.mysql.com
  4. MySQL 8.4: Making a Copy of a Databasedev.mysql.com
  5. OpenLogic: MySQL 8 End of Lifeopenlogic.com

📝 First written from my own work in 2017; updated and expanded in November 2026 with AI assistance, keeping my original fix. Spotted something wrong? Tell me.

Akam Omer
Written by

Akam Omer

IT leader, engineer and security specialist from Erbil with 20+ years across higher education, telecoms, healthcare and government. Founder of BulkSMSIraq, VerifyWay and Kurd.ai.