Skip to content

Instantly share code, notes, and snippets.

@gpanders
Last active November 20, 2023 13:16
Show Gist options
  • Save gpanders/e21e660102b7b15ae95ca161406d6603 to your computer and use it in GitHub Desktop.
Save gpanders/e21e660102b7b15ae95ca161406d6603 to your computer and use it in GitHub Desktop.
Backup and restore Docker volumes to/from a compressed tar file
#!/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."
#!/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."
@baxtheman
Copy link

it's ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment