4 min read

Automating MySQL Database Backups in Docker

Ensure Your Data’s Safety with Scheduled Nightly Backups

MySQL Docker AI Bash Guide
Automating MySQL Database Backups in Docker

Backing up your data is crucial, especially when it comes to databases. In the world of Docker, ensuring your MySQL database is backed up regularly can save you from potential data loss. Here’s a comprehensive guide on how to automate nightly backups for a MySQL database running inside a Docker container.

Why Automate Database Backups?

Automating your database backups ensures that your data is consistently backed up without manual intervention, reducing the risk of data loss due to human error or oversight. It also allows for recovery in case of accidental deletion or corruption.

Prerequisites

  • A running MySQL container in Docker.
  • Access to the host machine to schedule the backup script.

Step 1: Creating the Backup Script

First, we need a script that performs the backup. This script uses mysqldump to export the database to a SQL file, which can then be compressed to save space.

The Script db_backup.sh:

#!/bin/bash

# Current date in YYYY-MM-DD-HHMMSS format for unique backup filenames
DATE=$(date +%F-%H%M%S)

# Backup directory on the host
BACKUP_DIR="/var/backups/database_backups"

# Database credentials and details
DB_HOST="my-mysql" #name of the mysql container
DB_USER="username"
DB_PASSWORD="password"
DB_NAME="database_name"
NETWORK="my-app-network" #name of the network where mysql container is running. You can check the list of the docker neworks using doocker network ls

# Docker image version of MySQL
MYSQL_IMAGE="mysql:8.0"

# Backup filename
BACKUP_FILENAME="$BACKUP_DIR/$DB_NAME-$DATE.sql"

# Run mysqldump within a new Docker container
docker run --rm --network $NETWORK $MYSQL_IMAGE \\
  /usr/bin/mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASSWORD $DB_NAME > $BACKUP_FILENAME

# Compress the backup file
gzip $BACKUP_FILENAME

Explanation:

  • This script dynamically generates a backup filename based on the current date and time, ensuring each backup file is unique.
  • It then runs a new Docker container on the same network as the database container to execute mysqldump.
  • The output is redirected to a file on the host machine, which is then compressed using gzip.

Setting Execute Permissions:

After creating your backup script db_backup.sh, you’ll need to grant execute permissions to make it runnable. This can be done using the chmod command:

chmod +x /usr/local/bin/db_backup.sh

This command changes the mode of the script to executable, which is required for cron or any user to run it.

Step 2: Scheduling the Backup

To automate the backup process, we use cron, a time-based job scheduler in Unix-like operating systems.

Editing the Crontab:

Open the crontab for editing:

crontab -e

Add the following line to schedule the script to run at 2 AM every night:

0 2 * * * /usr/local/bin/db_backup.sh >> /var/log/db_backup.log 2>&1

This line tells cron to execute the db_backup.sh script at 2 AM daily, redirecting both standard output and errors to /var/log/db_backup.log.

Step 3: Testing the Backup Script

Before relying on this setup, it’s crucial to test the script. Run the script using terminal:

/usr/local/bin/db_backup.sh >> /var/log/db_backup.log 2>&1

Check the backup directory for the SQL file and /var/log/db_backup.log for any potential errors.

Best Practices

  • Regularly Test Backups: Periodically test your backups by restoring them to a test database to ensure they’re valid.
  • Secure Your Backups: Store your backups in a secure location, ideally off-site or in cloud storage, to protect against data loss from physical damage.
  • Monitor Backup Logs: Regularly check your backup logs for errors or issues.

Conclusion

Automating MySQL database backups in Docker ensures your data is safely backed up with minimal manual intervention. This guide provides a straightforward method to set up nightly backups, helping safeguard your data against unforeseen events.

By sharing this process, I hope to help others in the community secure their data and encourage a culture of proactive data management. Whether you’re a seasoned developer or just starting with Docker, understanding how to manage and automate database backups is a valuable skill in ensuring data integrity and availability.


Need Help With Your Laravel Project?

I specialize in building custom Laravel applications, process automation, and SaaS development. Whether you need to eliminate repetitive tasks or build something from scratch, let's discuss your project.

⚡ Currently available for 2-3 new projects

Hafiz Riaz

About Hafiz Riaz

Full Stack Developer from Turin, Italy. I build web applications with Laravel and Vue.js, and automate business processes. Creator of ReplyGenius, StudyLab, and other SaaS products.

View Portfolio →