134 lines
3.1 KiB
Bash
Executable File
134 lines
3.1 KiB
Bash
Executable File
#!/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
|
|
RECURSIVE=$2
|
|
D=$3
|
|
W=$4
|
|
M=$5
|
|
Y=$6
|
|
|
|
PREFIX=$7
|
|
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")
|
|
|
|
echo "[$DATASET -> $TARGET_REMOTE_DATASET]"
|
|
echo -e
|
|
|
|
|
|
### 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[@]}"
|
|
echo -e
|
|
|
|
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
|
|
|
|
echo -e
|
|
done
|
|
|
|
echo "Snapshot are synchronized up to $FROM_SNAPSHOT"
|
|
echo -e
|
|
|
|
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 [ $RECURSIVE = true ] ; then
|
|
recu="-R"
|
|
fi
|
|
zfs send -v -p $recu $incr "$DATASET@$TO_SNAPSHOT" | ssh $REMOTE zfs receive -F -s "$TARGET_REMOTE_DATASET" || return 1
|
|
|
|
echo "Done"
|
|
fi
|
|
}
|
|
|
|
|
|
for line in $(cat "$ZSC_CONFIG") ; do
|
|
IFS=: read dataset recursive d w m y prefix <<< "$line"
|
|
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 $recursive $d $w $m $y $prefix | awk '{print "| " $0}' || exit 1
|
|
|
|
echo -e "----------------------------------------------------------------------"
|
|
done
|
|
|
|
|
|
### UNLOCK ###
|
|
echo "Unlocking ZSC"
|
|
lockfile-remove "zsc"
|