How to Automatically Back Up MySQL Databases on Windows Server

A simple method using 7-Zip, a batch script, and Windows Task Scheduler

Posted by Hüseyin Sekmenoğlu on January 10, 2014 DevOps & Deployment

When setting up a MySQL server on a Windows Server machine, I started by installing the latest tools and software. Once MySQL was installed, I noticed that it stored its data files in the following folder:
C:\ProgramData\MySQL\MySQL Server 5.1\data\

Each database had its own separate folder inside. That gave me an idea: if I could simply back up these folders and save each one as a ZIP file with the current date in its filename, I’d have a clean and functional backup system.

I had done similar backup automation before for MS SQL Server and even some Access databases, so this approach felt familiar.


🧩 The Backup Strategy

After some research and trial and error, I put together a working solution using:

  • 7-Zip to compress files

  • A batch file (.bat) to automate the backup process

  • Windows Task Scheduler to run it daily

The trickiest part was writing the batch file. Even though most commands are commented online, making them work correctly still takes time and testing.

Let’s get to the actual batch script.

echo off
:: tarihi yyyy-mm-dd-hh şekline yazdırmak için komut
set year=%DATE:~6,4%
set day=%DATE:~0,2%
set mnt=%DATE:~3,2%
set hrs=%TIME:~0,2%
IF %day% LSS 10 SET day=0%day:~1,1%
IF %mnt% LSS 10 SET mnt=0%mnt:~1,1%
IF %hrs% LSS 10 SET hrs=0%hrs:~1,1%
set backuptime=%year%-%mnt%-%day%-%hrs%

:: Yedeklemenin yapılacağı klasörün adresi
set backupfldr=d:\Backup\MySQL\
:: Vertibanalarının bulunduğu klasörün adresi
set datafldr=C:\ProgramData\MySQL\MySQL Server 5.1\data\
:: 7-zip sıkıştırıcı programının adresi
set zipper="d:\7za.exe"
:: AYARLAR SON

:: KLASÖRE GİT VE YEDEKLE
pushd %datafldr%
FOR /D %%F IN (*) DO (
IF NOT [%%F]==[mysql] (
%zipper% a -tzip "%backupfldr%%%F-%backuptime%.zip" "%datafldr%%%F"
)
)
:: SON
echo "done"
popd

✅ Final Thoughts

This setup turned out to be a simple, lightweight, and effective solution for daily MySQL backups. No need for expensive tools or complicated software. With a bit of scripting and the help of Windows' built-in scheduling tools, your databases can be safely archived every day.

Let me know if you’d like the full batch file with explanations, or a follow-up guide on automating remote backups.