Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 37 ยท Deployment and Automation

Backups and Taking Care of the SD Card

Protect your Pi against SD card failure: image and file backups with rsync, retention rules, reducing writes, log2ram, read-only root, SSD boot and a runnable backup rotation model.

A microSD card is the weakest part of a Pi. Flash memory wears out with every write, and a power cut during a write can corrupt the filesystem. Many "my Pi died" stories are really "the card died". The solution is not better luck but better habits: reduce writes, back up the things that matter, and make restoring boring and fast.

Backups touch a real system, so commands here are read-only. The retention logic, which is where backup scripts most often go wrong, is modelled in Python you can run.

What to back up

Think in layers:

  1. Your data and code (the project folder, databases, config in /etc): small, changes often, vital.
  2. The list of installed packages and steps to rebuild (a README or setup script): free, and it makes a card swap quick.
  3. A full disk image: heavy, but turns recovery into "write the image to a new card".

The cheapest strategy for a project Pi is a setup script in Git, plus nightly file backups of data to another machine. A periodic full image is a bonus before big upgrades.

Terminal
# list what you installed by hand and the active services
apt list --manual-installed 2>/dev/null | head
systemctl list-unit-files --state=enabled
crontab -l

File backups with rsync

rsync copies only what changed, so nightly runs take seconds. Push to a laptop, a NAS or a USB drive:

Terminal
rsync -a --delete /home/ana/weather/ backup@nas.local:/backups/pi-weather/
rsync -a /etc/ /mnt/usb/pi-etc/

For versions that do not eat disk space, use hard-link snapshots: files that did not change are links to the previous snapshot.

Terminal
today=$(date +%F)
rsync -a --delete --link-dest=/mnt/usb/snap/latest /home/ana/ /mnt/usb/snap/$today/
ln -sfn /mnt/usb/snap/$today /mnt/usb/snap/latest

Run it from a systemd timer with Persistent=true. Databases need care: copy SQLite with its own command so the file is consistent:

Terminal
sqlite3 weather.db ".backup '/mnt/usb/weather-$(date +%F).db'"

Retention: keeping the right backups

If you keep every backup, the disk fills; if you delete blindly, you lose the one from before the bug. A common rule is: keep the last 7 daily backups, the last 4 weekly ones and the last 6 monthly ones. This model applies exactly that rule to a list of dates so you can check a scheme before trusting a script.

Python Keep 7 daily, 4 weekly, 3 monthly backups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Retention bugs are silent, so print what would be deleted first (a "dry run") and only then remove files.

Verify, or it is not a backup

A backup you never restored is a hope. Test regularly: restore a file to a temporary directory and compare checksums. This stand-in shows the comparison idea: hash each file and report differences between the source and the backup.

Python Verify a backup with checksums

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On the Pi the real tools are sha256sum, rsync --checksum --dry-run -i and, for images, cmp.

Full images

To clone a whole card, shut the Pi down and read the card on another computer (dd, Raspberry Pi Imager's tools, or a disk-image utility), or use a tool like rpi-clone to copy to a second card while running. Compress the image: an unused-space-heavy image shrinks a lot with xz. Label cards and keep a spare, already written, in a drawer.

Reducing SD card wear

Flash cards survive a limited number of writes per cell. Frequent small writes are the killers: logs, swap, databases, the journal.

  • Use A1 or A2 rated cards from known makers, with capacity larger than you need (wear leveling has more room).
  • Keep the journal in RAM: set Storage=volatile in /etc/systemd/journald.conf, accepting that logs vanish at reboot. Or use log2ram, which keeps /var/log in RAM and flushes it periodically.
  • Reduce swap: sudo dphys-swapfile swapoff or a small swap file; or use zram.
  • Write sensor data in batches, not once per second; or keep it in RAM and flush every few minutes.
  • Mount /tmp as tmpfs (often the default).
  • For appliances, consider a read-only root filesystem (the overlayroot option in raspi-config's Performance section): the card is never written, changes live in RAM, and a power cut cannot corrupt it. You then store data on a USB drive or a separate writable partition.
  • Best of all: boot from a USB SSD (Pi 4 and 5 support it). SSDs are far faster and last longer than SD cards.

Always shut down cleanly (sudo shutdown -h now) and wait for the green activity LED to stop before pulling power. A small button wired to a GPIO pin that runs shutdown is a nice addition.

flowchart TD
    A["Card failure?"] --> B{"Backup of data?"}
    B -- yes --> C["New card + setup script + restore data"]
    B -- no --> D["Try fsck on another PC, copy what is left"]
    C --> E["Back online in minutes"]

Preparation turns a failed card into a short interruption.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which approach gives versioned backups that save space on unchanged files?

    Unchanged files are hard links to the previous snapshot, so they take no extra space.
  2. 2. What is the best way to reduce SD card wear for a logging Pi?

    Fewer small writes and keeping heavy-write data off the card extend its life.
  3. 3. Why test restoring a backup?

    Verification catches missing files, stale data and wrong paths before you need the backup.
  4. 4. What does overlayroot do?

    The card is not written, so a power cut cannot corrupt it; changes are lost at reboot.

Summary

  • Back up data and code regularly, keep a setup script, and make full images before big changes.
  • rsync and hard-link snapshots make nightly backups cheap; use sqlite3 .backup for databases.
  • Retention rules (daily, weekly, monthly) need a dry run before deletion.
  • Verify by restoring or comparing checksums.
  • Reduce writes, prefer A1/A2 cards, log to RAM, use read-only root or a USB SSD, and shut down cleanly.

Next: Hardening Your Pi's Security