Linux bash remote backup script "Tower of Hanoi"

This is my first-choice backup script, based on the "Tower of Hanoi" game.
The benefith is a smart collection of full backups, that allows you to go back one day, 2 days, 4 days, 8 days 16 days, any power in between or any power of 2 higher.

Just choose the amount of full backups. 9 different "tapes" is already enough to go back more then one year in history.

Original script by Jeremdow. Modified to backup files from a remote server.
It is assumed that you added your SSH key to the remote host, to allow access without a password.

Project location: https://github.com/jobjol/Tower-of-Hanoi-remote-backup-script


#!/bin/bash
####################################
#
# Tower of Hanoi Backup script.
# hanoi_backup v2.3.1 jeremdow@gmail.com
# modified by jobjol jobjoling@gmail.com
#
# This script will archive specified files and folders on the remote host.
# If run as a daily cron job, archives are rotated on a Tower of Hanoi schedule.
# Archives from 1, 2, 4, 8, 16... $max days ago are retained depending on the setting.
#
####################################
# Configuration Settings
# Remote server access info.
user="remote_user"
server="37.187.92.174"
# Where to backup to.
dest="/home/jj/backup"
# Backup folder at remote server (dont append a slash)
files="/home/jj"
# Exclude pattern(s), see man rsync --exclude
exclude="**sites/default/files**"
# Oldest backup to keep (as a power of 2 in days)
max="10"
####################################
# Create archive filename.
date=$(date +%F)
file_archive="$server-$date";
# Print start status message.
echo $(date)
echo "Backing up locations: $server:$files to $dest/$file_archive"
echo
# Backup files. Full backup. No compression
rsync -az --update --delete $user@$server:$files $dest/$file_archive --exclude $exclude
# Print end status message.
echo "Backup $dest/$file_archive complete"
date
echo
# Calculate the previous move in the cycle
((day=$(date +%s)/86400))
for (( i=1; i<=$max/2; i=2*i ))
do
(( rotation=$day & i ))
if [ "$rotation" -eq "0" ]
then
(( expired=$i*2 ))
break
else
expired="4"
fi
done
# Remove the expired backups
expired_file=$server-$(date -d "$expired days ago" +%F)
echo "Attempting to remove expired backup: $expired_file"
if [ -d $dest/$expired_file ]
then
rm "$dest/$expired_file" -R
fi
# Finished
echo Complete!