A Complete Guide to Backing Up Your Raspberry Pi

Backing up your Raspberry Pi can be a straightforward process. Despite the abundance of advanced backup solutions, sometimes the most straightforward methods prove to be the most effective. In this guide, I will demonstrate how to utilize rsync – a remarkably efficient command-line tool ideal for generating dependable local backups of your Raspberry Pi environment.

The Advantages of Using Rsync for Raspberry Pi Backups

Rsync (short for remote sync) is a robust file syncing and transfer utility that is conveniently pre-installed on Raspberry Pi devices.

This tool excels in copying files from one directory to another, but it operates far more intelligently than a basic file transfer. Rsync transmits only the modified sections of files, which optimizes both time and system resources. This feature is particularly advantageous when backing up large files that are frequently updated.

Man Rsync Raspberry

I primarily employ rsync to safeguard crucial data on my Raspberry Pi. For example, I run a photo gallery server on my device, and rsync efficiently ensures a backup of those valuable images onto an external drive, automatically identifying and copying new or modified files.

Unlike other backup utilities that transfer entire files, rsync focuses solely on the modified segments, significantly speeding up the backup process and reducing wear on the SD card. Its command-line interface makes it suitable for automation with cron jobs, and its built-in checksumming guarantees the integrity of backups. Additionally, the lightweight design of rsync means that it won’t hinder your Pi’s performance unless a backup is in progress, nor does it consume excessive space on the SD card.

These features make rsync my preferred backup solution, and it can be an excellent option for many Raspberry Pi users seeking a reliable and efficient backup strategy.

Backing Up Files and Directories with Rsync on Raspberry Pi

The easiest method for backing up a directory is to use the following command:

For instance, to back up my home directory to an external drive located at “/media/backup” , I use this command:

This command securely saves my photo library database.

Rsync Raspberry Folder Backup

Here’s a breakdown of the command options:

  • -a activates archive mode, preserving file permissions, ownership, and timestamps.
  • -v enables verbose output, allowing me to monitor the copying progress.
  • --delete eliminates files from the backup that are no longer present in the source directory.

Ensure Your Raspberry Pi Backup is Successful

Before relying on your backup, it’s essential to confirm that it has completed successfully. The easiest way to do this is by performing a dry run with rsync, which can be indicated using either -n or --dry-run. Execute the following command:

If your backup is current, there should be no files indicated for transfer.

Rsync Raspberry Backup Dry Run

For a more comprehensive verification, you can include the -c or --checksum option in your rsync command. While this method is slower compared to the default timestamp and size checks, it ensures every file is identical between the source and backup by generating checksums. Simply run:

The most detailed verification method involves utilizing the diff command, which compares each file and directory between your source and backup locations. To do this, use:

The -r option allows diff to check all subdirectories recursively. If no output appears, it signifies that your backup matches the source. If differences are found, diff will specify which files vary or are missing.

Executing Comprehensive Raspberry Pi Backups with Rsync

In some cases, it might be more convenient and safer to perform a complete backup of your entire Raspberry Pi SD card rather than just a few essential folders.

To conduct full backups of your Raspberry Pi using rsync, you will require a backup target – either another SD card or an external drive with sufficient capacity to store your entire system. Ensure that the backup drive is formatted with a Linux-compatible filesystem such as ext4 to accurately maintain all file permissions and attributes.

The most challenging aspect of backing up a full Raspberry Pi setup is managing special system directories and files appropriately. Based on my experience, the best approach is to exclude system directories that either do not need to be backed up or could lead to complications if restored. You can create a file named “backup-exclude.txt” and include the following system directories as exclusions:

  • /proc/* # Runtime process information
  • /sys/* # Kernel and system details
  • /dev/* # Device files
  • /tmp/* # Temporary files
  • /run/* # Runtime data
  • /mnt/* # Mount points
  • /media/* # Removable media
  • /home/pi/.cache/* # User cache files
  • /lost+found # Filesystem recovery directory

With these exclusions set, you can close any unnecessary applications and execute the following backup command:

The -x option keeps rsync from navigating between different filesystem layers, which can lead to complications with system folders. The -h option ensures that rsync displays file sizes and transfer speeds in a more accessible format for users.

Scheduling Automated Rsync Backups

While manual full backups are valuable, I prefer to automate them through Linux’s built-in cron scheduling system. To set this up, create a script named “backup-pi.sh” in your home directory with the following content:

Make the script executable with chmod +x backup-pi.sh, then schedule it using cron by typing crontab -e and adding this line to run the backup daily at midnight:

Cron Raspberry Backup

How to Restore Your Rsync Raspberry Pi Backups

Restoring individual files or directories is a straightforward process. Simply switch the source and destination in your rsync command. For example, to restore your photo library from the backup:

For a complete system restoration, you will need a newly installed Raspberry Pi OS on your SD card. Begin by setting up a fresh installation of Raspberry Pi OS on your new SD card. After that, boot your Raspberry Pi with the new SD card and finish the initial setup. Connect your backup drive to the Pi, and once everything is properly mounted, you can restore your backup using rsync:

Once the restoration is complete, I recommend rebooting the Pi to ensure all restored files are correctly loaded. Additionally, verify that your essential services and configurations are functioning as anticipated.

If any applications do not operate correctly after restoration, check their log files (typically found in /var/log/) for permission-related issues. You may need to execute:

to resolve the ownership of your home directory files. Restoring data with rsync is just as simple as making backups – simply reverse the source and destination paths in your commands.

While I appreciate the straightforward command-line approach of rsync, there are numerous excellent GUI alternatives available for those who prefer not to use the terminal. For instance, Syncthing enables you to synchronize multiple folders across various systems, while Timeshift provides system snapshot capabilities with an user-friendly interface. Regardless of the tool you opt for, the key takeaway is to ensure you conduct regular backups of your Raspberry Pi to safeguard your critical data and system settings.

All images are courtesy of David Morelo.

© 2021 The Filibuster Blog