Backing Up Docker Volumes

Backing Up Docker Volumes

In this tutorial we go through creating backups of docker volumes. We cover creating .tar and .zip backup files as well as complete separate docker volume backups of other volumes, finally we cover restoring backups of .tar and .zip files and restoring volume backups of other volumes.

Quick Notes:

  • Stopping Containers: Before creating or restoring backups, it’s best to stop the running container to avoid data inconsistencies or file corruption, especially for databases or frequently changing files.
  • File Ownership and Permissions: After restoring backups, ensure that the file ownership and permissions are correct. For example, database containers like PostgreSQL may require specific user ownership (postgres:postgres). You can adjust ownership using chown.
  • Disk Space: Be mindful of the size of your volumes when creating backups. Large volumes may generate sizable .tar or .zip files, so ensure you have enough space in your current working directory ($(pwd)).
  • Volume Names: Replace placeholders like <volume_name> with the actual names of your volumes. Do not include the angle brackets (<>) in the final command.
  • Verbose Mode: The v flag in tar and zip enables verbose output, showing the files being processed. You can remove this flag if you prefer less output.
  • --strip 1 in .tar Restores: The --strip 1 flag ensures that files are restored directly into the target directory by removing the first level of directories from the .tar archive.

Quick notes provided by ChatGPT

Creating a .Tar Backup of a Volume

To create a .tar backup of our volume we will create a temporary lightweight docker container that mounts our volume and our current working directory, it then creates a tar archive of the volume and writes it to the current working directory. The temporary container is then deleted having performed its function.

							
							
					docker container stop <container_name>				
			

Stop the running container before backing up the associated volumes. 

							
							
					docker run --rm -v <volume_name>:/volume -v $(pwd):/backup alpine tar cvf /backup/backup.tar /volume
				
			

Command breakdown:

docker run

This starts a new container

--rm

This option removes the container after the backup process is completed.

-v <volume_name>:/volume 

This mounts the volume we are backing up to the /volume directory on the temporary container

-v ($pwd):/backup

This mounts the current working directory (via $pwd) to the /backup directory on the temporary container.

alpine 

Specifies the alpine linux image.

tar cvf /backup/backup.tar /volume 

This creates a tar archive called backup.tar of the /volume directory inside the temporary container (this is where we mounted the docker volume to) to the /backup directory inside the temporary container (this is where we mounted our current working directory too).

Modify this command as needed and be sure to replace <volume_name> with your volume’s name. After running you should see a backup.tar file in your working directory.

Creating a .Zip Backup of a Volume

Creating a .zip archive backup of a docker volume closely follows the same process:

							
							
					docker container stop <container_name>				
			

Stop the running container before backing up the associated volumes.

							
							
					docker run --rm -v <volume_name>:/volume -v $(pwd):/backup alpine sh -c "apk add --no-cache zip && cd /volume && zip -r /backup/backup.zip ."
				
			

The only difference between this command and the last is that it adds the zip utility (as alpine doesn’t automatically come with it) and uses zip instead of tar.

Backing Up a Volume to Another Volume

To backup an existing volume to another docker volume we will first create a new docker volume via:

							
							
					docker volume create <my_new_volume_name>				
			

We will then mount both volumes to a temporary container and copy the content of the original volume to the new volume.

							
							
					docker container stop <container_name>				
			

Stop the running container before backing up the associated volumes.

							
							
					docker run --rm -v <original_volume_name>:/from -v <my_new_volume_name>:/to alpine sh -c "cd /from && cp -a . /to"
				
			

Command Breakdown: 

docker run --rm 

Creates a new temporary docker container

-v <original_volume_name>:/from -v <my_new_volume_name>:/to 

Mounts the original volume (the one we are backing up) to the /from directory inside the temporary container and mounts the new directory (the one we are backing up files to) to the /to directory on the temporary container.

alpine 

Specifies the container image is alpine linux.

sh -c "cd /from && cp -a . /to"

This runs the “cd  /from && cp -a . /to” command in the shell. The command changes directories to the /from directory and copies all files recursively (ensuring the files remain as is) from the /from directory to the /to directory.

Restoring Backups

To restore backups we essentially reverse the backup process. 

Restoring a .Tar Backup

To restore a .tar backup we can run:

							
							
					docker container stop <container_name>				
			

Stop the running container before backing up the associated volumes.

							
							
					docker run --rm -v <volume_name>:/volume -v $(pwd):/backup alpine sh -c "cd /volume && tar xvf /backup/backup.tar --strip 1"
				
			

This creates a temporary docker container (again with the alpine linux image), mounts the volume we want to restore from the backup to the /volume directory inside the temporary container, mounts the backup.tar file (located in the current working directory) to the /backup directory inside the temporary container and then extracts the tar archive to the /volume directory inside the temporary container.

Restoring a .Zip Backup

To restore a .zip backup we can run: 

							
							
					docker container stop <container_name>				
			

Stop the running container before backing up the associated volumes.

							
							
					docker run --rm -v <volume_name>:/volume -v $(pwd):/backup alpine sh -c "apk add --no-cache unzip && cd /volume && unzip /backup/backup.zip"
				
			

This essentially follows the same restore process as for the .tar file restore, the only difference being that it is for a .zip file.

Restoring a Volume Backup

To restore a volume backup we can run:

							
							
					docker container stop <container_name>				
			
							
							
					docker run --rm -v <my_new_volume_name>:/from -v <original_volume_name>:/to alpine sh -c "cd /from && cp -a . /to"
				
			

This essentially mounts two volumes, the volume that we backed the other volume up to originally (this is <my_new_volume_name> volume, to be clear this is the volume that we will use to restore the <original_volume_name> volume), and the original volume that we backed up earlier (the <original_volume_name> volume).

That’s a wrap as always let me know your thoughts! 

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.