Skip to content

Instantly share code, notes, and snippets.

@pirafrank
Created January 31, 2021 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirafrank/e95c3e4eadd88756d96fa04cdfbf46f4 to your computer and use it in GitHub Desktop.
Save pirafrank/e95c3e4eadd88756d96fa04cdfbf46f4 to your computer and use it in GitHub Desktop.
scripts to dump docker volume to a tar.gz file and import from it
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: ./$0 tar_archive_name volume_name"
exit 1
fi
tararchive="$1"
volname="$2"
BASEDIR="$HOME/docker_vol_exports"
mkdir -p "$BASEDIR"
if [[ -f " $BASEDIR/$tararchive" ]]; then
echo 'removing old export'
rm -rf "$BASEDIR/$tararchive"
fi
echo 'creating new dir'
mkdir -p $BASEDIR
echo 'exporting data to tar archive'
src="$volname"
dest="$BASEDIR"
docker run --rm -it \
-v $src:/from \
-v $dest:/to \
alpine ash -c "cd /from ; tar -czf /to/$tararchive ."
echo "Volume $volname has been exported to $BASEDIR"
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: ./$0 tar_archive_name volume_name"
exit 1
fi
tararchive="$1"
volname="$2"
docker volume rm $volname
docker volume create $volname
src="$(pwd)"
dest="$volname"
docker run --rm -it \
-v $src:/from \
-v $dest:/to \
alpine ash -c "cd /from ; tar -xzf $tararchive -C /to/"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment