Because I want :
- The same container and same environments variables for backup my redis, mysql and mongodb databases.
- Auto rotation backup files.
- Alert (mail or slack) if the backup size is suspect or backup fail.
- Send backup to S3.
- ...
PLEASE . REMEMBER . TO . TEST . YOUR . BACKUPS . REGULARLY .
You can mount the directory /data. The directory contains 2 subdirectories :
/data/backupDirectory where is saved the backup files./data/restore(for mysql and mongo only) copy file from backup in this directory and the backup is imported in database.
All variables can used with docker secrets, simply append _FILE of the variable name, and set the secret file path. i.e. :
BACKUP_PASS_FILEand set to/run/secrets/BACKUP_PASS
BACKUP_NAME_FORMAT: (OPTIONNAL) Name format of backup (without extension). It use date format. Default is+%Y-%m-%d.%Hh%M.%S
BACKUP_CRON_TIME: (OPTIONNAL) Cron schedule, default is0 0 * * *BACKUP_RUN_ON_START: (OPTIONNAL) Run first backup when container start if this env is definedBACKUP_RUN_ON_START_DELAY: (OPTIONNAL) Delay in seconds if run on start is set. Default is20
BACKUP_DRIVER: Backup type, must bemongoBACKUP_HOST: Mongo hostBACKUP_PORT: Mongo portBACKUP_USER: Mongo usernameBACKUP_PASS: Mongo passwordBACKUP_DB: Mongo databaseBACKUP_EXTRA_OPTS: Extra options
BACKUP_DRIVER: Backup type, must bemysqlBACKUP_HOST: MySQL hostBACKUP_PORT: MySQL portBACKUP_USER: MySQL usernameBACKUP_PASS: MySQL passwordBACKUP_DB: MySQL database (optionnal)BACKUP_EXTRA_OPTS: Extra options (you can use--all-databases)
BACKUP_DRIVER: Backup type, must beredisBACKUP_HOST: Redis hostBACKUP_PORT: Redis portBACKUP_PASS: Redis passwordBACKUP_EXTRA_OPTS: Extra options
You can contribute ;)
See rotate-backups for more details
BACKUP_ROTATE_MINUTELY: NumberBACKUP_ROTATE_HOURLY: NumberBACKUP_ROTATE_DAILY: NumberBACKUP_ROTATE_WEEKLY: NumberBACKUP_ROTATE_MONTHLY: NumberBACKUP_ROTATE_YEARLY: NumberBACKUP_ROTATE_OPTS: Add more options
Send alert if backup file is not created, the size of last backup is greater a value or backup size is less a value.
BACKUP_ALERT_NAME: Name of backup, i.e. :My project - Mongo BackupBACKUP_ALERT_IF_THE_SIZE_OF_LAST_BACKUP_GREATER_THAN: Alert from diff size with last backup is lower. See numfmt format. Default no alertBACKUP_ALERT_IF_THE_BACKUP_SIZE_IS_SMALLER_THAN: If the size of backup is smaller of this value. See numfmt format. Default no alert.
Availables transports :
BACKUP_ALERT: value must beslackBACKUP_SLACK_WEBHOOK_URL: i.ehttps://hooks.slack.com/services/XXX/YYYY/ZZZZ
BACKUP_ALERT: value must bemailBACKUP_MAIL_FROM: expeditor@domain.comBACKUP_MAIL_HOST: smtp.host.comBACKUP_MAIL_PASS: mail passwordBACKUP_MAIL_TO: my@email.comBACKUP_MAIL_USER: usermail
BACKUP_S3_ACCESS_KEYBACKUP_S3_HOST_BASEBACKUP_S3_HOST_BUCKETBACKUP_S3_OPS: Additional options, you can (should?) use--delete-removedBACKUP_S3_PATH: i.e.:s3://mys3/my-projet-mongo-backup/BACKUP_S3_REGION: defaultus-west-1BACKUP_S3_SECRET_KEYBACKUP_S3_USE_HTTPS: defaultTrue
Simply copy zipped file in data/restore directory. A cron try to restore a database all minutes.
- Stop redis container
- Unzip the backup file and remplace the dump.rdb
- Start redis container
Host port 8042 and 9000 is required for test
- Clone the repository
git clone https://github.com/apoutchika/backup - Start databases and backups
docker-compose up - Wait all databases is started, and wait the first backup
- Run
./fixtures.shfor create datas in DB - See next backup size, on
data/backupsdirectory or on S3
You can see alert mail on http://localhost:8042/, and S3 data on http://localhost:9000/ (access is s3\_key and s3\_secret).
The backup is created all minutes.
Test restore :
- Stop all container
docker-compose down - Delete datas
sudo rm -rf ./data/{mongo,mysql,redis}/* - unzip last redis backup and move dump.rdb on
./data/redis/dump.rdb - Restart containers
docker-compose up --force-recreate - Copy file in
./data/backups/mongo/backup/to./data/backups/mongo/restore/for MongoDB - Copy file in
./data/backups/mysql/backup/to./data/backups/mysql/restore/for MySQL - See next data backup size, or connect to the container for see datas
Test alerts :
- Create a big fake backup, the next is smaller and send mail alert :
docker-compose exec backup-mysql bash -c "truncate -s 3M /data/backup/fakeBigBackup.gz"docker-compose exec backup-mongo bash -c "truncate -s 3M /data/backup/fakeBigBackup.gz"docker-compose exec backup-redis bash -c "truncate -s 3M /data/backup/fakeBigBackup.gz"
- Wait next backup, see alerts
- Try to delete fixutres with
./clean.sh. After the next backup, see alerts on webmail - Try to stop database :
docker-compose stop mysql mongo redis. After, the next backup, see alerts on webmail