Backing up your Raspberry Pi doesn’t need to be complicated. While there are many sophisticated backup solutions available, sometimes the simplest approach is the most effective. In this article, I’ll show you how to use rsync – a tiny but mighty command-line tool that’s perfect for creating reliable local backups of your Raspberry Pi setup.
Content
- Why Rsync Is The Best Tool For Backing Up Your Pi
- Using Rsync to Back Up Raspberry Pi Files and Folders
- Creating Full Raspberry Pi Backups Using Rsync
- Restoring Your Rsync Raspberry Pi Backups
Why Rsync Is The Best Tool For Backing Up Your Pi
Rsync (remote sync) is a powerful file synchronization and transfer tool that comes pre-installed on your Raspberry Pi.
At its core, rsync efficiently copies files from one location to another, but it’s much smarter than a simple copy operation. It only transfers the parts of files that have changed, saving both time and system resources. This is especially useful when backing up large files that change frequently.
I primarily use rsync to back up my important user data on the Pi. For instance, I maintain a photo gallery server on my Raspberry Pi, and rsync is perfect for keeping a backup copy of all those precious images on an external drive, with new or modified files automatically detected and copied.
Unlike tools backup that copy entire files, rsync only transfers changed portions, making backups significantly faster and reducing wear on your SD card. Its command-line interface makes it perfect for automation with cron jobs, while built-in checksumming ensures backup integrity. Best of all, rsync’s lightweight nature means it won’t slow down your Pi unless a backup process is currently running, nor will it take up too much SD card storage space.
For these reasons, rsync is the best solution for my needs, and I believe it’s an ideal choice for many other Raspberry Pi users who want a reliable, efficient backup system.
Using Rsync to Back Up Raspberry Pi Files and Folders
The simplest way to back up a directory is with the command:
rsync -av /source/directory /backup/location
For example, to back up my home directory to an external drive mounted at “/media/backup”, I use the command:
rsync -av --delete /home/david/immich-app /media/backup
to back up my photo library database.
Here’s what the different options do:
-a
enables archive mode, which preserves file permissions, ownership, and timestamps-v
provides verbose output so I can see what’s being copied.--delete
removes files from the backup location that no longer exist in the source.
Note: You should also know that rsync is particular about trailing slashes in paths. A trailing slash on the source means “copy the contents of this directory” rather than the directory itself. For instance, rsync -av --delete /home/pi/folder /media/backup
copies the contents of ‘folder’ directly into ‘backup’, while omitting the slash copies the ‘folder’ directory itself into ‘backup’.
Verify Your Raspberry Pi Backup
Before relying on your backup, you should verify that it worked correctly. The simplest way to check is using the dry run option with rsync, which can be specified as either -n
or --dry-run
. Running the command:
rsync -avn --delete /home/pi/folder /media/backup
should show no files needing to be transferred if your backup is up-to-date.
For a more thorough verification, you can add the -c
or --checksum
option to your rsync command. While slower than the default timestamp and size comparison, this guarantees that every file is completely identical between source and backup by computing checksums. Just run:
rsync -avc --delete /home/pi/folder /media/backup
The most detailed way to verify your backup is using the diff
command, which compares every file and directory between your source and backup locations. To perform this verification, use:
diff -r /home/pi/folder /media/backup/folder
The -r
option tells diff to check all subdirectories recursively. If the command produces no output, it means your backup is identical to the source. If there are differences, diff
will show you exactly which files are different or missing.
Creating Full Raspberry Pi Backups Using Rsync
In some situations, it may be more convenient and safer to back up your entire Raspberry Pi SD card instead of just a handful of important folders.
To create full Raspberry Pi backups using rsync, you’ll need a backup destination – either another SD card or an external drive with enough space to hold your entire system. The backup drive should be formatted with a Linux-compatible filesystem like ext4 to preserve all file permissions and attributes correctly.
The trickiest part of backing up a full Raspberry Pi system is handling special system directories and files correctly. In my experience, the easiest solution is to exclude system directories that either don’t need to be backed up or could cause problems if restored. Create a file called “backup-exclude.txt” and add the following system directories that should be excluded from the backup:
- /proc/* # Runtime process information
- /sys/* # Kernel and system information
- /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 in place, you can close as many running applications as you can and then execute the following backup command:
sudo rsync -avxhP --delete --exclude-from=/home/pi/backup-exclude.txt / /media/backup/rootfs/
The -x
option prevents rsync from crossing filesystem boundaries, which could cause issues with system directories. The -h
option makes rsync show file sizes and transfer speeds in a human-readable format.
Automating rsync backups
While manually created full backups are useful, I prefer to automate them using Linux’s built-in cron scheduler. To set this up, create a file called “backup-pi.sh” in your home directory with this content:
#!/bin/bash BACKUP_DRIVE="/media/backup" LOG_FILE="/home/pi/backup-log.txt" # Check if backup drive is mounted if ! mountpoint -q $BACKUP_DRIVE; then echo "Backup drive not mounted at $BACKUP_DRIVE" >> $LOG_FILE exit 1 fi # Create timestamp for logging date >> $LOG_FILE # Run backup with error checking sudo rsync -avxhP --delete \ --exclude-from=/home/pi/backup-exclude.txt \ / $BACKUP_DRIVE/rootfs/ \ >> $LOG_FILE 2>&1 # Record backup completion echo "Backup finished at $(date)" >> $LOG_FILE echo "-------------------" >> $LOG_FILE
Make the script executable with chmod +x backup-pi.sh
, then schedule it using cron. Type crontab -e
and add this line to run the backup daily at midnight:
0 0 * * * /home/pi/backup-pi.sh
Restoring Your Rsync Raspberry Pi Backups
For restoring individual files or directories, the process is straightforward. Simply swap the source and destination in your rsync command. For example, to restore your photo library from backup:
rsync -av /media/backup/home/david/immich-app/ /home/david/immich-app/
For a full system restoration, you’ll need a fresh installation of Raspberry Pi OS on your SD card first. Start by installing a fresh copy of Raspberry Pi OS on your new SD card. Once that’s done, boot your Pi with the new SD card and complete the initial setup process. Next, connect your backup drive to your Pi. When everything is connected and mounted properly, you can restore your backup using the rsync command:
sudo rsync -avxhP /media/backup/rootfs/ /
After restoration is complete, I recommend rebooting your Pi to check if all restored files are properly loaded. You should also verify that your important services and configurations are working as expected.
If certain applications aren’t working after restoration, check their log files (usually in /var/log/) for any permission-related errors. You may need to run:
sudo chown -R $USER:$USER /home/$USER
to fix ownership of your home directory files. As you can see, restoring with rsync is as easy as creating backups – just reverse the source and destination paths in your commands.
While I prefer rsync’s command-line simplicity, there are several excellent GUI alternatives if you’re not comfortable with the terminal. For example, Syncthing allows you to synchronize multiple folders across different systems, while Timeshift offers system snapshot functionality with a clean interface. Whichever tool you choose, the important thing is to maintain regular backups of your Raspberry Pi to protect your valuable data and system configurations.
All images by David Morelo.
David Morelo –
Staff Writer
David Morelo is a professional content writer in the technology niche, covering everything from consumer products to emerging technologies and their cross-industry application. His interest in technology started at an early age and has only grown stronger over the years.
Subscribe to our newsletter!
Our latest tutorials delivered straight to your inbox
Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe