Rsync incremental backup using hard-links

There are probably dozens of ways to do an incremental backup using rsync (including using the rsync –link-dest option), but I decided to create a simple script that combines cp and rsync to do incremental backups from a remote server. You’ll need to change paths and credentials in the script.

sudo vi /usr/local/bin/mybackupscript
#!/bin/bash
 
# Rsync incremental backup
#/usr/local/bin/mybackupscript
#change credentials and paths in this file before executing it
 
BASEDIR="/path/to/backup/"
TODAY=$(date +"%d-%m-%Y")
YESTERDAY=$(date --date="yesterday" +"%d-%m-%Y")
cp -al $BASEDIR$YESTERDAY $BASEDIR$TODAY
rsync -aqz --delete --exclude-from '/path/to/exclude.list' -e ssh user@example.com:/path/to/directory/ $BASEDIR$TODAY

The trailing slash is important in /path/to/directory/

You need to

sudo chmod +x /usr/local/bin/mybackupscript

this file to execute it.

You can add it to your crontab to run it every so often. eg:

crontab -e
0 0 * * * /usr/local/bin/myback > /dev/null 2>&1

If you use a cron job, you need to pass credentials to rsync/use a key based login.