I backed up my Raspbian install with dd:

dd id=/dev/sdb of=/home/user/raspbian-backup.img

That gives me a img of the whole card (16GB). Then I followed a tutorial to resize the image and used:

truncate --reference 2015-05-05-raspbian-wheezy.img raspbian-backup.img

Will this mess up my backup image? Is this the correct process or does the tutorial only work with "new" images? I don't have any SD card to dd in my new backup image and try it.

  • If you mean that the filesystem on the card was resized up at some point, yes, this will have wrecked the image, see raspberrypi.stackexchange.com/q/29947/5538 But if you have never changed the filesystem size, then it should be okay (presuming the size of the partitions in the 5-05-15 Raspbian image are the same as the one you used to install).
    – goldilocks
    Commented May 22, 2015 at 12:15

2 Answers 2


No that is the wrong way to do it; it makes the bad assumption that there are no files in the truncated part.

The correct way is to mount the img via loopback and resize the fs and partition before truncating:


#get partition offset
S=$(fdisk -l $IMG | grep Linux | perl -pe 's/ +/\t/g' | cut -f 2)
B=$(fdisk -l $IMG | grep Units | perl -pe 's/.*=//g;s/[^0-9]//g')
O=$(echo "$S*$B" | bc)

#shrink file system
L=$(losetup -f --show $IMG -o $O)
e2fsck -f $L
resize2fs -M $L
losetup -d $L

#get new file system size
mkdir $MDIR
mount -o loop,offset=$O $IMG $MDIR
U=$(df -B KB $MDIR | tail -n 1 | perl -pe 's/ +/\t/g' | cut -f 2)
umount $MDIR
rm -r $MDIR

L=$(losetup -f --show $IMG -o $O)
zerofree -v $L
losetup -d $L

#shrink the partition
L=$(losetup -f --show $IMG)
N=$(parted $L print | grep ext4 | perl -pe 's/ +/\t/g' | cut -f 2)
S=$(parted $L unit kB print | grep ext4 | perl -pe 's/ +/\t/g' | cut -f 3)
U=$(echo $U | perl -pe 's/[^0-9]//g' )
S=$(echo $S | perl -pe 's/[^0-9]//g' )
U=$(echo "$U+$S+1" | bc)
S=$(parted $L unit s print | grep ext4 | perl -pe 's/ +/\t/g' | cut -f 3)
parted $L rm $N
parted $L mkpart primary ext4 $S $U"kB"
losetup -d $L

#shrink the img file
E=$(fdisk -l $IMG | grep Linux | perl -pe 's/ +/\t/g' | cut -f 3)
NE=$(echo "($E+1)*$B" | bc)
truncate --size=$NE $IMG

This will shrink the official image by 37% from 3.1G to 2.3G (991M to 628M compressed)

Using zerofree and 7z will make a small file for backup/storage/distribution if you don't want to resize the img file.

  • This is a bash script?
    – Andi Giga
    Commented Jun 9, 2017 at 8:58
  • @AndiGiga yes; or likely any similar Unix like shell. Commented Jun 9, 2017 at 17:00

As of today (1 Sep 2020), the Image File Utilities provide a set of tools to easily create and update fully compressed images of a Raspbian/RPiOS system while the system is running. I've used it on my RPis for almost a year now, and find it works very well.

If you elect to use Image File Utilities in an incremental or scheduled backup plan, know that the expected workflow is to use image-backup interactively (i.e. from the command line) to create an initial full backup; incremental backups may be automated (e.g. using cron). Restoration from a backup image to a µSD card is easily accomplished using Etcher or an equivalent.

Image File Utilities was "released" on Aug 1, 2019 in this post to the Raspberry Pi organization's Advanced Users forum, where it is available for download. Support is provided by the author of Image File Utilities in this forum thread. If you prefer to use git to download and maintain Image File Utilities, you may use this unofficial repo on GitHub.


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.