3

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)

flakeshake
  • 6,235
  • 1
  • 14
  • 33
jefferyear
  • 193
  • 3
  • 9
  • 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 08 '17 at 02: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 08 '17 at 07:05
  • Hi, I add some words for explaining my situation. – jefferyear May 10 '17 at 00: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 08: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 00:43
  • sudo fdisk -l /dev/mmcblk0 this command shows up my system size in detail. I just use sudo raspi-config to expand my full size of my sd card. Now, it shows up the full size. However, if I want to use a 8gb or 16gb card to recover my image. It seems a bother. Do I have a command way to handle this? – jefferyear May 12 '17 at 00:49

3 Answers3

3

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

flakeshake
  • 6,235
  • 1
  • 14
  • 33
  • Thanks. I don't know this tool. I thinks I should try it. – jefferyear May 10 '17 at 01:04
  • Please mark this answer as accepted if its useful. – flakeshake May 10 '17 at 07:26
  • 1
    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 08: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 00:51
  • , I have expanded my sd card to the full size via sudo rasps-config. 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 dd it in vaild. – jefferyear May 12 '17 at 01:03
  • (i.e. Is Gparted workable to shrink my sd card and in the same time to let me make a smaller size system to codedd it?) – jefferyear May 12 '17 at 01:06
  • The 2nd part of this answer (piclone) is probably the most approachable for many inexperienced users, at least if they're using the Pixel desktop. A duplicate card is a good backup. – bobstro Sep 17 '17 at 19:50
1

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

TheDiveO
  • 1,591
  • 1
  • 11
  • 16
0

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.

MatsK
  • 2,791
  • 3
  • 16
  • 20
user2497
  • 679
  • 5
  • 8