Backup with rsync



Rsync is a program that allows for incremental backups. This means that rsync will not create an additional copy of the data when backing up, it will only backup changes to the files/directories, saving bandwidth and storage space.

Installation

sudo apt install rsync

Backup

rsync -azh $ORIGINAL $BACKUP

Replace $ORIGINAL with the file/directory to backup, and $BACKUP with the location for the backup to reside.

The $BACKUP destination must be a blank directory, an rsync directory, or not currently exist.

Remote rsync backup

If you need to rsync from one PC to another, it's essential the same command, but with the additional layer of ssh

rsync -azh -e ssh $ORIGINAL $BACKUP

$BACKUP here will be an ssh connection pointed to a location, much like when using scp, so the command will look like

rsync -azh -e ssh $ORIGINAL $USER@$HOST:$LOCATION

Replacing $USER and $HOST with the username and hostname/IP for the server

Restore

A restore in rsync doesn't require any rsync code per-se, as you can just copy individual files from the backup location to the restore location.

Alternatively to restore the entire directory, keeping files that haven't changes, and those that have to the time of the last backup, rsync can do that as below

rsync -auv $BACKUP $RESTORE

Over the internet

Like with backups, these restores can be done over the network/internet too

rsync -auv $USER@$HOST:$BACKUP $RESTORE

Notes/Advanced


-r recursive. All files/directories in the path will be backed up
-a archive mode. Recursive, but with file permissions, symlinks, etc retained.
-z compress
-b backups
-R relative
-u update - copy only changed files
-P progress
-c compress
-p preserve permissions
-h human readable. Make the output readible by humans

Downsides

Rsync only keeps one copy of the data, and doesn't keep the changes that were made, making it impossible* to restore a file's contents from the day previous. If this is what you're after, look at rdiff-backup.

* Not impossible, as you can set rsync to do this, but it requires a bit of scripting, and isn't as easy as just running the program