Incremental backups with rsync

I had some code lying around from another project and I minified it. I use this to automatically do incremental backups of my home assistant folder (or only the config one). I put it with 5 minute delay and run it with cron.

What this does:

  • first time, it makes a full copy of your folder in /backups/year_month_day_hour_minute_second
  • second time and afterwards, it makes hard links to the previous backup, then copies the changes over

Non-technical: It makes a backup of your files, copying the least amount of data possible. Each backup looks like it has all the files, but it only contains the difference. If you have a 100MB folder, and made 3 backups with 5 MB for each 2 changes, you would need:

  • 100 mb for first backup
  • ~5MB for second backup
  • ~5MB for third backup


original ideal:

How to use:

  • install php-cli

  • copy the .php file somewhere on your server

  • edit the first part of the file

    $C[‘timezone’ ] = ‘America/New_York’;
    $C[‘from’ ] = ‘/path/to/config/files’;
    $C[‘to’ ] = ‘/path/to/backups’;
    $C[‘keep’ ] = 1000;
    $C[‘rounding’ ] = 605;
    $C[‘rsync’ ] = ‘rsync’;
    $C[‘behind’ ] = 20160

  • set it up with cron every X minutes


  • to / from
  • timezone: see
  • keep - how many backups to keep
  • rounding - make a backup at most X seconds (in the above example: 5 minutes)
  • rsync executable path
  • behind - how many backups to look back for a good one for the diff

If you only want to use rsync, look at the original article, you only need to run this in the /backups/ folder:

rm -rf backup.3
mv backup.2 backup.3
mv backup.1 backup.2
mv backup.0 backup.1
rsync -a --delete --link-dest=../backup.1 source_directory/  backup.0/


  • source_directory = path to your config / home assistant folder

This will create a maximum of 4 backups (0 1 2 3) and every time you run the script it will delete the older one, move others one down, then create a new backup.

If you want to use it and don’t know how

  • create a folder for backups
  • edit a file with ssh ( nano )
  • put in the above commands and edit source_directory
  • save the files ( in nano control+x then y )
  • run it with bash

In cron add command
cd /folder/with/backups/ && bash

I’d also look at rsnapshot. It’s also a wrapper for rsync, and is designed for calling from cron.