Many thanks to Rich, Miles, Vitor and Laurence for their hints on this subject.
Especially the suggestion to use BTRFS Snapshots was very helpful.

Finally I have come up with the following shell script for backing up my /home directory,
comments are more than welcome, of course.
(/HBackUp contains a BTRFS file system)


#!/bin/zsh
mount /HBackUp
pushd /HBackUp
NUM_TO_KEEP=5
if [ -d "ohome_$NUM_TO_KEEP" ]
then
    btrfs subvolume delete "ohome_$NUM_TO_KEEP"
fi

if ! [ -f _RSync_Failed ]
then
    for backup_no in $(seq $((NUM_TO_KEEP-1)) -1 1)
    do
        if [ -d ohome_$backup_no ]
        then
            backup_no_plus_1="$((backup_no+1))"
            mv $ohome_prev "ohome_$backup_no_plus_1"
        fi
    done
    if ! btrfs subvolume snapshot ohome ohome_1
    then
        echo Sorry, creating snapshot ohome_1 failed 1>&2
        exit 1
    fi
fi

RSync_OK=False
touch _RSync_Failed

popd
pushd /home

if rsync -aqxAHSW --delete  . /HBackUp/ohome/
then
  RSync_OK=True
else
    case "$?" in
        23)
# This means some files gave permission denied - assume that's OK
            RSync_OK=True
            ;;
        24)
            # This means one or more files disappeared
            RSync_OK=True
            ;;
        *)
            echo rsync failed. Exit code was $?"
            ;;
    esac
fi

popd

cd /HBackUp

case "$RSync_OK" in
    True)
        rm _RSync_Failed
        ;;
    False)
        cd /
       umount  /HBackUp
        exit 1
        ;;
esac

cd /
umount /HBackUp

Reply via email to