Skip to content
GEEKnodes

Raspberry Pi

How to Back Up Your Raspberry Pi SD Card

Clone your Raspberry Pi SD card on Windows, macOS or Linux, compress and restore the image, and back up just your data with rsync.

microSD cards fail, and almost always without warning: a power cut or a worn-out card and you lose weeks of configuration. A full backup gets your Pi running again in minutes. Here you will see how to clone the card on Windows, macOS and Linux, how to compress and restore the image, and how to back up only your data with rsync.

In short

  • There are two strategies: a full image of the card (restore everything as it was) or a data backup with rsync (lighter and more frequent).
  • Raspberry Pi Imager does not make backups: it only writes images. To read the card you need dd, Win32 Disk Imager, SD Card Copier or rpi-clone.
  • With dd, a mistake in the disk name erases the wrong disk. Check the device twice.

Full image or data backup

Comparison: full image of the whole SD card versus copying just some folders with rsync to a USB drive
The image keeps the whole card; rsync only the folders you choose.
Full imageData backup with rsync
What it keepsEverything: system, partitions and dataThe folders you choose
SizeThat of the whole card (less if compressed)Only what your files take
RestoreWrite the image to a cardInstall the system and copy files back
Sensible frequencyAfter big changesDaily or weekly
Pi powered offYes, normallyNo: done while running

Ideally combine both: an image once your system is set up and rsync day to day.

Clone the card on Linux with dd

Shut down the Pi, take out the card and connect it to your computer with a reader.

dd diagram: SD card /dev/sdb is the if source and the raspberry-backup.img file is the of destination
if is the source and of the destination: swap them and you overwrite the card.
  1. Identify the device. Look at the size, not just the name:
lsblk

The card shows up as something like /dev/sdb or /dev/mmcblk0, with two partitions (bootfs and rootfs).

  1. Unmount the partitions if the system mounted them automatically:
sudo umount /dev/sdb1 /dev/sdb2
  1. Read the whole card into a file:
sudo dd if=/dev/sdb of=~/raspberry-backup.img bs=4M status=progress conv=fsync

if is the source (the card) and of the destination (the file). If you swap them, you overwrite the card with the file. Always use the whole disk (/dev/sdb), never a partition (/dev/sdb1).

Clone the card on macOS

macOS also includes dd, with different disk names.

  1. List disks and find the card by its size:
diskutil list
  1. Unmount the card, do not eject it. Replace disk4 with yours:
diskutil unmountDisk /dev/disk4
  1. Copy using rdisk, the raw access, which is much faster:
sudo dd if=/dev/rdisk4 of=~/raspberry-backup.img bs=4m

macOS dd does not always show progress. Press Ctrl+T in the terminal to see how many bytes it has copied.

Clone the card on Windows

Windows has no dd. The most used tool is Win32 Disk Imager, which can read a card:

  1. In Image File type the path and name of the file to create, ending in .img.
  2. In Device select the drive letter of the card’s bootfs partition.
  3. Click Read. Do not click Write: that writes the image to the card.

If Windows offers to format the card when you insert it, say no. It sees the rootfs partition (ext4) as unreadable; that is normal.

Clone from the Raspberry Pi itself

If you prefer not to remove the card, you can clone it to another card or a USB-connected SSD with the Pi running.

SD Card Copier (desktop)

Raspberry Pi OS with desktop includes SD Card Copier in the Accessories menu:

  1. Connect the destination card or drive with a USB adapter.
  2. In Copy From Device choose the internal card and in Copy To Device the destination.
  3. Click Start. It erases the destination completely.

It adjusts partition sizes to the destination, so you can copy to a smaller card if the data fits. The New Partition UUIDs option lets you use both cards on the same Pi at once.

rpi-clone (terminal)

rpi-clone is a script that does the same from the terminal and, on later runs, only copies changes with rsync, making it very fast for periodic backups. The original project is no longer maintained; use a maintained fork and read its install instructions. Basic use:

sudo rpi-clone sda

Where sda is the destination disk, without /dev/. The script shows what it will do and asks for confirmation.

Cloning a running system is convenient, but if a program is writing at that moment (a database, for example), those files may end up inconsistent. For critical services, stop them first or image the Pi powered off.

Compress the image

An image takes as much as the card, even half empty: a 64 GB card gives a 64 GB file. Empty space compresses very well:

ToolCommandNote
gzipgzip -k raspberry-backup.imgFast, compatible with everything
xzxz -k -T0 raspberry-backup.imgSmaller files, slower; -T0 uses all cores
zipzip backup.zip raspberry-backup.imgHandy to open on Windows

You can also compress while copying, skipping the giant file:

sudo dd if=/dev/sdb bs=4M status=progress | xz -T0 > ~/raspberry-backup.img.xz

To shrink further, PiShrink (Linux) shrinks the system partition to the used space. The image then fits on smaller cards and expands again on first boot.

Restore the image

The safest way is Raspberry Pi Imager: in Choose OS scroll to Use custom and select your file. It accepts .img and compressed ones such as .img.xz, .gz or .zip.

With dd on Linux:

xzcat ~/raspberry-backup.img.xz | sudo dd of=/dev/sdb bs=4M status=progress conv=fsync

Note that two 32 GB cards do not have exactly the same capacity. If the destination is slightly smaller, restoring an unshrunk image fails at the end. Restoring to an equal or larger card avoids the problem.

Back up only your data with rsync

Day to day you do not need to clone the system. Just keep what cannot be reinstalled: your home folder, /etc and your services’ data. With a USB drive mounted at /mnt/backup:

sudo rsync -aAXH --delete /home/ /mnt/backup/home/
sudo rsync -aAXH --delete /etc/ /mnt/backup/etc/
OptionWhat it does
-aArchive mode: recursive, keeps permissions, dates and links
-AKeeps ACLs
-XKeeps extended attributes
-HKeeps hard links
--deleteDeletes at the destination what no longer exists at the source
--dry-runSimulates without copying

The trailing slash on the source matters: /home/ copies the folder’s contents; /home would create home/home at the destination. Try --dry-run first, especially with --delete.

You can also send the backup to another computer on the network over SSH:

rsync -aAXH --delete /home/your-user/ user@192.168.1.50:/copias/raspberry/

To make it automatic, schedule these lines with cron or a systemd timer.

Common mistakes

  • Picking the wrong disk in dd. It is irreversible. Check size with lsblk or diskutil list right before and unplug external drives you do not need.
  • Thinking Raspberry Pi Imager makes backups. It only writes; it does not read the card.
  • Keeping the backup on the same card. If the card dies, the backup dies with it.
  • Never testing a restore. A backup you have not restored is a guess. Write it to another card once and boot from it.
  • Using --delete without --dry-run the first time, with source and destination swapped.
  • Running out of space on the computer. The uncompressed image takes the whole card; compress on the fly if you are short.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor