Save and restore SD card.

Run df -h without SD card.

Insert SD card then run df -h. Notice the SD card is at /dev/sdc which has two partitions /dev/sdc1 and /dev/sdc2.

To back up the whole disk:

To back up only the used portion of disk use dd’s block size (bs) and count parameters.
For example, as we can see in above listing, both /dev/sdc1 and /dev/sdc2 partitions use less than 3GB. We can use 3000 counts of 1MB block size for 3GB. Adjust block size (bs) and count to match yours.

To restore. Use the df -h methods to confirm the SD device path. The dd command will clone the new SD to the same size as the one you save in above steps. If the new SD card is of larger size, you can expand the root partition later. See this post for instruction on how to expand partition.

The dd comand doesn’t display any progress status. It could take more than 5 minutes to finish. To see dd progress you can run pkill -USR1 -n -x dd in another terminal.