Docker Backup External Volume

Backup docker volumn script:

Script 1: backup.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# This script allows you to backup a single volume from a container
# Data in given volume is saved in the current directory in a tar archive.
CONTAINER_NAME=$1
VOLUME_PATH=$2

usage() {
echo "Usage: $0 [container name] [volume path]"
exit 1
}

if [ -z $CONTAINER_NAME ]
then
echo "Error: missing container name parameter."
usage
fi

if [ -z $VOLUME_PATH ]
then
echo "Error: missing volume path parameter."
usage
fi

sudo docker run --rm --volumes-from $CONTAINER_NAME -v $(pwd):/backup busybox tar cvf /backup/backup.tar $VOLUME_PATH

Script 2: restore.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# This script allows you to restore a single volume from a container
# Data in restored in volume with same backupped path
NEW_CONTAINER_NAME=$1

usage() {
echo "Usage: $0 [container name]"
exit 1
}

if [ -z $NEW_CONTAINER_NAME ]
then
echo "Error: missing container name parameter."
usage
fi

sudo docker run --rm --volumes-from $NEW_CONTAINER_NAME -v $(pwd):/backup busybox tar xvf /backup/backup.tar

Reference: - https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes


Docker Backup External Volume
http://blog.chivier.site/2024-04-16/2024/Docker-Backup-External-Volume/
Author
Chivier Humber
Posted on
April 16, 2024
Licensed under