Transfering all Raspberry Pi OS data to a new SD Card

Posted on 19 August 2023

If you ever need to transfer your whole operating system data from one SD Card to another on Raspberry Pi...

Disk dump

...then you might consider as whole "disk" dump:

dd if=/dev/sdx of=/diskdump status=progress

Which you can then transfer to another device with:

dd if=/diskdump if=/dev/sdy status=progress

It's quite simple and enough for many use cases. However, there are some disadvantages with this approach:

  • It requires transfering every single byte (including empty space inside partitions), so it's slow for large SD cards and adds unnecessary strain on the SD card life span.
  • If /dev/sdy is smaller than /dev/sdx, this will not work at all.
  • The partitions will have duplicated UUIDs.

Efficient backup with rsync

An alternative approach is to rsync all data in the root partition of /dev/sdx, which on Ubuntu defaults to being mounted to /media/$USER/rootfs/:

# Old SD card root partition is mounted at /media/$USER/rootfs/
sudo rsync -axogHAX --progress --delete-during /media/$USER/rootfs/ /tmp/rootfs/

The use of sudo is important to preserve all OS files. If all you want is to make a backup of your RPi data, you may stop here (altough you problably want to store this backup somewhere other than /tmp/rootfs/).

Transfering data to a new SD card

After that, use the disk imager of Raspberry Pi to create a new dump for your new SD card. Then Boot the RPi using the new card, this will make cause OS to trigger the automatic partition resize, once done, shutdown your RPi and remove the SD card back to your main computer and move the saved data to your new SD card:

# New SD card root partition is mounted at /media/$USER/rootfs/
sudo rsync -axogHAX --progress --delete-during /tmp/rootfs/ /media/$USER/rootfs/

Now you are almost done, all you need to done is look for the UUID of your new SD card with

# New SD card boot partition is mounted at /media/$USER/bootfs/
cat /media/$USER/bootfs/cmdline.txt

And then edit the fstab to point to this new UUID:

# New SD card root partition is mounted at /media/$USER/rootfs/
sudo nano /media/$USER/rootfs/etc/fstab