Backup container volumes
A dissection of the command to back up a container volume
This is a short one and is based on Christian Lempa's YouTube video on backing up container volumes. That video helped me quickly understand all the cryptic parts of that command. You can find the command to backup in the Docker documentation, but I wanted to have it written down for myself and future reference.
This command uses podman as that is what NixOS uses to run containers and I happen to use NixOS on my server. But as it is usual with podman, you can run the same command with docker.
Command
podman run --rm --volumes-from CONTAINER -v $(pwd):/backup ubuntu tar cvfz /backup/backup.tar /data
This command does:
podman run
runs a new container--rm
this makes our container temporary and automatically cleans up the container after it has run.--volumes-from CONTAINER
attaches all volumes used by the container we want to backup from the container namedCONTAINER
. Replace this with your container name which you can look up withpodman ps
.-v $(pwd):/backup
binds a volume in the process working directory (pwd) as a volume in our temporary container under the path/backup
. This is where we are going to store the backup.ubuntu
is the image we are using to run the backup command in. You can choose a more lightweight image likebusybox
if you want but I don't really care about the potentially small performance improvement for this and prefer using something I am more familiar with.tar cvfz
collects all files and archives them in a single file for our convenience.cvfz
is a list of options we pass to the command.c
(create) instructs tar to create a new archive.v
(verbose) instructs tar to put out more informationf
(file) instructs tar to put the archive into a file. (I assume in contrast to standard out?)z
(gzip) instructs tar to compress the file using gzip to reduce its size which is useful for our backup as we probably want to move it to another machine in case the current one goes up in smoke. This is not in the official docker documentation.
/backup/backup.tar
is where tar should store the file. This puts it through the mount binding to the process working directory right into the directory from which we execute this command./data
is the last path that tells tar where the data to be backed up is in. This should be the directory inside the container where your container stores the data that would be lost if it was completely destroyed including its volumes.
Example
As I did this to back up a Core Keeper Game Server this is what I used it for:
podman run --rm --volumes-from coreKeeperGameServer -v $(pwd):/backup ubuntu tar cvfz /backup/backup.tar /home/steam/core-keeper-data
If you need to backup multiple directories or files you should be able to just append them separated with a space.
Also note that I needed to run this as sudo as the container under NixOS runs under the system.
Additionally if I wanted to have access to the file as I created it with privileges I needed to change the ownership with chown claas backup.tar
using sudo.
If you want to copy the backup to another machine you can use the scp
(secure copy) command. This would copy it from the server to your local machine's current directory scp username@server:/home/username/backup.tar .
. You can also use this to copy the file from server to server. Replace username and server with the same parameters you'd use for SSH.