Separate snapshot creation from snapshot sync

This commit is contained in:
Alice Gaudon 2021-03-04 14:21:35 +01:00
parent 471ab52a9d
commit b2069ce598
3 changed files with 158 additions and 14 deletions

View File

@ -53,7 +53,7 @@ for local_snapshot in "${LOCAL_SNAPSHOTS[@]}"; do
done
if [ $found = true ] ; then
echo "> Found remotely, settings as last snapshot"
echo "> Found remotely, setting as last snapshot"
LAST_SNAPSHOT=$local_snapshot
EXISTING_SNAPSHOT=""
else

130
zsc-sync.sh Executable file
View File

@ -0,0 +1,130 @@
#!/bin/bash
# ZSC-Sync - zfs snapshot cron sync
# Usage: <script> <remote> <remote_dataset>
ZSC_CONFIG="$HOME/.zsc"
REMOTE=$1
if test -z "$REMOTE"; then
echo "No remote specified"
exit -1
fi
REMOTE_ROOT_DATASET=$2
if test -z "$REMOTE_ROOT_DATASET"; then
echo "No remote root dataset specified"
exit -1
fi
### LOCK ###
echo "Locking ZSC"
if ! lockfile-create -p -r 2 "zsc"; then
echo "Another backup is running, aborting"
exit 49
fi
handle_dataset() {
DATASET=$1
D=$2
W=$3
M=$4
Y=$5
PREFIX=$6
if test -z "$PREFIX"; then
PREFIX="zsc-"
fi
if test "!no_prefix!" = "$PREFIX"; then
PREFIX=""
fi
RECURSIVE=$7
if [ $recusive = 'true' ]; then
echo "Recursive"
fi
TARGET_REMOTE_DATASET="$REMOTE_ROOT_DATASET/"$(echo -e $DATASET | sed -r "s/\//_/g")
echo "--- $DATASET -> $TARGET_REMOTE_DATASET ---"
### Resume existing transfer ###
RESUME_TOKEN=$(ssh $REMOTE zfs get -H -p receive_resume_token -o value "$TARGET_REMOTE_DATASET")
if [ "$RESUME_TOKEN" != "" ] && [ "$RESUME_TOKEN" != "-" ]; then
echo "Resuming interrupted transfer..."
zfs send -v -t "$RESUME_TOKEN" | ssh $REMOTE zfs receive -F -s "$TARGET_REMOTE_DATASET" || return 1
echo "Resumed transfer finished."
return 0
fi
### Sync existing snapshots ###
REMOTE_SNAPSHOTS=($(ssh $REMOTE zfs list -H -t snapshot -o name -s creation $TARGET_REMOTE_DATASET | cut -d"@" -f 2))
LOCAL_SNAPSHOTS=($(zfs list -H -t snapshot -o name -s creation $DATASET | cut -d"@" -f 2))
echo "Remote snapshots: ${#REMOTE_SNAPSHOTS[@]}"
echo "Local snapshots: ${#LOCAL_SNAPSHOTS[@]}"
for local_snapshot in "${LOCAL_SNAPSHOTS[@]}"; do
echo "Syncing local snapshot $local_snapshot ..."
found=false
for remote_snapshot in "${REMOTE_SNAPSHOTS[@]}"; do
if [ "$remote_snapshot" = "$local_snapshot" ] ; then
found=true
break
fi
done
if [ $found = true ] ; then
echo "> Found remotely, setting as last snapshot"
FROM_SNAPSHOT=$local_snapshot
TO_SNAPSHOT=""
else
echo "> Needs sync"
TO_SNAPSHOT=$local_snapshot
fi
done
echo "Snapshot are synchronized up to $FROM_SNAPSHOT"
if test ! -z "$TO_SNAPSHOT"; then
echo "Synchronizing snapshots from $FROM_SNAPSHOT to $TO_SNAPSHOT ..."
if test ! -z "$FROM_SNAPSHOT"; then
incr="-I $FROM_SNAPSHOT"
fi
if [ $recusive = 'true' ]; then
recu="-R"
fi
zfs send -v -p $recu $incr $TO_SNAPSHOT | ssh $REMOTE zfs receive -F -s $TARGET_REMOTE_DATASET || return 1
echo "Done"
fi
}
cat "$ZSC_CONFIG" | while IFS=: read dataset recursive d w m y prefix; do
echo "| ##### Dataset: $dataset Recursive: $recursive Prefix: $prefix d: $d w: $w m: $m y: $y #####"
if [ $recursive = "true" ]; then
SUBDATASETS=($(zfs list -o name | grep "$dataset/"))
echo -e "| Subdatasets: ${SUBDATASETS[@]}"
echo -e '|'
#for p in "${SUBDATASETS[@]}"; do
# handle_dataset $p $d $w $m $y $prefix | awk '{print "| " $0}' || exit 1
# echo -e '|'
#done
fi
handle_dataset $dataset $d $w $m $y $prefix $recursive | awk '{print "| " $0}' || exit 1
echo -e "----------------------------------------------------------------------"
done
### UNLOCK ###
echo "Unlocking ZSC"
lockfile-remove "zsc"

40
zsc.sh
View File

@ -7,12 +7,21 @@ CONFIG_FILE="$HOME/.zsc"
FORMAT='+%Y-%m-%d'
DATE=$(date $FORMAT)
### LOCK ###
echo "Locking ZSC"
if ! lockfile-create -p -r 2 "zsc"; then
echo "Another backup is running, aborting"
exit 49
fi
split_date() {
echo "$1" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\1\n\2\n\3/'
}
handle_pool() {
POOL=$1
handle_dataset() {
DATASET=$1
D=$2
W=$3
M=$4
@ -27,9 +36,9 @@ handle_pool() {
PREFIX=""
fi
echo "--- $POOL --- $D $W $M $Y $PREFIX"
echo "--- $DATASET --- $D $W $M $Y $PREFIX"
SNAPSHOTS=($(zfs list -t snapshot -o name | grep "$POOL@$PREFIX" | sort -r))
SNAPSHOTS=($(zfs list -t snapshot -o name | grep "$DATASET@$PREFIX" | sort -r))
echo "Snapshots: ${#SNAPSHOTS[@]}"
EXISTS=0
@ -66,25 +75,30 @@ handle_pool() {
done
if [ $EXISTS -eq 0 ]; then
echo -e "Creating snapshot $POOL@$PREFIX$DATE"
zfs snapshot $POOL@$PREFIX$DATE
echo -e "Creating snapshot $DATASET@$PREFIX$DATE"
zfs snapshot $DATASET@$PREFIX$DATE
fi
}
cat "$CONFIG_FILE" | while IFS=: read pool recursive d w m y prefix; do
echo "| ##### Pool: $pool Recursive: $recursive Prefix: $prefix d: $d w: $w m: $m y: $y #####"
cat "$CONFIG_FILE" | while IFS=: read dataset recursive d w m y prefix; do
echo "| ##### Dataset: $dataset Recursive: $recursive Prefix: $prefix d: $d w: $w m: $m y: $y #####"
if [ $recursive = "true" ]; then
SUBPOOLS=($(zfs list -o name | grep "$pool/"))
echo -e "Subpools: ${SUBPOOLS[@]}"
SUBDATASETS=($(zfs list -o name | grep "$dataset/"))
echo -e "| Subdatasets: ${SUBDATASETS[@]}"
echo -e '|'
for p in "${SUBPOOLS[@]}"; do
handle_pool $p $d $w $m $y $prefix | awk '{print "| " $0}'
for p in "${SUBDATASETS[@]}"; do
handle_dataset $p $d $w $m $y $prefix | awk '{print "| " $0}'
echo -e '|'
done
fi
handle_pool $pool $d $w $m $y $prefix | awk '{print "| " $0}'
handle_dataset $dataset $d $w $m $y $prefix | awk '{print "| " $0}'
echo -e "----------------------------------------------------------------------"
done
### UNLOCK ###
echo "Unlocking ZSC"
lockfile-remove "zsc"