I've revisited this one today. I think there is an approach which gives a week's worth of backups, while automatically allowing for missed days such as weekends or holidays. The date of the previous successful backup is recorded in the file system by touching the one directory and that date is later retrieved using
stat with
date via the UNIX epoch.
Code:
...
d1=$(date +"%u")
d2=$(date -d "@$(stat --format='%X' ./backup/)" +"%u")
set -e
rsync -avH --link-dest=./backup/${d2} -e '...' \
far@away:/source/ ./backup/${d1}/
touch ./backup/
...
If multiple runs happen on the same day, the --link-dest option is basically a non-operation. I'm not sure what
rsync does with it under the hood, but if it $d2 and $d1 are the same it is either ignored or pointing to the same and thus unchanged files. Either way, those files are not copied while any differences from the remote server are still processed.
The
set -e is just a quick hack, there are probably more appropriate ways using the $? variable to deal with tracking whether the backup was successful and the directory's date refreshed.
If a year's worth of backups are needed then %j can be used instead of %u in the date calculation. Or else use %d for a month's worth of backups.