Skip to content

Instantly share code, notes, and snippets.

@anvie
Last active October 7, 2021 08:33
Show Gist options
  • Save anvie/1673262de459f43dc78a7b5dd4ac1649 to your computer and use it in GitHub Desktop.
Save anvie/1673262de459f43dc78a7b5dd4ac1649 to your computer and use it in GitHub Desktop.
Bash script to snapshot nuchain db use hardlink & 7z
#!/usr/bin/env bash
SRCDIR=$1
DSTDIR=$2
show_usage(){
echo "Usage: nuchain-snapshot [SRCDIR] [DSTDIR]"
echo "Example: nuchain-snapshot /data/nuchain nuchain-snapshot"
}
do_snapshot(){
echo "Starting snapshot..."
rsync -avr --link-dest=$SRCDIR --exclude=keystore --exclude=network $SRCDIR/ $DSTDIR
7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on $DSTDIR-$(date +"%Y-%m-%d").7z $DSTDIR
echo "Done."
}
if [ -z "$SRCDIR" ] || [ -z "$DSTDIR" ]; then
show_usage
exit 0
fi
echo SRCDIR=$SRCDIR
echo DSTDIR=$DSTDIR
echo ""
[ ! -d "$SRCDIR" ] && echo "[ERROR] Directory SRCDIR=$SRCDIR not exists" && exit 1
[ -d "$DSTDIR" ] && echo "[ERROR] Directory DSTDIR=$DSTDIR already exists" && exit 2
if [ "$3" == "--yes" ]; then
echo "Skip confirmation"
do_snapshot
else
echo "Will execute this:"
echo '>' rsync -avr --link-dest=$SRCDIR --exclude=keystore --exclude=network $SRCDIR/ $DSTDIR
echo '>' 7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on $DSTDIR-$(date +"%Y-%m-%d").7z $DSTDIR
read -p "Are you sure? [y/n] " yn
if [[ "$yn" =~ "y" ]]; then
do_snapshot
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment