Backup a Rasperry SD

SDcard to an other SDcard

Raspberry PI OS comes with piclone that is a gui application https://github.com/Raspberrypi-ui/piclone.

Do not click the UUID option for a clone. It might happen that side effects might occur due to different UUID.

To not accidentally overwrite the wrong disk, install Raspberry PI OS Desktop in a Virtualmachine as Virtualbox.

To not mix up the source and the destination SDcard, use a simple SD card to USB Adapter for the source and a Multi Memory Card for the destination. The piclone shows additionally to /dev/sd<x> some device information.

SDcard to a backup file

Write the SDcard to the file

Insert SDcard to be backup-ed into a PC, check with fdisk -l or blkid what /dev/sd<x> or /dev/mmcblk<x> it gets and run

dd if=/dev/sd<x> bs=4M status=progress | gzip -c > ~/Backup.img.gz

This takes a long time (the SDcard is generally the bottle neck not the USB) observe the MB/s value to get a rough idea how long it takes (it can take more than one hour) and obviously during this time the Raspberry can not run. This might be an issue.

To see exactly how much time it uses type:

time dd if=/dev/sd<x> bs=4M status=progress | gzip -c > ~/Backup.img.gz

gzip does a good job, so the file size is surprisingly small.

Write the file back to a SDcard

To an equal or bigger SDcard

To create a cloned SDcard plug in an empty card with at least the same size check it name fdisk -l or blkid and run

gunzip -c ~/Backup.img.gz | dd of=/dev/sd<x> bs=4M status=progress && sync

or to see the time it takes

time gunzip -c ~/Backup.img.gz | dd of=/dev/sd<x> bs=4M status=progress && sync

If the card is bigger than necessary then Raspberry-config can expand the file-system to use the complete card. The bigger the card the longer it takes to wear-out.

If Raspberry-config is not available as when having an other Linux distribution run e2fsck /dev/sd<x>3 to assure having a clean partition. There are different ways.

Use fdisk to create a 4th partition /dev/sd<x>4 and use it to store data

resize2fs /dev/sd<x>3 and run e2fsck /dev/sd<x>3 to check.

Use fdisk delete the 3rd partition and recreate it to have the full size. Then exit fdisk with w and run e2fsck /dev/sd<x>3 to check.

To a smaller or any SDcard

Use the original SDcard or do the steps above on a equal or bigger SDcard. Plug this source SDcard in the PC and mount it so it appears under /run/media.

Then plug in a new SDcard that can also be smaller as long as the files fit. If possible plug it into an embedded SDcard slot so it appears under /dev/mmcblk<x> so the probability to make the following steps on the wrong SDcard is reduced. Use fdisk to create the empty destination SDcard with the size, partitions and filesystems as desired:

/dev/mmcblk<x>1 128M c W95 FAT32 (LBA)

/dev/mmcblk<x>2 2G 82 Linux swap / Solaris

/dev/mmcblk<x> <rest of it>G 83 Linux

mkfs.vfat -F 32 /dev/mmcblk<x>p1

mkfs.ext4 -T small /dev/mmcblk<x>p3

mkswap /dev/mmcblk<x>p2

swapon /dev/mmcblk<x>p2

and then do blkid to see what is there and do for all data partitions

cp -arT <source> <destination>

Important

Be aware that this way the UUID of the partitions will be different. /etc/fstab might be required to adapt this.


Linurs startpage