keepcalculator

06 Numbers and Powers

Raspberry Pi Automatic Backup: How To

Card No. 06·87159 · Updated · Standard formula, shown below

Yes, you can turn a Raspberry Pi into an automatic backup server, and it is one of the most practical projects for the tiny computer. You connect external drives to the Pi, install backup software, and set a schedule so your files are copied without any daily effort. This guide covers the hardware, software, setup steps, and common pitfalls so you can build a reliable backup system.

Why Use a Raspberry Pi for Backups

A Raspberry Pi is a low-power, always-on mini computer that costs little to run. Unlike a desktop PC that you might shut down at night, a Pi can stay on 24/7, which makes it ideal for scheduled backups. It also uses an SD card or USB drive for its operating system, so you can dedicate large external drives purely to storing backups. The entire setup is cheap compared to a dedicated NAS (network-attached storage) box, and it teaches you the basics of Linux and networking along the way.

The most common approach is to turn the Pi into a NAS or a backup target using software like Samba (for Windows file sharing), NFS (for Linux), or a dedicated backup tool like rsync, BorgBackup, or Restic. You can also run a Time Machine server for macOS using Netatalk. The Pi itself does not need to be powerful; even a Raspberry Pi 3 or 4 can handle multiple backup jobs for a home network.

What You Need: Hardware and Software

Before you start, gather the following components. Most are standard Raspberry Pi accessories, and you might already own them.

  • Raspberry Pi board – any model works, but a Pi 4 or 5 with at least 2GB RAM is recommended for speed.
  • MicroSD card – at least 16GB, Class 10, for the operating system.
  • Power supply – official USB-C or micro-USB supply rated for your model.
  • External storage – one or more USB hard drives or SSDs. The backup drive should be as large as the total data you want to back up.
  • Network connection – Ethernet cable is more reliable than Wi-Fi for large transfers.
  • Optional – a USB hub with external power if you connect multiple drives.

For software, you need the Raspberry Pi OS (formerly Raspbian) flashed to the SD card. You can download it from the official Raspberry Pi website and write it with the Raspberry Pi Imager tool. After booting, you will install backup software via the terminal.

Setting Up the Raspberry Pi

Follow these steps to prepare your Pi for backup duty. The commands assume you are using Raspberry Pi OS with a desktop or the Lite version (no desktop) – both work.

  1. Flash Raspberry Pi OS to the SD card using the official Imager. Enable SSH in the Imager settings if you want headless access.
  2. Boot the Pi, connect to your network, and find its IP address (e.g., with hostname -I).
  3. Run sudo apt update && sudo apt upgrade to update the system.
  4. Connect your external backup drive via USB. Check it is detected with lsblk.
  5. Format the drive if it is new – for example, use sudo mkfs.ext4 /dev/sda1 (replace with your device name). Or use a filesystem that Windows can read, like NTFS or exFAT, if you need to access the backups from a PC.
  6. Create a mount point and mount the drive automatically at boot. Add a line to /etc/fstab using the drive’s UUID (find it with blkid).
  7. Install backup software – rsync is usually preinstalled; for BorgBackup, run sudo apt install borgbackup.

Once the drive is mounted and visible, you can start creating backup jobs.

How to Schedule Automatic Backups

The core of automation is the cron scheduler, which runs commands at set times. You edit your user’s crontab with crontab -e and add a line like:

0 2 * * * /home/pi/backup.sh

This runs the script every day at 2:00 AM. The script itself contains the backup command. For example, a simple rsync backup from your main computer’s shared folder to the Pi’s drive might look like:

rsync -av --delete /mnt/source/ /mnt/backup/daily/

Replace the source and destination paths with your own. The --delete flag makes the backup mirror the source, removing files that were deleted on the source. For versioned backups, use --backup --backup-dir to keep old copies.

If you want to back up multiple computers over the network, you can use rsync with SSH. On the Pi, enable SSH, then from each client computer run a command like:

rsync -av /home/user/documents/ pi@raspberrypi:/mnt/backup/laptop/

You can also set up SSH keys so the backup runs without a password prompt, which is required for cron jobs.

Choosing the Right Backup Software

Rsync is the simplest and most common tool, but it only copies files – it does not compress or deduplicate. For more advanced features, consider these options:

  • BorgBackup – deduplicates data, compresses archives, and supports encryption. Great for backing up many versions with minimal disk space.
  • Restic – similar to Borg, but with a focus on easy cloud storage integration. Works well with local drives too.
  • Syncthing – not a backup tool per se, but it continuously syncs folders between devices. Good for keeping files in sync, but not for versioning.
  • Duplicity – uses rsync-style incremental backups with encryption. Good for offsite backups.

For most home users, rsync is enough. If you want a more “set and forget” solution with versioning, BorgBackup is a strong choice. You can also use a NAS-oriented OS like OpenMediaVault, which provides a web interface and built-in backup plugins, but that is a heavier setup.

Common Problems and How to Fix Them

Even a simple backup setup can hit snags. Here are the most frequent issues and their solutions:

  • Drive not mounting after reboot – your fstab entry might have the wrong UUID or filesystem type. Double-check with blkid and test with sudo mount -a.
  • Backup fails silently – cron sends output to email by default, but if email is not set up, you never see errors. Redirect output to a log file in your cron line, e.g., 0 2 * * * /home/pi/backup.sh >> /home/pi/backup.log 2>&1.
  • Permission denied – the user running the cron job may not have read access to the source or write access to the backup drive. Use sudo inside the script if needed, or adjust permissions.
  • Slow backups over Wi-Fi – use Ethernet for the Pi and the source machine. Wi-Fi is fine for small files, but large transfers can take hours.
  • SD card corruption – if the Pi loses power during a backup, the SD card can corrupt. Use a UPS or a quality power supply, and consider running the OS from a USB SSD instead of an SD card.

Costs and Power Consumption

The Raspberry Pi itself costs between $35 and $80 depending on the model. A basic kit with power supply and case adds about $20-30. The biggest cost is the external drive – a 1TB portable hard drive is around $50-60, and an SSD is more expensive but faster. Over a year, the Pi consumes about 5-10 watts, which at typical electricity rates adds up to roughly $5-15 per year. Compare that to a dedicated NAS, which can cost $200-500 plus drives, and the Pi is clearly the budget option.

However, the Pi is not a full replacement for a commercial NAS. It lacks hardware RAID (though you can use software RAID), and its USB and network speeds are limited (about 100-300 MB/s on Pi 4/5). For a home with a few computers, that is plenty.

Safety and Best Practices

A backup is only useful if it works when you need it. Follow these practices to avoid losing data:

  • Test your backups regularly – try restoring a file from your backup at least once a month.
  • Use the 3-2-1 rule – keep at least three copies of your data, on two different media, with one copy offsite. The Pi can handle two local copies, but you still need an offsite copy (e.g., cloud storage or a drive at a friend’s house).
  • Protect the Pi from power loss – a sudden shutdown can corrupt the SD card. Consider a small UPS or a battery hat.
  • Encrypt sensitive backups – if you use Borg or Restic, enable encryption so that if the drive is stolen, your data is safe.
  • Monitor backup health – set up a simple script that checks the backup log and sends a notification via email or a messaging service if something fails.

Frequently Asked Questions

Can I use a Raspberry Pi Zero for backups?

Yes, but it is very slow. The Pi Zero has a single-core CPU and limited RAM, so large backups will take a long time. It works for small text files or if you are patient, but for anything serious, use a Pi 3 or 4.

How do I back up my Windows PC to the Raspberry Pi?

You can use Samba on the Pi to create a shared folder, then use Windows’ built-in File History or robocopy to copy files to that share. Alternatively, install rsync for Windows (e.g., via WSL or cwRsync) and run it from Task Scheduler.

Can I back up my Mac with Time Machine to a Raspberry Pi?

Yes, you can set up Netatalk on the Pi to act as a Time Machine server. There are many tutorials online, but the basic steps are: install netatalk, configure it to allow Time Machine, and then point your Mac to the Pi’s shared folder.

What is the difference between a backup and a sync?

A sync copies files both ways – if you delete a file on your computer, it gets deleted on the Pi. A backup copies one way and keeps a history, so you can recover old versions or deleted files. For backups, always use a tool that keeps versions, like rsync with backup options or Borg.

How often should I run backups?

It depends on how much data you can afford to lose. For important documents, daily is common. For photos or videos, weekly might be enough. The key is to be consistent – a backup that runs every day at 2 AM is better than one that runs only when you remember.

Can I back up the Raspberry Pi itself?

Yes, you can create an image of the SD card using dd or tools like Raspberry Pi Imager’s backup feature. This is useful if you want to restore the entire system, not just files. However, for most users, backing up the data files is sufficient.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →