Backup a Raspberry Pi with Rsync
I am going to upgrade our previous Raspberry Pi backup script that previously used Secure Copy to one that uses rsync. The primary reason for this is that rsync will only copy new or changed files over. This post goes through the reasons why you might want to use rsync instead of scp when performing a backup.
Why do we want to use rsync over scp?
In our original backup script we used scp
to copy files across the network. This works well when you are copying single files but doesn’t work well for our use case. We want to use something that when the file already exists it will see if it has changed, then only copy it if it has.
rsync
is a Linux tool that can be used for synchronising file across two folders (which can be on different computers). The key feature that we are interested in is that it can check the timestamp, file size and contents to determine if it needs to copy the file or not. By checking to see if the file has changed it is able to copy only the files that have. Since we are going to be making our backups incrementally we would expect only a small number of files to change.
The downside of using rsync
is that it needs to determine if the files are different and will use a bit more processing power then scp
. However typically copying files across a network is the primary slowdown for backing up systems. This means that for our use case rsync is ideally suited.
Changes to our script to use rsync
The first thing we need to do improving our script is to remove the first line that deletes the home and var directory. This is because we are going to use this to speed up our transfers.
Once we have removed the line deleting the directories we need to change the scp command to a rsync command. My new backup commands are
rsync -rthvz --progress --delete bunker-master2:/home . rsync -rthvz --progress --delete bunker-master2:/var/www var/
Here I call rsync with a number of flags to backup my Raspberry Pi. The full details of the flags are below:
- -r Recursively copy directories
- -t Preserve the file modification times
- -h Output all numbers in a human readable format
- -v Verbose mode to increase the information during a copy
- -z Compress the data during the transfer
- –progress Show progress of the current transfers
- –delete Delete files in your backup directory that are not in the directory you are copying from. This ensures you don’t keep building up deleted files
Using rsync with these flags will copy over the files from the Raspberry Pi. In addition, this will only copy the new or changed files which means the speed of copy is much faster.
I haven’t changed the final zipping part of the script as once the copy has finished with rsync I still want it to create a folder archive.
Now we have an archiving script that intelligently copies files over from the Raspberry Pi and archives them.