Backing up sd card image on external usb drive

I just use cp -a to make backups of my Raspberry Pi installs and to copy them between cards. I have been doing this on another machine running linux but a quick test just now shows the backup seems to work even from a mounted system drive, just using a running OSMC. Another advantage over dd is you back up only the files, not the empty space.

For this method, you need a backup drive (partition) formatted with an extX filesystem, so that permissions are preserved. There may be an alternative of writing the files to a tar archive (on any filesystem) but I haven’t tried that.

I’m going to assume it’s a removeable drive, so mounted at /media/backup (say). If you back up to a network, modify to suit. Then as root:

cd /media
mkdir boot
mkdir system
mount /dev/mmcblk0p1 boot
mount /dev/mmcblk0p2 system
cd backup
mkdir OSMCbackup
cd …
cp -a boot backup/OSMCbackup/
cp -a system backup/OSMCbackup/
umount boot
umount system
rmdir boot (optional - could just leave it there)
rmdir system (optional)

These commands can be put in a shell script and run with cron daily or whatever.

In order to be able to re-construct OSMC on a re-formatted card, make a note of the partition size of the boot partition (currently 244MiB but I don’t think that’s a magic number, the size should be a multiple of 4MiB). For restoring, make partitions with gparted or your weapon of choice. The OSMC install image starts the FAT32 boot partition at sector 2048 (1MiB) but there are good reasons for starting it at 4MiB. It does not seem necessary to preserve the UUIDs of either partition as you would if using a boot manager. The FAT partition must be flagged as boot [edit: no, see below]. The linux partition takes up all the available space after that.

Mount the formatted card and just copy (cp -a) all the files back into the two partitions.