Dev/Embedded/Rootfs/Backup: Difference between revisions

From Embeded Linux (and more) Wiki by Nathael
< Dev‎ | Embedded‎ | Rootfs
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
* you will not be able to change the partition sizes or layout when creating a copy
* 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
* you will not be able to change the filesystem type
* you will get the same partition's and filesystem's  UUID (!!!) on each SD card (or whatever) you create with your backup (might save you if your bootloader config is bloated and looks for a specific UUID to find your root partition, use partition labels instead !)
* 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.
* 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 !!
* 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 !!
Line 49: Line 50:
* 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 :)
* 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/***
  cfdisk /dev/***
Note that it's also possible to "dump" (or "export") the partitioning structure to a file using sfdisk
sfdisk -d /dev/*** > partitions.list
And then use it to create the partition tables of a new device, once again with sfdisk :
sfdisk /dev/*** < partitions.list
* Then create the filesystem
* Then create the filesystem
  mkfs.*** /dev/*** [options]
  mkfs.*** /dev/*** [options]
...
* Mount the partition somewhere
* Mount the partition somewhere
  mount /dev/*** /somewhere
  mount /dev/*** /somewhere

Latest revision as of 23:30, 8 April 2023

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 easily 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
  • you will get the same partition's and filesystem's UUID (!!!) on each SD card (or whatever) you create with your backup (might save you if your bootloader config is bloated and looks for a specific UUID to find your root partition, use partition labels instead !)
  • 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/***) is to 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 in sub-directories, 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/***

Note that it's also possible to "dump" (or "export") the partitioning structure to a file using sfdisk

sfdisk -d /dev/*** > partitions.list

And then use it to create the partition tables of a new device, once again with sfdisk :

sfdisk /dev/*** < partitions.list
  • 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