Backing Up MySQL Databases Made Easy on Docker with Mysql-bkup


Introduction
Backing up your data is essential, especially when managing databases in Docker environments. Regular backups of your MySQL database can prevent data loss and ensure business continuity. This guide provides a comprehensive walkthrough on automating nightly backups for a MySQL database running inside a Docker container using the Mysql-bkup tool.
Mysql-bkup is a Docker container image designed to backup, restore, and migrate MySQL databases. It supports multiple storage options, including local storage, S3, SFTP, and Azure Blob, and ensures data security through GPG encryption.
Prerequisites
Before proceeding, ensure the following are in place:
- Docker installed and running.
- A MySQL or MariaDB database running in a Docker container.
- (Optional) S3-compatible storage, SFTP, or Azure Blob storage for remote backups.
Backup Methods
1. Backup Using Local Storage
This method binds local storage to the Docker container for database backups.
Method 1: Using Docker CLI
docker run --rm --network your_network_name \
-v $PWD/backup:/backup/ \
-e "DB_HOST=dbhost" \
-e "DB_USERNAME=username" \
-e "DB_PASSWORD=password" \
-e "DB_PORT=3306" \
jkaninda/mysql-bkup backup -d database_name
Explanation:
- The command runs a new Docker container on the same network as the MySQL database container.
- The backup is saved to a local directory (
./backup
) on the host machine. - Ensure the
mysql-bkup
container is connected to the same Docker network as your database to avoid connectivity issues.
Method 2: Using Docker Compose
Create a compose.yaml
file with the following content:
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
networks:
- web
networks:
web:
Explanation:
- The
mysql-bkup
container is configured to back up the specified database. - The backup is stored in the
./backup
directory on the host machine.
Method 3: Recurring Backups
Mysql-bkup supports scheduled backups using cron expressions. You can use predefined schedules or custom intervals.
Predefined Schedules:
Entry | Description | Equivalent To |
@yearly | Run once a year, midnight, Jan. 1st | 0 0 1 1 * |
@monthly | Run once a month, midnight | 0 0 1 * * |
@weekly | Run once a week, midnight | 0 0 * * 0 |
@daily | Run once a day, midnight | 0 0 * * * |
@hourly | Run once an hour | 0 * * * * |
@every "duration" | Run on fixed intervals ( @every 1h20m ) | - |
Example: Recurring Backup with Docker Compose
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup -d database -e @midnight
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
networks:
- web
networks:
web:
Explanation:
- The
-e @midnight
flag schedules a daily backup at midnight. - Backups are stored in the
./backup
directory.
2. Backup Using S3 Storage
Mysql-bkup supports S3-compatible storage for backups. Use the --storage s3
flag to enable this feature.
Example: S3 Backup with Docker Compose
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup --storage s3 -d database --cron-expression "0 1 * * *"
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
## AWS Configuration
- AWS_S3_ENDPOINT=https://s3.amazonaws.com
- AWS_S3_BUCKET_NAME=backup
- AWS_REGION=us-west-2
- AWS_ACCESS_KEY=xxxx
- AWS_SECRET_KEY=xxxxx
- AWS_DISABLE_SSL="false"
- AWS_FORCE_PATH_STYLE=false
networks:
- web
networks:
web:
Required Environment Variables:
AWS_S3_ENDPOINT
: S3 endpoint URL (e.g.,https://s3.amazonaws.com
).AWS_S3_BUCKET_NAME
: Name of the S3 bucket.AWS_REGION
: AWS region (e.g.,us-west-2
).AWS_ACCESS_KEY
: AWS access key.AWS_SECRET_KEY
: AWS secret key.AWS_DISABLE_SSL
: Set to"true"
for S3 alternatives without SSL.AWS_FORCE_PATH_STYLE
: Set to"true"
for S3 alternatives like Minio.
3. Backup Using Azure Blob Storage
Mysql-bkup also supports Azure Blob storage. Use the --storage azure
flag to enable this feature.
Example: Azure Blob Backup with Docker Compose
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup --storage azure -d database --path your-custom-path
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
## Azure Blob Configuration
- AZURE_STORAGE_CONTAINER_NAME=backup-container
- AZURE_STORAGE_ACCOUNT_NAME=account-name
- AZURE_STORAGE_ACCOUNT_KEY=your-account-key
networks:
- web
networks:
web:
Required Environment Variables:
AZURE_STORAGE_CONTAINER_NAME
: Name of the Azure Blob container.AZURE_STORAGE_ACCOUNT_NAME
: Azure Storage account name.AZURE_STORAGE_ACCOUNT_KEY
: Azure Storage account key.
Notifications
Mysql-bkup can send email or Telegram notifications for backup success or failure.
Email Notifications
Configure SMTP credentials to enable email notifications.
Example: Email Notification Configuration
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
## SMTP Configuration
- MAIL_HOST=smtp.example.com
- MAIL_PORT=587
- MAIL_USERNAME=your-email@example.com
- MAIL_PASSWORD=your-email-password
- MAIL_FROM=Backup Jobs <backup@example.com>
- MAIL_TO=me@example.com,team@example.com
- MAIL_SKIP_TLS=false
- TIME_FORMAT=2006-01-02 at 15:04:05
- BACKUP_REFERENCE=database/Paris cluster
networks:
- web
networks:
web:
Telegram Notifications
Provide your Telegram bot token and chat ID to enable Telegram notifications.
Example: Telegram Notification Configuration
services:
mysql-bkup:
image: jkaninda/mysql-bkup
container_name: mysql-bkup
command: backup
volumes:
- ./backup:/backup
environment:
- DB_PORT=3306
- DB_HOST=mysql
- DB_NAME=database
- DB_USERNAME=username
- DB_PASSWORD=password
## Telegram Configuration
- TG_TOKEN=[BOT ID]:[BOT TOKEN]
- TG_CHAT_ID=your-chat-id
- TIME_FORMAT=2006-01-02 at 15:04:05
- BACKUP_REFERENCE=database/Paris cluster
networks:
- web
networks:
web:
Conclusion
Automating MySQL database backups in Docker using Mysql-bkup is a robust and efficient way to ensure your data is secure and recoverable. With support for multiple storage options local, S3, SFTP, and Azure Blob this tool provides unparalleled flexibility. The added layer of GPG encryption ensures your backups remain secure, while customizable notifications keep you informed about backup successes or failures. By implementing these strategies, you can safeguard your critical data and minimize downtime in case of unexpected failures.
For more detailed instructions and advanced configurations, explore the official documentation: Mysql-bkup Documentation. Start automating your backups today and enjoy peace of mind knowing your data is protected.
Subscribe to my newsletter
Read articles from Jonas Kaninda directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Jonas Kaninda
Jonas Kaninda
I am a Software Engineer with 5+ years of experience specializing in Kotlin, Spring Boot, Golang, MySQL, PostgreSQL, Linux, DevOps, Docker, and Kubernetes. Programming is both my profession and passion, driving me to stay updated with the latest tech trends. I am committed to Open Source, having developed tools for database backup, API Gateway Management, and Kubernetes Operators. My focus areas include Microservices, Cloud-Native Architecture, Kubernetes, DevSecOps, and GitOps practices. With expertise in designing and deploying software systems, I deliver scalable and efficient solutions. I thrive in both independent work and team collaboration, ensuring quality in every project.