In my case, I had 50+ project databases, each with its own user. Doing manual exports was:
β Time-consuming
β Error-prone
β Not scalable
So I built a simple Windows .bat automation script to handle everything in one go i.e cleanly, consistently, and safely.
When working across multiple projects:
Each project had its own database
Each database had its own user + permissions
Backups needed to be:
Frequent
Organized
Reliable
Manual process:
mysqldump db1 > db1.sql
mysqldump db2 > db2.sql
...
π Multiply that by 50+β¦ not fun.
I created a script that:
β
Automatically loops through all databases
β
Skips system databases
β
Exports each DB individually
β
Compresses backups to save space
β
Stores everything in a timestamped folder
β
Also exports users + permissions
Each time the script runs, it creates a folder like:
C:\DB_Backups\08042026_1530\
databases\
project_db_08042026_1530.sql.gz
users\
users_08042026_1530.sql
This ensures:
No overwriting
Easy tracking
Clean restore points
Hereβs the full script (sanitized for safety):
@echo off
setlocal enabledelayedexpansion
:: CONFIG
set BASE_DIR=C:\DB_Backups
:: DATE + TIME
for /f %%i in ('powershell -command "Get-Date -Format ddMMyyyy_HHmm"') do set DATETIME=%%i
:: FOLDERS
set BACKUP_DIR=%BASE_DIR%\%DATETIME%
set DB_DIR=%BACKUP_DIR%\databases
set USER_DIR=%BACKUP_DIR%\users
if not exist "%BASE_DIR%" mkdir "%BASE_DIR%"
if not exist "%BACKUP_DIR%" mkdir "%BACKUP_DIR%"
if not exist "%DB_DIR%" mkdir "%DB_DIR%"
if not exist "%USER_DIR%" mkdir "%USER_DIR%"
echo Starting backup...
:: DATABASE BACKUP
for /f "skip=1 tokens=*" %%D in ('mysql -u root -e "SHOW DATABASES;" ^| findstr /V "information_schema mysql performance_schema sys"') do (
set DB=%%D
echo Backing up !DB! ...
mysqldump -u root !DB! | gzip > "%DB_DIR%\!DB!_%DATETIME%.sql.gz"
)
:: USERS + GRANTS BACKUP
set USER_FILE=%USER_DIR%\users_%DATETIME%.sql
echo -- Users Backup > "%USER_FILE%"
echo -- Generated on %DATETIME% >> "%USER_FILE%"
mysql -u root -N -e "SELECT CONCAT('CREATE USER ''', user, '''@''', host, ''' IDENTIFIED BY PASSWORD ''***'';') FROM mysql.user;" >> "%USER_FILE%"
echo -- Grants >> "%USER_FILE%"
for /f "skip=1 tokens=1,2" %%U in ('mysql -u root -e "SELECT user, host FROM mysql.user;"') do (
mysql -u root -N -e "SHOW GRANTS FOR '%%U'@'%%V';" >> "%USER_FILE%"
)
echo Backup complete!
pause
Make sure you have:
mysql and mysqldump in your PATH
gzip installed (via Git Bash or Chocolatey)
Before sharing or using scripts like this publicly:
β Avoid exposing real database names
β Mask password hashes
β Donβt include real credentials
Example (sanitized):
IDENTIFIED BY PASSWORD '***'
Backing up databases alone is not enough.
Without users:
Access denied for user...
Organizing backups by date + time:
Prevents overwrites
Makes restores easier
Once this is set up:
Run once β everything backed up
Zero stress
If you want to take this further:
π Schedule with Task Scheduler (daily backups)
βοΈ Upload to cloud (S3 / Google Drive)
π Encrypt backups
π§Ή Auto-delete old backups
β‘ Parallel dumping for speed
This script wasnβt just about automation β it came from real, practical needs that most developers eventually run into.
π Preparing for automation (cron jobs / scheduled tasks)
I needed a way to run backups automatically without manual intervention.
This script can easily plug into Windows Task Scheduler or even cron in a server environment.
π» System resets & OS reinstalls
Sometimes you need to:
Format your computer
Upgrade your OS
Switch development environments
Having a complete backup (databases + users + permissions) means you can restore everything exactly as it was β no guesswork.
π¦ Managing multiple projects at scale
With 50+ databases:
Manual backups donβt scale
Itβs easy to forget one
Inconsistency becomes a risk
This script ensures everything is backed up consistently in one run.
π¨ Safety net before risky changes
Before:
Running migrations
Refactoring schemas
Testing new features
π One command gives you a full restore point
π§ Peace of mind
Honestly, this is the biggest one.
Knowing that:
Your data is backed up
Your users and permissions are backed up
You can recover anytime
π That removes a lot of stress from development work.
π Closing Thought
Good developers write code.
Great developers protect their data.
This setup turned backups from:
β A chore
β A risk
into:
β
A one-command habit
β
A reliable safety system
If youβre working with multiple databases, this kind of automation isnβt just helpful β itβs essential.
If youβve built something similar or improved this approach, Iβd love to hear it π