Last active
November 20, 2023 13:16
-
-
Save gpanders/e21e660102b7b15ae95ca161406d6603 to your computer and use it in GitHub Desktop.
Backup and restore Docker volumes to/from a compressed tar file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
usage() { | |
echo "Usage: $(basename "$0") [-v] volume_name backup_dir" | |
} | |
v="" | |
while getopts "hv" o; do | |
case "$o" in | |
h) usage; exit 0 ;; | |
v) v="v" ;; | |
*) usage >&2; exit 1 ;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ $# -ne 2 ]; then | |
usage >&2 | |
exit 1 | |
fi | |
VOLUME="$1" | |
ARCHIVE="$2/$VOLUME.tar.gz" | |
echo "Backing up $VOLUME to $ARCHIVE" | |
mkdir -p $(dirname "$ARCHIVE") | |
docker run --rm -v "$VOLUME":/from -v $(dirname "$ARCHIVE"):/to alpine tar cz${v}f /to/$(basename "$ARCHIVE") -C /from . | |
echo "Done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
usage() { | |
echo "Usage: $(basename "$0") [-v] volume_name archive" | |
} | |
v="" | |
while getopts "hv" o; do | |
case "$o" in | |
h) usage; exit 0 ;; | |
v) v="v" ;; | |
*) usage >&2; exit 1 ;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ $# -ne 2 ]; then | |
usage >&2 | |
exit 1 | |
fi | |
VOLUME="$1" | |
ARCHIVE="$2" | |
echo "Restoring $VOLUME from $ARCHIVE" | |
docker run --rm -v $(dirname "$ARCHIVE"):/from -v "$VOLUME":/to alpine tar xz${v}f /from/$(basename "$ARCHIVE") -C /to . | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's ok