2021-03-04 14:21:35 +01:00
|
|
|
#!/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
|
2021-03-04 15:22:33 +01:00
|
|
|
RECURSIVE=$2
|
|
|
|
D=$3
|
|
|
|
W=$4
|
|
|
|
M=$5
|
|
|
|
Y=$6
|
2021-03-04 14:21:35 +01:00
|
|
|
|
2021-03-04 15:22:33 +01:00
|
|
|
PREFIX=$7
|
2021-03-04 14:21:35 +01:00
|
|
|
if test -z "$PREFIX"; then
|
|
|
|
PREFIX="zsc-"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test "!no_prefix!" = "$PREFIX"; then
|
|
|
|
PREFIX=""
|
|
|
|
fi
|
|
|
|
|
|
|
|
TARGET_REMOTE_DATASET="$REMOTE_ROOT_DATASET/"$(echo -e $DATASET | sed -r "s/\//_/g")
|
|
|
|
|
2021-03-04 15:22:33 +01:00
|
|
|
echo "[$DATASET -> $TARGET_REMOTE_DATASET]"
|
|
|
|
echo -e
|
2021-03-04 14:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
### 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[@]}"
|
2021-03-04 15:22:33 +01:00
|
|
|
echo -e
|
2021-03-04 14:21:35 +01:00
|
|
|
|
|
|
|
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
|
2021-03-04 15:22:33 +01:00
|
|
|
|
|
|
|
echo -e
|
2021-03-04 14:21:35 +01:00
|
|
|
done
|
|
|
|
|
|
|
|
echo "Snapshot are synchronized up to $FROM_SNAPSHOT"
|
2021-03-04 15:22:33 +01:00
|
|
|
echo -e
|
2021-03-04 14:21:35 +01:00
|
|
|
|
|
|
|
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
|
2021-03-04 15:22:33 +01:00
|
|
|
if [ $RECURSIVE = true ] ; then
|
2021-03-04 14:21:35 +01:00
|
|
|
recu="-R"
|
|
|
|
fi
|
2021-03-04 15:22:33 +01:00
|
|
|
zfs send -v -p $recu $incr "$DATASET@$TO_SNAPSHOT" | ssh $REMOTE zfs receive -F -s "$TARGET_REMOTE_DATASET" || return 1
|
2021-03-04 14:21:35 +01:00
|
|
|
|
|
|
|
echo "Done"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-03-04 15:22:33 +01:00
|
|
|
|
|
|
|
for line in $(cat "$ZSC_CONFIG") ; do
|
|
|
|
IFS=: read dataset recursive d w m y prefix <<< "$line"
|
2021-03-04 14:21:35 +01:00
|
|
|
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
|
|
|
|
|
2021-03-04 15:22:33 +01:00
|
|
|
handle_dataset $dataset $recursive $d $w $m $y $prefix | awk '{print "| " $0}' || exit 1
|
2021-03-04 14:21:35 +01:00
|
|
|
|
|
|
|
echo -e "----------------------------------------------------------------------"
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
|
|
### UNLOCK ###
|
|
|
|
echo "Unlocking ZSC"
|
|
|
|
lockfile-remove "zsc"
|