6

I did a backup for my RPi (64GB microsd card) and it saved the entire 64GB worth .img file... while theres only ~14GB worth actual data, the image file accounts also for that empty space.

Anyone know if theres a way to remove that empty space from the .img file?

Many thanks in advance :-) EDIT: I am using Windows10

0

4 Answers 4

6

Already answered here dd-on-entire-disk-but-do-not-want-empty-portion

Assuming you want to save /dev/sdXN to /tgtfs/image.raw and you are root:

mkdir /srcfs && mount /dev/sdXN /srcfs

Use zerofill or just:

dd if=/dev/zero of=/srcfs/tmpzero.txt

To fill unused blocks with zero (wait for it to fill the file system completely then

rm /srcfs/tmpzero.txt

Take the image with dd and use conv=sparse to punch zeros on-the-fly:

dd conv=sparse if=/dev/sdxn of=/tgtfs/image.raw

If you want to use compression you don't need to punch the zeros with dd as zero blocks are highly compressible:

dd if=/dev/sdxn | gz -c | dd of=/tgtfs/image.raw

PS: You should note that this is not a good idea on a flash memory based storage media (i.e. your source file system be SSD).

5

https://www.raspberrypi.org/documentation/linux/filesystem/backup.md explains backup and restoration.

You don't indicate what OS you are using but it is simple on any 'NIX system. I use the following on macOS

#!/bin/bash
# script to backup Pi SD card
# 2017-06-05
# 2018-11-29    optional name
# DSK='disk4'   # manual set disk
OUTDIR=~/temp/Pi

# Find disk with Linux partition (works for Raspbian)
# Modified for PINN/NOOBS
export DSK=`diskutil list | grep "Linux" | sed 's/.*\(disk[0-9]\).*/\1/' | uniq`
if [ $DSK ]; then
    echo $DSK
    echo $OUTDIR
else
    echo "Disk not found"
    exit
fi

if [ $# -eq 0 ] ; then
    BACKUPNAME='Pi'
else
    BACKUPNAME=$1
fi
BACKUPNAME+="back"
echo $BACKUPNAME

diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > $OUTDIR/Piback.img.gz

#rename to current date
echo compressing completed - now renaming
mv -n $OUTDIR/Piback.img.gz $OUTDIR/$BACKUPNAME`date +%Y%m%d`.img.gz
10
  • I am very sorry for the late reply I am on Windows10 Commented Sep 3, 2017 at 10:07
  • doesn't work. bs=4m makes dd choke and changing it to 4M also doesn't help Commented Oct 19, 2019 at 8:59
  • @WolfgangFahl "changing it to 4M also doesn't help" hardly surprising because dd on BSD Unix doesn't support this.
    – Milliways
    Commented Oct 19, 2019 at 10:26
  • Since my computer is a MacOS computer i had assumed things would work. Commented Oct 19, 2019 at 10:58
  • @WolfgangFahl: Thanks for your comment as I was considering using this, but it seems to have issues? Did you ever get it to work? If so, would you post your approach as another answer?
    – Seamus
    Commented Jan 27, 2020 at 18:08
1

Shrink your second partition with gparted to eliminate free space and use dd to only backup the parts you need.

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

You can now use dd to only backup the parts of the SD card that matter.

Any sensible compression program will automatically detect free space and collapse it efficiently.

0

Either use raspiBackup to create the dd image and read FAQ 16 or use pishrink.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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