Backup & Restore Docker Named Volumes

When I was looking for a way to back up Docker named volumes I was surprised to find out that there’s no standard way of handling the process. In the official documentation there’s only a note about using data volume containers and a --volumes-from option. There’s also a docker cp command, but it requires the volume path inside the container that uses them, which makes it less generic.

After a bit of research, it turned out it’s actually pretty easy to back up volumes using volume mounts and a tar utility. For example to backup some_volume to /tmp/some_archive.tar.bz2, you can simply run:

And to restore it, just run:

Note: As a safety precaution, I advise you to stop all the containers using the volume being backed-up or restored, otherwise an inconsistent / intermediate state might be archived or recovered.

In the end I wrote my own little volume-backup utility that simplifies the backup & restore process even further and offers some additional benefits, such as using stdout and stdin streams.

Here’s the example backup command:

And here’s an example restore command:

Feel free to check out the project page on GitHub for complete documentation and let me know what you think.

Edit: Changed the cleanup code to delete hidden files — thanks Olivier.

Edit: It’s also possible to backup to standard output and restore from standard input. I added this capability to volume-backup — thanks Holger.

Edit: Added –rm flag to remove the container when finished — thanks awade.

Originally published at loomchild.net on March 26, 2017.

Full-Stack Developer / Agilist / Free and Open Source Software Fan.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store