How To: backup (and restore) Docker containers using Dropbox

Backups are must-have and they should be constant; no one can argue with that, but what's the right way of doing it with Docker containers? Where to keep backups if you don't have an infrastructure for that? I'll show you my best practices for that.

Backup manually

In this section I will describe how to perform manual backups and later one I'll provide some bash scripts so automation can take place with those.

Data volumes

Thats right. The only thing you want to backup is Docker container's data volumes. Except database containers where you can simply use mysqldump command or any other containers with custom implementation (i.e. GitLab).

To back up Docker container volumes (in this example container's name is proxy), first you have to find one (or more), simply by running the following command:

# docker inspect --format '{{range .Mounts}}{{.Source}}{{" -> "}}{{.Destination}}{{"\n"}}{{end}}' proxy
/var/lib/docker/volumes/6890bad0490303e738ecae184da43fe9860e17ceca474c0a74c2d5974976e179/_data -> /usr/share/nginx/html

Then we can simply tar whole data volume path:

tar zcfv backup.tar.gz /var/lib/docker/volumes/6890bad0490303e738ecae184da43fe9860e17ceca474c0a74c2d5974976e179/_data  

Untar backup with the following:

tar zxfv backup.tar.gz -C /  

This will automatically overwrite the contents of your data volume (data volume path must be the same that we've compressed).

Database containers

To backup and restore databases (MySQL / MariaDB) running in a container is very simple. You can do that using the same method described about data volumes, but you can also use mysqldump tool that's more versatile.

Backup with the following command:

  docker exec database sh -c 'exec mysqldump -uroot -p"$MYSQL_ROOT_PASSWORD" --all-databases' > /tmp/backup.sql

This will backup all database tables of database named container to /tmp/backup.sql using $MYSQL_ROOT_PASSWORD environment variable as root password within container.

Restore simply with the following:

docker exec -i database sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /tmp/backup.sql

Backup to Dropbox

Have plenty of unused space in Dropbox? Why not to use it as a backup storage? I've found that Dropbox-Uploader fits my needs perfectly. Follow all instructions on that GitHub page and setup your Dropbox.

Then you can simply backup your .sql or .tar.gz files:

./dropbox_uploader.sh upload backup.tar.gz /location_in_dropbox/

Automating backups

I wrote a simple bash script that decides on what's the best way of backing up a container based on its image name, i.e. if it's MySQL or MariaDB container, mysqldump tool will be used. On all other occasions, data volumes will be compressed using tar command.

Additionally my script can use several Dropbox accounts. The usage of a script is fairly simple:

./backup.sh <container_name> [dropbox_account]

You can add this to crontab and have daily/weekly/monthly backups.

Restore from backup script

As a backup, restoration script acts similarly:

./restore.sh <container_name> <dropbox_account> <days_back>

i.e.

./restore.sh proxy EA -1