This question already has an answer here:

I use 32gb micro sd card for my raspibian. I use dd this command to backup it. But, I found it is too large. How to shrink it before I backup it? Thanks.

Add words:
I had used a 16gb sd card. Then, I used sudo dd if=/dev/sdx of=~/myraspibianbackup.img bs=1m for backup. Later, I just used 32gb card for recovering image.

The df -h show me as below:

<code>
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  5.2G  8.6G  38% /
devtmpfs        458M     0  458M   0% /dev
tmpfs           462M     0  462M   0% /dev/shm
tmpfs           462M   13M  450M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           462M     0  462M   0% /sys/fs/cgroup
/dev/mmcblk0p1   63M   22M   42M  35% /boot
</code>

But, why my /dev/root is still 15G, not 31G. (eg. 16gb card=>15G 32gb card=>31G)

marked as duplicate by goldilocks Sep 23 '17 at 12:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Why do you want to shrink? If it is just to make a smaller backup, pipe the output of dd to gzip. This will be the same size as if you shrunk it first. Shrinking an image (strictly a partition on the image) is not straightforward and cannot be done on a mounted partition. – Milliways May 8 '17 at 2:44
  • I also agree with Milliways : dd if=/dev/sda | gzip -9 > /mnt/backup.gz , where sda is where your system is. – H G Sur May 8 '17 at 7:05
  • Hi, I add some words for explaining my situation. – jefferyear May 10 '17 at 0:20
  • The fact that you have never expanded your system to use the full SD does not change my comment. If you desperately want to make an uncompressed 16GiB image paste the output of sudo fdisk -l /dev/mmcblk0 – Milliways May 10 '17 at 8:38
  • Hi, thanks. I didn't expand it with running this compress command. The result is good. I get a 5gb size of file. But, I still need to use the 32gb card when I plan to recover my backup. – jefferyear May 12 '17 at 0:43

Working with Raspberry Pi SD cards is painful on Windows.

I suggest using a gparted Live CD (based on Linux) to modify RPi partitions. You can boot from such a disk without touching your Windows install at all. This is much safer.

gparted Screenshot

A better option is the SD card copier (piclone) included in Raspbian itself , since it works with cards both smaller and bigger than the original. You must be running Raspbian on the Pi and need to use an additional USB cardreader (or USB harddisk/pendrive).

SD card copier screenshot

  • Thanks. I don't know this tool. I thinks I should try it. – jefferyear May 10 '17 at 1:04
  • Please mark this answer as accepted if its useful. – flakeshake May 10 '17 at 7:26
  • The OP never mentioned Windows, indeed the use of dd contraindicates this. Also he did not ask how to duplicate a card, but to backup. – Milliways May 10 '17 at 8:42
  • SD Card Copier is my first time to know this tool. It seems convenient to backup the all device. Unfortunately – jefferyear May 12 '17 at 0:51
  • , I have expanded my sd card to the full size via <code>sudo rasps-config</code>. I think I can't go back to try this tool SD Card Copier under a situation which is not fully expanded system. I know the other tool, Gparted. I had been using it to rescue a block sd card. (i.e. This is the other long story and I think I skip it.) This tool is quite good. But, I am not sure if I shrink my system from currently fully expanded 32gb to 16gb or 8gb, then I <code>dd</code> it in vaild. – jefferyear May 12 '17 at 1:03

If you don't need an exact image clone or image file, but instead just need a logical mSD clone, then have a look at rpi-clone which is a terminal script. It allows to "clone" to a physically different mSD, and creates bootable mSDs.

A simple rpi-clone -f sda creates a fresh copy from the running system. Lateron, rpi-clone sda does a delta update.

Project is on GitHub: https://github.com/billw2/rpi-clone

  • Thumb!!!!!!!!!! – jefferyear Jul 12 '17 at 7:54

If you need to generate a compressed image, first overwrite free space with a contiguous stream of zeroes.

nice -20 dd if=/dev/zero bs=1M of ~/nulls ; sync ; rm ~/nulls

Then halt and make your image.

xz -e9v image.dd this will usually generate a highly compressed file, usually smaller than the used space on the card.

xz -e5v is a lower-memory alternative.

Not the answer you're looking for? Browse other questions tagged or ask your own question.