#linux #homelab #security

It is important to first unmount the SD Card to avoid corruption of the backup.

Unmount SD Card


Find Device Identifier

To unmount the card properly, first identify its device identifier. Use lsblk in the terminal after plugging in the SD Card.

You should find a device similar to this:
mmcblk0 179:0 0 29.7G 0 disk
├─mmcblk0p1 179:1 0 256M 0 part /media/<user>/boot
└─mmcblk0p2 179:2 0 29.5G 0 part /media/<user>/rootfs


This shows the partitions you want to unmount:
  • mmcblk0p1
  • mmcblk0p2


You can also see these with df -h:
/dev/mmcblk0p2 29G 6.4G 22G 23% /media/<user>/rootfs
/dev/mmcblk0p1 253M 51M 202M 20% /media/<user>/boot


Unmount SD Card

sudo umount /dev/mmcblk0p2
sudo umount /dev/mmcblk0p1


Verify by running df -h again. Now both partitions should be unmounted. You can still see the device with lsblk.

Ensure you have enough space on your computer to backup the card.




Create the Image


DD stands for 'Disk Duplicator'.

  • if: input file needs to be set to the device identifier of the SD card.
  • of: output file.
  • bs: Block Size.
  • sync: Holds the command open until everything is synchronized and set.

sudo dd if=/dev/mmcblk0 of=~/Documents/sdcard.img bs=4M; sync





Optional: Compress the Image & Shortcut

One can use PiShrink shrinks the SD Card Image as small as it can be. This only works for Pis.

Use gzip
gzip sdcard.img


Shortcut:
Do it in one command:

sudo dd if=/dev/mmcblk0 | gzip > sdcard.img.gz





Restore the Image


Easiest way to flash a card is with balenaEtcher.

You can do it also yourself with the potential to destroy everything:
Reverse the dd command:

sudo dd if=~/Documents/sdcard.img of=/dev/mmcblk0 bs=4M; sync


The reverese of the shortcut command:
gzip -dc sdcard.img.gz | sudo dd of=/dev/mmcblk0