Skip to content

Instantly share code, notes, and snippets.

@poeli
Last active April 4, 2018 16:04
Show Gist options
  • Save poeli/adaa11cab1a7b99c2a19da110a30b4b1 to your computer and use it in GitHub Desktop.
Save poeli/adaa11cab1a7b99c2a19da110a30b4b1 to your computer and use it in GitHub Desktop.
Backup a single volume from a container to a tar archive
#!/bin/bash
# Backup a single volume from a Docker container to a tar archive.
CONTAINER_NAME=$1
VOLUME_NAME=$2
BACKUPTAR=$3
usage() {
echo "Usage: $0 [container name] [volume name] ([backup.tar])"
exit 1
}
if [ -z $BACKUPTAR ]
then
BACKUPTAR="backup.tar"
fi
TARDIR=$(dirname "${BACKUPTAR}")
TARFN=$(basename "${BACKUPTAR}")
if [[ $TARDIR != /* ]]
then
TARDIR="$(pwd)/$TARDIR"
fi
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
docker run --rm --volumes-from $CONTAINER_NAME -v $TARDIR:/backup busybox tar cvf /backup/$TARFN $VOLUME_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment