Automated MySQL Backup Script with Docker and Crontab
This guide explains how to set up an automated MySQL backup solution on AlmaLinux using Docker containers, a Bash script, and crontab, covering environment preparation, script creation, command checks, logging, error handling, and optional enhancements such as email notifications and security best practices.
This article provides a step‑by‑step tutorial for creating an automated MySQL backup system on an AlmaLinux 8.x host where the MySQL server runs inside a Docker container.
Environment
Operating system: AlmaLinux 8.x
Database: MySQL running in Docker
Scripts directory: ~/scripts
Preparation – Create script directory
Log in to the master server: ssh [email protected]
Create the directory and enter it: mkdir -p ~/scripts cd ~/scripts
Verify permissions: ls -ld ~/scripts Expected output (example): drwxr-xr-x 2 dba dba 4096 May 25 10:00 /home/dba/scripts
Shell script – backup_mysql.sh
The script backs up all databases at 02:00 each day and retains the last 7 days of backups.
#!/bin/bash
# backup_mysql.sh – automatic MySQL master backup
# Environment: AlmaLinux + Docker
# Dependencies: docker, gzip, mysqldump
# ---- 1. Basic variables ----
CONTAINER_NAME="mysql-master"
BACKUP_DIR="/home/dba/scripts/backups"
RETENTION_DAYS=7
DATE=$(date +"%F_%H%M")
# ---- 2. Command checks ----
for cmd in docker mysqldump gzip; do
command -v $cmd > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: command $cmd not found, please install it and retry!"
exit 1
fi
done
# ---- 3. Create backup directory ----
mkdir -p "$BACKUP_DIR"
if [ $? -ne 0 ]; then
echo "Error: failed to create directory $BACKUP_DIR!"
exit 1
fi
# ---- 4. Perform backup ----
BACKUP_FILE="$BACKUP_DIR/mysql_backup_${DATE}.sql.gz"
echo "[$(date +"%F %T")] Starting backup: $BACKUP_FILE"
docker exec $CONTAINER_NAME \
mysqldump -uroot -p'your_password' --all-databases \
| gzip > "$BACKUP_FILE"
if [ $? -eq 0 ]; then
echo "[$(date +"%F %T")] Backup completed!"
else
echo "[$(date +"%F %T")] Backup failed!"
exit 1
fi
# ---- 5. Clean old backups ----
echo "[$(date +"%F %T")] Cleaning backups older than $RETENTION_DAYS days"
find "$BACKUP_DIR" -type f -name "mysql_backup_*.sql.gz" -mtime +$RETENTION_DAYS -print -delete
echo "[$(date +"%F %T")] Cleanup finished!"Crontab configuration
Open the crontab editor (recommended editors: vim or nano ) and add the following line at the end of the file:
02 * * * * /home/dba/scripts/backup_mysql.sh >> /home/dba/scripts/backup.log 2>&1This schedules the script to run daily at 02:00 and appends both standard output and error output to backup.log .
Common error troubleshooting
Error Message
Cause & Solution
bash: backup_mysql.sh: command not foundMake the script executable (
chmod +x backup_mysql.sh) or use the absolute path.
docker: command not foundInstall Docker (
sudo dnf install docker) and ensure it is in
PATH.
mysqldump: command not foundInstall MySQL client inside the container (
yum install -y mysql).
permission deniedAdjust directory permissions (
chmod -R 755 ~/scripts).
File not foundUse an absolute path in the crontab entry.
Advanced usage & notes
Email notification (append at script end): mail -s "MySQL Backup Report $(date +'%F')" [email protected] < backup.log
Master‑slave replication check via a separate check_replication.sh script using SHOW SLAVE STATUS .
Centralized log management – ship backup.log to a log server or ELK stack.
Security – avoid hard‑coding passwords; use .my.cnf or Docker secrets.
Summary
Script writing: variables, command checks, error exits, logging.
Scheduled execution: crontab configuration and log redirection.
Troubleshooting: command existence, absolute paths, permissions.
Environment dependencies: AlmaLinux, Docker, GNU tools.
Follow the example, adjust paths and credentials as needed, and your MySQL database will be backed up reliably every night.
IT Xianyu
We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.
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.