Master MySQL Backups with mysqldump, Bash Scripts, and Cron
This guide details how to protect MySQL databases from errors and crashes by using mysqldump for manual dumps, creating Bash scripts for automated daily backups, and scheduling them with Linux cron, including command examples, restoration methods, and crontab syntax for reliable periodic backups.
During data operations, errors or database crashes can occur, so regular MySQL backups are essential. This article explains several methods for scheduled MySQL backups using the mysqldump command, Bash scripts, and Linux cron jobs.
1. mysqldump command backup
The mysqldump utility exports database data and structure. Basic usage:
# MySQLdump common
mysqldump -u root -p --databases db1 db2 > xxx.sql2. Common mysqldump examples
Backup all databases (data and structure):
mysqldump -uroot -p123456 -A > /data/mysqlDump/mydb.sqlBackup only structure (add -d):
mysqldump -uroot -p123456 -A -d > /data/mysqlDump/mydb.sqlBackup only data (add -t):
mysqldump -uroot -p123456 -A -t > /data/mysqlDump/mydb.sqlBackup a single database:
mysqldump -uroot -p123456 mydb > /data/mysqlDump/mydb.sqlBackup a single database’s structure:
mysqldump -uroot -p123456 mydb -d > /data/mysqlDump/mydb.sqlBackup a single database’s data:
mysqldump -uroot -p123456 mydb -t > /data/mysqlDump/mydb.sqlBackup multiple tables:
mysqldump -uroot -p123456 mydb t1 t2 > /data/mysqlDump/mydb.sqlBackup multiple databases at once:
mysqldump -uroot -p123456 --databases db1 db2 > /data/mysqlDump/mydb.sql3. Restoring MySQL backups
Two ways: using the MySQL client or a shell command.
From command line:
mysql -uroot -p123456 < /data/mysqlDump/mydb.sqlInside MySQL client:
source /data/mysqlDump/mydb.sql4. Bash script for automated backups
A sample script mysql_dump_script.sh keeps the last 31 daily backups, logs actions, and removes the oldest files when the limit is exceeded.
#!/bin/bash
# keep 31 backups
number=31
backup_dir=/root/mysqlbackup
dd=$(date +%Y-%m-%d-%H-%M-%S)
tool=mysqldump
username=root
password=TankB214
database_name=edoctor
if [ ! -d $backup_dir ]; then
mkdir -p $backup_dir
fi
$tool -u$username -p$password $database_name > $backup_dir/$database_name-$dd.sql
echo "create $backup_dir/$database_name-$dd.dupm" >> $backup_dir/log.txt
delfile=$(ls -l -crt $backup_dir/*.sql | awk '{print $9}' | head -1)
count=$(ls -l -crt $backup_dir/*.sql | awk '{print $9}' | wc -l)
if [ $count -gt $number ]; then
rm $delfile
echo "delete $delfile" >> $backup_dir/log.txt
fi5. Scheduling the script with crontab
Enable the cron daemon and add a crontab entry to run the script daily (example at 02:00):
0 2 * * * /root/mysql_backup_script.shTypical crontab syntax is minute hour day-of-month month day-of-week command with special characters * / - ,. Common schedule examples are provided.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
