1

For testing various setups I feel it more comfortable to use tar archives instead of images.

  • You only save programs and data, no need to fiddle with nullbytes to optimize compression or shrink partition sizes.
  • The size of the sd card doesn't matter. It is easy to get the tar ball from a 16GB card and extract it onto a 8GB card (when the programs fit to it).
  • You are more flexible in creating additional partitions.

Is it possible to use tar balls instead of images to backup Raspbian?

1

It is possible to backup the installation into a tar archive and restore it to a free partitioned SD Card.

For example I use Raspbian Stretch Lite 2018-06-27. We do not need a Raspberry Pi for this. We can all do on a pc with a linux operating system with SD Card inserted. You should have attached the SD Card. For my example it is attached to /dev/sdb.

Setup

Step 1: backup image

Download the Raspbian image unzip it and mount its partitions. I use parted to get the offsets for mounting. We need Start for offset and Size for sizelimit. You should find the right numbers with the example.

pc ~$ unzip 2018-06-27-raspbian-stretch-lite.zip
pc ~$ sudo -Es
pc ~# parted 2018-06-27-raspbian-stretch-lite.img unit s print
Model:  (file)
Disk /home/ingo/Downloads/2018-06-27-raspbian-stretch-lite.img: 3637248s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End       Size      Type     File system  Flags
 1      8192s   96663s    88472s    primary  fat32        lba
 2      98304s  3637247s  3538944s  primary  ext4

pc ~# mkdir img/
pc ~# mount -o loop,offset=$((98304*512)),sizelimit=$((3538944*512)) 2018-06-27-raspbian-stretch-lite.img img/
pc ~# mount -o loop,offset=$((8192*512)),sizelimit=$((88472*512)) 2018-06-27-raspbian-stretch-lite.img img/boot

Prepare settings for booting: change device names in img/boot/cmdline.txt and in img/etc/fstab from PARTUUID=... to /dev/mmcblk0p

pc ~# sed -i 's/ root=PARTUUID=[a-z0-9]*-0/ root=\/dev\/mmcblk0p/' img/boot/cmdline.txt
pc ~# sed -i 's/^PARTUUID=[a-z0-9]*-0/\/dev\/mmcblk0p/' img/etc/fstab

Backup the image into a tar archive:

pc ~# tar -cvzf 2018-06-27-raspbian-stretch-lite.tar.gz -V "backup of Raspbian Stretch Light 2018-06-27" -C img/ ./ #dot and slash as last characters
pc ~# umount img/boot
pc ~# umount img/
pc ~# rmdir img/

Step 2: partition SD Card

Create the master boot record (MBR) on the SD Card by writing this pattern to the MBR.
Warning! This will overwrite the existing partition table and destroy all data on the SD Card without asking:

pc ~# dd if=/dev/zero of=/dev/sdb bs=512 count=1
pc ~# /bin/echo -ne '\x28\xe4\x3e\x4d' | dd of=/dev/sdb bs=1 seek=440
pc ~# /bin/echo -ne '\x55\xaa' | dd of=/dev/sdb bs=1 seek=510

Partition and format the SD Card. You can use every sizes you want. For this example I use 60 MB for boot, a bit more than the original and 3 GB for root.

pc ~# parted /dev/sdb mkpart primary fat32 8192s 60MB
pc ~# parted /dev/sdb mkpart primary ext4 60MB 3GB

If you do not want that the root partition will be expanded to maximum size on first boot of the SD Card you can optional add an empty partition:

pc ~# parted /dev/sdb mkpart primary 3GB 100%

Step 3: install from archive

Format the partitions:

pc ~# mkfs.vfat -F 32 -n BOOT /dev/sdb1
pc ~# mkfs.ext4 -L rootfs /dev/sdb2

Mount the SD Card and restore the archive:

pc ~# mkdir mnt/
pc ~# mount /dev/sdb2 mnt/
pc ~# mkdir mnt/boot
pc ~# mount /dev/sdb1 mnt/boot
pc ~# tar -xvzf 2018-06-27-raspbian-stretch-lite.tar.gz -C mnt/

pc ~# umount mnt/boot
pc ~# umount mnt/
pc ~# rmdir mnt/
pc ~# exit
pc ~$

That's it. Put the SD Card into a RasPi and boot.

Details

Step 1: backup image

Have in mind that we are working in units (sectors) of 512 bytes.

We have to address the beginning of the partitions in the image. This is done with an offset to the mount command. For details to do this look at (1).

By default Raspbian uses PARTUUID to address storage and partitions on it. But this is a problem when using partitioner like parted, fdisk and others. They may change the PARTUUID silently when they find it's not unique (2). Then you have the problem that the installation does not boot. So we change the partition names to the more generic names /dev/mmcblk0p1 and /dev/mmcblk0p2.

Backup an image is only needed one time from a new version. Then you can use the archive multiple times to restore to a prepared SD Card.

Step 2: partition SD Card

To boot from a storage it must have the master boot record (MBR) in its first sector that also contains the partition table. We create the MBR by writing the specific pattern for a Raspberry Py into the first sector. This will overwrite the existing partition table without warning. Afterwards we have to create a new partition table by partitioning the storage.

On formating the SD Card you can add an empty partition as last partition to avoid expanding the root partition to maximum. After first boot you can delete this partition and use the space for what ever you want. On my tests I had to powercycle with this filled SD Card before it boots, don't know why.

Preparing a SD Card this way is only needed one time. Then you can use it multiple times by formating the partitions and restore from the archive.

Step 3: install from archive

The boot partition is formated as fat32 and the root partition as ext4 as needed for Raspbian. You can format a prepared SD Card (Step 2) multiple times and restore the archive as shown.


referencess:
[1] How can I mount a Raspberry Pi Linux distro image?
[2] Is it possible to use partition UUID for root-parameter in cmdline.txt?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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