Aug 17, 2017, 12:27

Fully automated docker mysql database backup from remote low-end NAS

The main idea is to have fully automated docker database backup from low end D-Link NAS DNS-320.

Solution design is following:

  1. My backup box will copy script to the remote coreos-03 host.
  2. Then remote host copies script into database container.
  3. Backup box executes docker command «docker exec itservice_db_1» on coreos-03 host, which, in turn, executes mysqlbackup. SQL dump is captured directly from command output and then gzipped.
  4. Rsnapshot saves folder with gzipped SQL dump and rotates old backup folders as necessary.

So, we will need only

  • ssh
  • tar
  • rsnapshot

Here is my working implementation:


## env vars are already in docker container


/ffp/bin/scp /ffp/home/root/ core@coreos-03:/home/core/itservice/
/usr/sbin/ssh -C core@coreos-03 "docker cp /home/core/itservice/ itservice_db_1:/usr/local/bin/"
/usr/sbin/ssh -C core@coreos-03 "docker exec itservice_db_1 /usr/local/bin/" > latest.sql
/opt/bin/tar czf itservice-sql-dump.tar.gz latest.sql --remove-files


backup_script	/mnt/HD/HD_a2/ffp/home/root/	coreos-03/itservice_db_1


0 */4 * * * rsnapshot hourly
30 3 * * *  rsnapshot daily
0  3 * * 1  rsnapshot weekly
30 2 1 * *  rsnapshot monthly

Keep in mind, that you will need to generate ssh keys for your backup box and add it to authorized_keys on coreos-03 host, but this is out of scope this article.