Raspberry Pi backup using fsarchiver and other tricks

So I ran into a few issues using the dd image backup I referenced prior Raspberry Pi 3 SDCard backup

  1. The Image is very large even though the data was not.  For example on a 32GB SD card I was getting a 12GB file.  I only have 3GB of data! so that was a bummer.
  2. When it comes time to recover, I have to expand the gz image file to a full 32GB to then image it onto another SD device.  There’s tricks around this I’m sure but still.
  3. Since dd was reading 100% of the SD card (/dev/mmcblk0) even with compression it took a LONG time to create the image.  20 minutes or so.  Since I’m backing up a live system this was a real issue.

I did manage to figure out how to create a partial image if your partition sizes were smaller than the actual device – This seemed to work but it still was storing 6.6GB of data which was over double what I actually had:

Trimmed SD Image…

root@webpi:/mnt/usb# blockdev --getsize64 /dev/mmcblk0p1 /dev/mmcblk0p2
root@webpi:/mnt/usb# echo `blockdev --getsize64 /dev/mmcblk0p1` `blockdev --getsize64 /dev/mmcblk0p2` + p | dc
root@webpi:/mnt/usb# dd if=/dev/mmcblk0 conv=sync,noerror iflag=count_bytes count=8995806208 \
| gzip > /mnt/usb/webpi.trimmed.img.gz

Still not good enough….  Any I might have to tweak the count to make sure I’m not missing the last little piece of the lasat partition since we would have partition data in front of the partitions.


To remedy a few issues,  I researched other ways to backup.  I came to the conclusion that fsarchiver was a decent fit.  Simple to use and only backs up data.  The downside was I would have to use another Linux system to reconstruct the SD card.  I can’t just blast a image write to a SD card and call it good.

Here are the steps.  Since fsarchiver doesn’t support vfat I had to make a dd image of the 66MB vfat boot partition.  Not a big deal.  The newer fsarchiver supports vfat;  I just didn’t want to install the packages need to do a full compile for the latest.

Benefits:  Much faster.  take 5 minutes total.  Much smaller data footprint – 3GB of data is storing in a  2.2GB image!
Downside:  Not one image – need to do some recovery with another Linux system with a SD card loaded.  Since I have a Pi setup for VPN and such that’s not a problem for me.

Disclaimer – I’m only posting this stuff to help me remember what I did and possibly help others that understand how to not shoot themselves in the foot.  Please be very careful in trying any of this stuff.  Depending on your situation it may not apply.

Raspberry Pi Backup using fsarchiver

  1. # Quiesce any major services that might write…
    service apache2 stop
    service mysql   stop
    service cron    stop
  2. # Save the Partition Table for good keeping…
    sfdisk -d /dev/mmcblk0 > /mnt/usb/webpi.backup.sfdisk-d_dev_mmcblk0.dump
  3. # Save the vfat boot partition
    dd if=/dev/mmcblk0p1 conv=sync,noerror | gzip > /mnt/usb/webpi.backup.dd_dev_mmcblk0p1.img.gz
  4. # Save the main OS image efficiently…
    fsarchiver savefs -A -j4 -o /mnt/usb/webpi.backup_dev_mmcblk0p2.fsa /dev/mmcblk0p2
  5. # Restart the services…
    service cron    start
    service mysql   start
    service apache2 start

Raspberry Pi Restore using fsarchiver

  1. # put a new SD card in a card reader and plugged it 
    # into a raspberry pi - showed up as /dev/sdb
  2. # Restore the partition table
    sfdisk /dev/sdb < /mnt/usb/webpi.backup.sfdisk-d_dev_mmcblk0.dump
  3. # Restore the vfat partition
    gunzip -c /mnt/usb/webpi.backup.dd_dev_mmcblk0p1.img.gz | dd of=/dev/sdb1 conv=sync,noerror
  4. # Run fsarchiver archinfo to verify you have a fsarchiver file and 
    # determine which partition you want to recover if you did multiple partitions
    fsarchiver archinfo /mnt/usb/webpi.backup_dev_mmcblk0p2.fsa 
    ====================== archive information ======================
    Archive type:                   filesystems
    Filesystems count:             1
    Archive id:                     5937792d
    Archive file format:           FsArCh_002
    Archive created with:           0.6.19
    Archive creation date:         2017-06-12_07-51-00
    Archive label:                 <none>
    Minimum fsarchiver version:
    Compression level:             3 (gzip level 6)
    Encryption algorithm:           none
    ===================== filesystem information ====================
    Filesystem id in archive:       0
    Filesystem format:             ext4
    Filesystem label:
    Filesystem uuid:               8a9074c8-46fe-4807-8dc9-8ab1cb959010
    Original device:               /dev/mmcblk0p2
    Original filesystem size:       7.84 GB (8423399424 bytes)
    Space used in filesystem:       3.37 GB (3613343744 bytes)
  5. # Run the restfs option for fsarchiver
    fsarchiver restfs /mnt/usb/webpi.backup_dev_mmcblk0p2.fsa id=0,dest=/dev/sdb2
    filesys.c#127,devcmp(): Warning: node for device [/dev/root] does not exist in /dev/
    Statistics for filesystem 0
    * files successfully processed:....regfiles=59379, directories=6999, symlinks=5774, hardlinks=331, specials=80
    * files with errors:...............regfiles=0, directories=0, symlinks=0, hardlinks=0, specials=0
  6. Run sync for warm fuzzies...


Worked like a CHAMP!

Tags :