1

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.

Darth Vader
  • 4,206
  • 24
  • 45
  • 69
David P.
  • 121
  • 1
  • 7
  • If you mean that the filesystem on the card was resized up at some point, yes, this will have wrecked the image, see http://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 May 22 '15 at 12:15

2 Answers2

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:

IMG=2015-05-05-raspbian-wheezy.img
MDIR=/media/img_resize

#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

#zerofree
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.

user1133275
  • 2,216
  • 15
  • 31
1

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.

Seamus
  • 21,900
  • 3
  • 33
  • 70
  • 1
    I found this Image File Utilities very good, even DD newbies like me find it easy. *Image File Utilities* - rpi.org.forums.advancedusers 2019aug01 https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=247568#p1511694. Many thanks for the new news. – tlfong01 Sep 03 '20 at 07:35