From 9fc2541b3c325fb78e006df0f17f6c3ad969822e Mon Sep 17 00:00:00 2001 From: Frieder Schlesier Date: Fri, 8 Jun 2018 19:57:21 +0200 Subject: [PATCH] first draft of docker volume management script --- bin/dvbackup | 26 ++++++++++++++++++++++++++ bin/dvrestore | 20 ++++++++++++++++++++ bin/move-dockervol | 3 +++ 3 files changed, 49 insertions(+) create mode 100644 bin/dvbackup create mode 100644 bin/dvrestore create mode 100644 bin/move-dockervol diff --git a/bin/dvbackup b/bin/dvbackup new file mode 100644 index 0000000..f47e1a1 --- /dev/null +++ b/bin/dvbackup @@ -0,0 +1,26 @@ +#!/bin/bash + +# https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes/26339869#26339869 +# 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_NAME=$2 + +usage() { + echo "Usage: $0 [container name] [volume name]" + exit 1 +} + +if [ -z $CONTAINER_NAME ] +then + echo "Error: missing container name parameter." + usage +fi + +if [ -z $VOLUME_NAME ] +then + echo "Error: missing volume name parameter." + usage +fi + +sudo docker run -rm --volumes-from $CONTAINER_NAME -v $(pwd):/backup busybox tar cvf /backup/$CONTAINER_NAME_$VOLUME_NAME.tar $VOLUME_NAME diff --git a/bin/dvrestore b/bin/dvrestore new file mode 100644 index 0000000..6940fc5 --- /dev/null +++ b/bin/dvrestore @@ -0,0 +1,20 @@ +#!/bin/bash + +# https://stackoverflow.com/questions/26331651/how-can-i-backup-a-docker-container-with-its-data-volumes/26339869#26339869 +# This script allows you to restore a single volume from a container +# Data in restored in volume with same backupped path +CONTAINER_NAME=$1 +VOLUME_NAME=$2 + +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 $CONTAINER_NAME -v $(pwd):/backup busybox tar xvf /backup/$CONTAINER_NAME_$VOLUME_NAME.tar diff --git a/bin/move-dockervol b/bin/move-dockervol new file mode 100644 index 0000000..df075d4 --- /dev/null +++ b/bin/move-dockervol @@ -0,0 +1,3 @@ +docker run --rm \ +-v :/from \ +alpine ash -c "cd /from ; tar -cf - . " | ssh 'docker run --rm -i -v :/to alpine ash -c "cd /to ; tar -xpvf - " '