Back up a Raspberry Pi SD card over SSH with dd
Create a compressed raw image of a Raspberry Pi SD card over SSH with dd, and restore it to a card.
A raw image of a Raspberry Pi SD card can be taken from a Linux computer over SSH, with the data compressed on the Pi before it is sent. The image can be written back to a card of the same or a larger size.
1. Identify the SD card device
Connect to the Raspberry Pi and list the block devices:
lsblk -p
Note the device that holds the root partition (for example /dev/mmcblk0), then disconnect.
2. Create the backup
Run this from the Linux computer that will store the image:
ssh pi@192.0.2.10 "sudo dd if=/dev/mmcblk0 bs=1M | gzip -" | dd of=~/Desktop/pibackup.gz
Enter the password of the Pi user when prompted, and the backup starts.
| Part | Role |
|---|---|
ssh pi@192.0.2.10 |
opens an SSH session to the Raspberry Pi |
sudo dd if=/dev/mmcblk0 bs=1M |
reads the SD card as root, in 1 MiB blocks |
gzip - |
compresses the data on the Pi before it is sent |
dd of=~/Desktop/pibackup.gz |
writes the compressed stream to a local file |
To follow the progress from another terminal on the local computer:
watch -n 10 ls -lh ~/Desktop/pibackup.gz
When dd finishes, it prints the number of records and bytes copied.
3. Restore the image
The Pi cannot be restored remotely. Remove the SD card, insert it in a Linux computer with a card reader, and find its device name:
lsblk -p
Then write the image to the card, replacing /dev/sdX with the card device:
gunzip -dc pibackup.gz | sudo dd of=/dev/sdX bs=1M
Put the card back into the Pi once the copy is finished.
Notes
- dd copies the whole device, including all partitions, so the target card must be at least as large as the source card.
- Writing to the wrong device destroys its content. Check the device name with
lsblkbefore the restore.