179 lines
4.3 KiB
Bash
Executable File
179 lines
4.3 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
|
|
|
|
|
|
### Resume existing transfer ###
|
|
# Returns 0 if no action was performed, 1 if a transfer was resumed and finished successfully, -1 if there was an error during resumed transfer
|
|
resume_transfer() {
|
|
remote_dataset=$1
|
|
|
|
RESUME_TOKEN=$(ssh $REMOTE zfs get -H -p receive_resume_token -o value "$remote_dataset")
|
|
if [ "$RESUME_TOKEN" != "" ] && [ "$RESUME_TOKEN" != "-" ]; then
|
|
echo "Resuming interrupted transfer..."
|
|
zfs send -v -t "$RESUME_TOKEN" | ssh $REMOTE zfs receive -F -s "$remote_dataset" || return -1
|
|
echo "Resumed transfer finished."
|
|
return 1
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
|
|
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_transfer "$TARGET_REMOTE_DATASET" || return 0
|
|
|
|
|
|
### 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
|
|
}
|
|
|
|
init_subdataset() {
|
|
DATASET=$1
|
|
PARENT_DATASET=$2
|
|
|
|
TARGET_REMOTE_DATASET="$REMOTE_ROOT_DATASET/"$(echo -e $PARENT_DATASET | sed -r "s/\//_/g")"/"${DATASET:$(echo -e "$PARENT_DATASET" | wc -c)}
|
|
REMOTE_DATASETS=($(ssh $REMOTE zfs list -H -o name -s creation -r $REMOTE_ROOT_DATASET))
|
|
|
|
|
|
resume_transfer "$TARGET_REMOTE_DATASET" || return 0
|
|
|
|
|
|
found=false
|
|
for d in "${REMOTE_DATASETS[@]}"; do
|
|
if [ "$TARGET_REMOTE_DATASET" == "$d" ]; then
|
|
found=true
|
|
break
|
|
fi
|
|
done
|
|
|
|
if [ $found = false ]; then
|
|
echo "Subdataset not found on $TARGET_REMOTE_DATASET, initializing..."
|
|
|
|
LOCAL_SNAPSHOT=($(zfs list -H -t snapshot -o name -s creation "$DATASET" | cut -d"@" -f 2 | tail -n 1))
|
|
|
|
if test -z "$LOCAL_SNAPSHOT"; then
|
|
echo "No local snapshot"
|
|
return 1
|
|
fi
|
|
|
|
echo "[$DATASET@$LOCAL_SNAPSHOT -> $TARGET_REMOTE_DATASET]"
|
|
zfs send -v -p "$DATASET@$LOCAL_SNAPSHOT" | ssh $REMOTE zfs receive -F -s "$TARGET_REMOTE_DATASET" || return 1
|
|
|
|
echo -e "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
|
|
init_subdataset $p $dataset | awk '{print "| " $0}' || exit 1
|
|
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"
|