Dev/Embedded/Rootfs/Backup

From Embeded Linux (and more) Wiki by Nathael
< Dev‎ | Embedded‎ | Rootfs
Revision as of 15:33, 16 October 2019 by Drizzt (talk | contribs) (Created page with "== Do NOT use dd == First of all, avoid any site which tells you to use ''dd'' to backup or clone your system, as it's author either does not want to take some time to explain...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Do NOT use dd

First of all, avoid any site which tells you to use dd to backup or clone your system, as it's author either does not want to take some time to explain how things work, or simply doesn't know.
When using dd :

  • you won't be able to extract the content to explore it on your development system
  • you will not be able to change the partition sizes or layout when creating a copy
  • you will not be able to change the filesystem type
  • even if you compress the result, extracting it will require as much space as your SD card had, using 4GB (or 8, or 16 ?), and no way to extract to a smaller µSD card or you may loose data and get a broken filesystem.
  • and you will also copy any junk found on the SD card, as the empty space is not necessarily filled with zeroes, making a bigger image than required, even when compressed !!

The right way to do it is to use tar.

Rootfs (and more) Backup (or copy part 1)

Simple case, with complete tar tool

For each partition you need to backup, you must run one of the following tar commands :

  • Store the archive on a separate local partition :
tar --one-file-system cjf /somewhere/else/backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2 *
  • Combine tar and ssh to directly transfer the archive to your host system :
tar --one-file-system cjf - * | ssh user@devhost "cat > backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2"

Or with compression on the host system :

tar --one-file-system cf - * | ssh user@devhost "bzip2 | cat > backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2"

You can of course chose the backup image name according to your preferences, this is only an example.

It is then a good idea to create a README file to store with your archive with indications of partitions, sizes, filesystems, labels, bootable flag and so on (The output of sfdisk may be useful). If your system requires more than one archive you can then place them together with the README file in the same directory, and even create an archive of this directory :)

Limited tar tool (Busybox)

If your tar does not have the "--one-file-system" option (busybox one for example) you must mount each partition you need to backup on an empty directory (possibly with bind mount option), and then create an archive :

mntpoint=/mnt/tmp
mkdir ${mntpoint}
disk=mmcblk0  # Use the right disk name (sda, sdb, ... mmcblk1, ...)
partition=p1  # Use the right partition indicator (p1, p2, p3, ... for mmcblk* or simply 1, 2, 3, ... for sd*)
mount /dev/${disk}${partition} ${mntpoint}
cd ${mntpoint}
tar cjf /somewhere/else/backup_${disk}${partition}_$(date +%Y-%m-%d).tar.bz2 *
cd && umount ${mntpoint}

Mount "bind" alternative

An alternative to specifying the device you want to backup (/dev/***), you can perform a "bind mount" and specify the directory (for example the system root directory "/") :

mount -o bind / /mnt/tmp

This will bind only the filesystem mounted on "/", and not any other filesystem mounted for example on /proc, /sys or /dev.

Multiple mount option

Note that by using the mount solution you can combine different filesystems in a single archive. Let's say that your home directory is on a separate partition, then you can combine both by doing so :

mount -o bind / /mnt/tmp
mount -o bind /home /mnt/tmp/home
cd /mnt/tmp && tar cf - * | ssh user@devhost "bzip2 | cat > backup_full.tar.bz2"

Rootfs restoration (or copy part 2)

  • Start by partitioning your disk / SD card / whatever storage support. My preferred tool for this is cfdisk, but any other partitioning tool should do the job :)
cfdisk /dev/***
  • Then create the filesystem
mkfs.*** /dev/*** [options]
  • Mount the partition somewhere
mount /dev/*** /somewhere
  • And finally decompress your archive
cd /somewhere && tar xf /somewhere/else/archive.tar.bz2
  • Do not forget to umount
cd - && umount /somewhere