Last active
May 3, 2023 09:18
-
-
Save hellvesper/36f7196209368126d08c25a466d73f8e to your computer and use it in GitHub Desktop.
Replication TrueNAS's apple time machnine snapshots with hetzner's cloud storage box [draft]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
LAST_LOCAL_SNAPSHOT=`zfs list -t snapshot | sort -r | egrep '^.+aapltm-[0-9]+' -o -m 1` | |
LAST_REMOTE_SNAPSHOT=`rclone ls storagebox:/home/ | sort -k 2 -r | grep aapltm -m 1 | xargs | cut -d ' ' -f 2 | cut -d '.' -f 1` | |
DATASET_PREFIX=`zfs list -t snapshot | grep aapltm -m 1 | cut -d ' ' -f 1 | cut -d '@' -f 1` | |
LOCAL_SNAPSHOT_POSTFIX=`echo $LAST_LOCAL_SNAPSHOT | cut -d '@' -f 2` | |
RCLONE_REMOTE="storagebox" | |
RCLONE_REMOTE_SNAPSHOT_PATH="/home/" | |
echo "LAST_LOCAL_SNAPSHOT:$LAST_LOCAL_SNAPSHOT" | |
echo "LAST_REMOTE_SNAPSHOT:$LAST_REMOTE_SNAPSHOT" | |
echo "DATASET_PREFIX:$DATASET_PREFIX" | |
echo "LOCAL_SNAPSHOT_POSTFIX:$LOCAL_SNAPSHOT_POSTFIX" | |
echo "RCLONE_REMOTE:$RCLONE_REMOTE" | |
echo "RCLONE_REMOTE_SNAPSHOT_PATH:$RCLONE_REMOTE_SNAPSHOT_PATH" | |
#check if TM snapshot exist | |
if [ -z "$LAST_LOCAL_SNAPSHOT" ] #test -z <var> => if string has zero lenght | |
then | |
echo "Local snapshot not found, is it exist?" | |
exit 1 | |
fi | |
if [ -z "$LAST_REMOTE_SNAPSHOT" ] | |
then | |
echo "Remote snapshot not found: $LAST_REMOTE_SNAPSHOT" | |
fi | |
if [ "$LAST_LOCAL_SNAPSHOT" = "$DATASET_PREFIX@$LAST_REMOTE_SNAPSHOT" ] | |
then | |
echo "Local and remote sanphots are equal, backup doesn't needed." | |
exit 0 | |
else | |
echo "Local and remote sanphots are NOT equal: $LAST_LOCAL_SNAPSHOT" = "$DATASET_PREFIX@$LAST_REMOTE_SNAPSHOT" | |
fi | |
IS_LOCAL_SNAPSHOT_STILL_EXISTS=`zfs list -t snapshot | grep $DATASET_PREFIX"@"$LAST_REMOTE_SNAPSHOT` | |
if [ -z "$LAST_REMOTE_SNAPSHOT" ] || [ -z "$IS_LOCAL_SNAPSHOT_STILL_EXISTS"] | |
then | |
echo "Remote snapshot not found or local copy doesn't exist, prepare for initial backup upload." | |
UPLOAD_SIZE=`zfs send -nPc $LAST_LOCAL_SNAPSHOT | grep size | cut -d ' ' -f 2` | |
echo "Uploading " `expr $UPLOAD_SIZE / 1024 / 1024 / 1024` " GiB" | |
echo "Calculating sha1sum for integrity check, this will take a wile" | |
LOCAL_CHECKSUM=`sudo zfs send -c $LAST_LOCAL_SNAPSHOT| pv -s $UPLOAD_SIZE -w 80 | sha1` | |
echo "Local snapshot sha1 sum: $LOCAL_CHECKSUM" | |
sudo zfs send -c $LAST_LOCAL_SNAPSHOT | rclone -P rcat $RCLONE_REMOTE":"$RCLONE_REMOTE_SNAPSHOT_PATH$LOCAL_SNAPSHOT_POSTFIX".snap" --size $UPLOAD_SIZE | |
echo "Upload done, prepearing integrity check" | |
echo "Calculetaing remote sha1 sum" | |
REMOTE_CHECKSUM=`rclone sha1sum $RCLONE_REMOTE":"$RCLONE_REMOTE_SNAPSHOT_PATH$LOCAL_SNAPSHOT_POSTFIX".snap" | cut -d ' ' -f 1` | |
if [ "$LOCAL_CHECKSUM" = "$REMOTE_CHECKSUM" ] | |
then | |
echo $LOCAL_CHECKSUM "=" $REMOTE_CHECKSUM "ok" | |
return 0 | |
else | |
echo $LOCAL_CHECKSUM "!=" $REMOTE_CHECKSUM "checksum not equal!" | |
return 1 | |
fi | |
else | |
echo "Last remote snapshot found:" $LAST_REMOTE_SNAPSHOT "prepare for incremental upload" | |
UPLOAD_SIZE=`zfs send -nPcI $DATASET_PREFIX"@"$LAST_REMOTE_SNAPSHOT $LAST_LOCAL_SNAPSHOT | grep size | cut -d ' ' -f 2` | |
echo "Uploading " `expr $UPLOAD_SIZE / 1024 / 1024 / 1024` " GiB" | |
echo "Calculating local sha1 sum for integrity check, this will take a wile" | |
LOCAL_CHECKSUM=`sudo zfs send -cI $DATASET_PREFIX"@"$LAST_REMOTE_SNAPSHOT $LAST_LOCAL_SNAPSHOT | pv -s $UPLOAD_SIZE -w 80 | sha1` | |
echo "Local snapshot sha1 sum: $LOCAL_CHECKSUM" | |
echo "Uploading snapshot:" | |
sudo zfs send -cI $DATASET_PREFIX"@"$LAST_REMOTE_SNAPSHOT $LAST_LOCAL_SNAPSHOT | rclone -P rcat $RCLONE_REMOTE":"$RCLONE_REMOTE_SNAPSHOT_PATH$LOCAL_SNAPSHOT_POSTFIX".snap" --size $UPLOAD_SIZE | |
echo "Upload done, prepearing integrity check" | |
echo "Calculetaing remote sha1 sum" | |
REMOTE_CHECKSUM=`rclone sha1sum $RCLONE_REMOTE":"$RCLONE_REMOTE_SNAPSHOT_PATH$LOCAL_SNAPSHOT_POSTFIX".snap" | cut -d ' ' -f 1` | |
if [ "$LOCAL_CHECKSUM" = "$REMOTE_CHECKSUM" ] | |
then | |
echo $LOCAL_CHECKSUM "=" $REMOTE_CHECKSUM "ok" | |
return 0 | |
else | |
echo $LOCAL_CHECKSUM "!=" $REMOTE_CHECKSUM "checksum not equal!" | |
return 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples of work, first snapshot is not incremental:
Incremental backup: