2020-10-27 15:15:22 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
DATASET=$1
|
|
|
|
if test -z "$DATASET"; then
|
|
|
|
echo "Missing dataset argument"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TARGET=$2
|
|
|
|
if test -z "$TARGET"; then
|
|
|
|
echo "Missing target"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TARGET_PATH=$3
|
|
|
|
if test -z "$TARGET_PATH"; then
|
|
|
|
echo "Missing target path"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
DATE=$4
|
|
|
|
if test -z "$DATE"; then
|
|
|
|
DATE=$(date +%Y-%m-%d_%H-%M)
|
|
|
|
fi
|
2020-10-27 15:15:22 +01:00
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
REMOTE_NAME="$TARGET_PATH/"$(echo -e $DATASET | sed -r "s/\//_/g")
|
2020-10-27 15:15:22 +01:00
|
|
|
|
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
### Resume existing transfer ###
|
|
|
|
RESUME_TOKEN=$(ssh $TARGET zfs get -H -p receive_resume_token -o value "$REMOTE_NAME")
|
|
|
|
if [ "$RESUME_TOKEN" != "" ] && [ "$RESUME_TOKEN" != "-" ]; then
|
|
|
|
echo "Resuming interrupted transfer..."
|
|
|
|
zfs send -v -t "$RESUME_TOKEN" | ssh $TARGET zfs receive -F -s "$REMOTE_NAME" || exit 1
|
|
|
|
echo "Resumed transfer finished."
|
|
|
|
exit 0
|
|
|
|
fi
|
2020-10-27 15:15:22 +01:00
|
|
|
|
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
### Sync existing snapshots ###
|
|
|
|
REMOTE_SNAPSHOTS=($(ssh $TARGET zfs list -H -t snapshot -o name -s creation $REMOTE_NAME | cut -d"@" -f 2))
|
|
|
|
LOCAL_SNAPSHOTS=($(zfs list -H -t snapshot -o name -s creation $DATASET | cut -d"@" -f 2))
|
2020-10-27 15:15:22 +01:00
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
echo "Remote snapshots: $REMOTE_SNAPSHOTS"
|
2020-10-27 15:15:22 +01:00
|
|
|
|
2020-11-13 11:06:19 +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
|
2021-03-04 14:21:35 +01:00
|
|
|
echo "> Found remotely, setting as last snapshot"
|
2020-11-13 11:06:19 +01:00
|
|
|
LAST_SNAPSHOT=$local_snapshot
|
|
|
|
EXISTING_SNAPSHOT=""
|
|
|
|
else
|
|
|
|
echo "> Needs sync"
|
|
|
|
EXISTING_SNAPSHOT=$local_snapshot
|
|
|
|
fi
|
|
|
|
done
|
2020-10-27 15:15:22 +01:00
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
|
|
|
|
### New snapshot ###
|
|
|
|
if [ "$EXISTING_SNAPSHOT" = "" ]; then
|
|
|
|
NAME=$DATE
|
2020-10-27 15:15:22 +01:00
|
|
|
else
|
2020-11-13 11:06:19 +01:00
|
|
|
NAME=$EXISTING_SNAPSHOT
|
2020-10-27 15:15:22 +01:00
|
|
|
fi
|
2020-11-13 11:06:19 +01:00
|
|
|
SNAPSHOT="$DATASET@$NAME"
|
|
|
|
echo "New snapshot name is: $NAME"
|
|
|
|
echo "New snapshot is: $SNAPSHOT"
|
2020-10-27 15:15:22 +01:00
|
|
|
|
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
### Last snapshot ###
|
2020-10-27 15:15:22 +01:00
|
|
|
echo "Last snapshot is: $LAST_SNAPSHOT"
|
|
|
|
|
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
## Perform transfer
|
|
|
|
echo "Starting backup with args $DATASET $NAME $LAST_SNAPSHOT"
|
|
|
|
$HOME/scripts/zfs_backup.sh $DATASET $NAME "$LAST_SNAPSHOT" $TARGET $TARGET_PATH || exit 1
|
2020-10-27 15:15:22 +01:00
|
|
|
|
2020-11-13 11:06:19 +01:00
|
|
|
|
|
|
|
## Destroy last snapshot
|
2020-10-27 15:15:22 +01:00
|
|
|
echo "Destroying last snapshot..."
|
2020-11-13 11:06:19 +01:00
|
|
|
zfs destroy "$DATASET@$LAST_SNAPSHOT" || exit 1
|
|
|
|
|
2020-10-27 15:15:22 +01:00
|
|
|
|
|
|
|
echo "Backup finished."
|