Backing up your docker volumes

Table of Contents

Backing up your docker volumes

https://github.com/soiqualang/docker-backup-volume


How to backup your volume

backup-volume.sh will create a new running docker container and mount the volume to the container. A docker cp will be run and the contents of the volume will be saved into a location determined by:

$prefix/$volume/<timestamp>_$volume.tar.gz

An example to run the command is the following:

backup-volume.sh -v <volume_name> -p <prefix>
backup-volume.sh

```bash
#!/usr/bin/env bash

# creates a backup of the docker volume

while [ $# -gt 0 ]; do
case "$1" in
-v|-volume|--volume)
volume="$2"
;;
-p|-prefix|--prefix)
prefix="$2"
;;
-h|-help|--help)
printf "--volume the name of the volume\n"
printf "--mount the mount point of the volume\n"
exit 1
;;
*)
printf "***************************\n"
printf "* Error: Invalid argument.*\n"
printf "***************************\n"
exit 1
esac
shift
shift
done

uuid=$(cat /proc/sys/kernel/random/uuid)
if [ ! -d $prefix/$volume ]; then
mkdir -p $prefix/$volume
fi

IMAGE=alpine:latest
docker run \
--mount "type=volume,src=${volume},dst=/data" \
--name $uuid \
$IMAGE

timestamp=$(date +%Y-%m-%d_%H%M%S)
docker cp -a $uuid:/data /tmp/$uuid
tar -C /tmp/$uuid -czf $prefix/$volume/${timestamp}_${volume}.tar.gz .
rm -rf /tmp/$uuid
docker rm $uuid
```

backup-volume.sh (last update)

Restoring your docker volume

Volumes can be restored using create-volume-from-backup.sh by invoking it in the following manner:

create-volume-from-backup.sh -v <volume_name> -p <prefix>

Optionally if you wish to create a volume with a different name from the backup the script can be invoked in the following manner:

create-volume-from-backup.sh -v <volume_name> -p <prefix> -n <new_volume_name>
create-volume-from-backup.sh

```bash
#!/bin/bash
# creates a docker volume from a backup

while [ $# -gt 0 ]; do
case "$1" in
-v|-volume|--volume)
volume="$2"
;;
-p|-prefix|--prefix)
prefix="$2"
;;
-n|-volume-name|--volume-name)
volume_name="$2"
;;
-s|-snapshot|--snapshot)
snapshot="$2"
;;
-h|-help|--help)
printf "--volume the backup name of the volume\n"
printf "--volume-name (optional) create the backup as this volume name\n"
printf "--prefix the storage prefix of the backup location"
exit 1
;;
*)
printf "***************************\n"
printf "* Error: Invalid argument.*\n"
printf "***************************\n"
exit 1
esac
shift
shift
done

if [ -z "$volume_name" ]; then
volume_name=$volume
fi

volume_exists=$(docker volume ls | grep -q $volume_name)
if [ $volume_exists ]; then
printf "***********************************\n"
printf "Error: volume $volume_name exists. \n"
printf "Please delete before proceeding \n"
printf "***********************************\n"
exit 1
fi

if [ -z "$snapshot" ]; then
snapshot=$(ls -At ${prefix}/${volume}/ | tail -n 1)
fi

if [ -z "$snapshot" ]; then
printf "**********************************\n"
printf "* Error: Unable to find snapshot.*\n"
printf "**********************************\n"
exit 1
else
echo "creating from snapshot: ${snapshot}"
fi

uuid=$(cat /proc/sys/kernel/random/uuid)
cwd=$(pwd)

IMAGE=alpine:latest
docker run \
--mount "type=volume,src=${volume_name},dst=/data" \
--name $uuid \
$IMAGE

mkdir /tmp/$uuid
tar -xf $prefix/$volume/$snapshot -C /tmp/$uuid
cd /tmp/$uuid

docker cp -a . $uuid:/data
docker rm $uuid
cd $cwd
```

create-volume-from-backup.sh (last update)

Execution Example

The animation below illustrates how the docker volume owncloud-docker_files is saved to /tmp/backups/owncloud-docker_files. The volume is saved as a zipped tar file with the date and time it was saved.

create-volume-backup.gif

The following animation shows how the docker volume is restored from /tmp/backups by specifying the snapshot file to restore fom. Note: If you wish to restore the docker volume with a different name than the backup, this can be done by adding a -n flag to the command and specifying the new volume name.

restore-docker-volume.gif

Created by www.spherex.dev

We hope this code is useful for you.

Leave a Reply

Your email address will not be published. Required fields are marked *