If you are using named volumes in Docker, sometimes you might want to perform backups of those volumes and restore said backups. It might be for the safety of your data or to move your containers to another host machine.
Uncompressed
In a general sense, here is the command you need to run to perform an uncompressed backup:
$ docker run --rm --volumes-from container_name \
-v $(pwd):/backup busybox \
tar -cvf /backup/backup.tar /path/to/backup
Explanation:
- start a
busyboxcontainer (busybox is a lightweight Linux image with some useful tooling) --rm– remove container after it exits--volumes-from container_name– mounts the volumes used by the container namedcontainer_nameinside the busybox container-v $(pwd):/backup– mounts the current path on the host as a/backupfolder in the busybox container- execute a
tarcommand that packages/path/to/backupto abackup.tarfile in the current directory
To restore the uncompressed backup:
$ docker run --rm --volumes-from container_name \
-v $(pwd):/backup busybox \
tar -xvf /backup/backup.tar -C /
Compressed
Here is the command to perform a compressed backup:
$ docker run --rm --volumes-from container_name \
-v $(pwd):/backup busybox \
tar -czvf /backup/backup.tar.gz /path/to/backup
To restore the compressed backup:
$ docker run --rm --volumes-from container_name \
-v $(pwd):/backup busybox \
tar -xzvf /backup/backup.tar.gz -C /
An example
Ok, now let’s take a look at an example. I will backup and restore the volumes of my nginx-proxy container. The container has 2 volumes, mounted at /data and /etc/letsencrypt.
Backup:
$ docker run --rm --volumes-from nginx-proxy \
-v $(pwd):/backup busybox \
tar -cvf /backup/nginx-data.tar /data
$ docker run --rm --volumes-from nginx-proxy \
-v $(pwd):/backup busybox \
tar -cvf /backup/letsencrypt-data.tar /etc/letsencrypt
Restore:
$ docker run --rm --volumes-from nginx-proxy \
-v $(pwd):/backup busybox \
tar -xvf /backup/nginx-data.tar -C /
$ docker run --rm --volumes-from nginx-proxy \
-v $(pwd):/backup busybox \
tar -xvf /backup/letsencrypt-data.tar -C /
Hope this helps, have fun clickity-clacking.
Leave a Reply