Docker volume backup and restore the easy way.
I haven’t had to move docker volumes around in a few years, but I finally had the need today. As usual, I searched for the process, knowing that most examples are… well… not very good. Well, as I almost resorted to pulling a manual job using ubuntu, I found a great write-up by Jarek Lipski on Medium. Here’s how you backup using alpine and tar. Also, make sure you “docker stop” the containers that use the volume, so you get a consistent backup.
Which containers use a volume?
docker ps -a --filter volume=[some_volume]
Backup using an alpine image with tar:
docker run --rm -v [some_volume]:/volume -v /tmp:/backup alpine tar -cjf /backup/[some_archive].tar.bz2 -C /volume ./
docker run --rm -v [some_volume]:/volume -v /tmp:/backup alpine sh -c "rm -rf /volume/* /volume/..?* /volume/.[!.]* ; tar -C /volume/ -xjf /backup/[some_archive].tar.bz2"
Backup using loomchild/volume-backup
I love that Jarek also created an image to simplify further the process called loomchild/volume-backup. Here’s how the image works:
docker run -v [volume-name]:/volume -v [output-dir]:/backup --rm loomchild/volume-backup backup [archive-name]
docker run -v [volume-name]:/volume -v [output-dir]:/backup --rm loomchild/volume-backup restore [archive-name]
What’s great is this method allows inline copying of a volume from one system to another using ssh. Here’s an example Jarek provides:
docker run -v some_volume:/volume --rm --log-driver none loomchild/volume-backup backup -c none - |\ ssh firstname.lastname@example.org docker run -i -v some_volume:/volume --rm loomchild/volume-backup restore -c none -