System backup

Tutorials >
On this page I take a quick look at the main tools we can use to backup a linux system.

  1. Rsync     a powerful copy tool that preserves file attributes.
  2. Tar     an archive packer that can utilise powerful compression programs.
  3. FSArchiver     an archive packer with checksum safety.
  4. Partclone     a partition cloning tool and Clonezilla, a gui frontend.
  5. Penguins eggs     a live ISO creator.




Rsync

There are several good ways to backup a Linux system, and I'll start with the simplest, rsync.

Using an rsync command, like this one below, could be very useful, as you could make use of its update option, -u, that would let you run it frequently, subsequent runs sending less data to the target file system than the initial sync. It won't compress though (unless you pipe it through gzip etc. to an archive), but you just need to afford some space for the backup (like a spare partition).

 



Tar command

The first one I ever heard of was the tar archive command, that can compress using gzip, bzip2, zstd or plain xz.

It's easy to make a tar file of all system and user files with something like
sudu tar {tar options} {location for tar file} {excludes} {dir to backup}

like this,,,
 

Or like this, to be more succinct, one for system only, excluding user's home...
 

And one for home (config files)...
 

The quotes around each exclude item above are very vital. Also, I add `2> error.log` at the end to catch the error output to a file, as there is too much on the terminal to scroll up and find any errors, in case tar aborts.

It's a good idea to exclude $HOME/.cache or clean your web browser's cache before backup (e.g with bleachbit).

Compression options: -j bzip2, -J xz, -z gzip, -a auto detect from file extension,

without any of these options tar just makes one uncompressed file of everything.

Another option is -u which updates the tar file, making changes, not sending everything all over again (but files are not removed only new ones appended).

Adding the option --one-file-system will exclude files from other partitions, e.g. for backup of system only, but check to see if needed system directories are not included, such as /dev and /sys (even if the contents of these is meant to be excluded).


Test the tar ball integrity with:
 

A good archive will give no errors.

Or for xz, send content list to a file..
 


Cpu threading

To compress utilising multi-core cpu threads, you will need to install a couple of extra packages for gzip and bzip2:
install pigz for gzip and run with the option: -I pigz -cvpf
install pbzip2 for bzip2 and run with the option: -I pbzip2 -cvpf
(the -I option is a capital i)

With xz compression, set the variable XZ_OPT=-T0 (0 sets max available threads) and run it with the option J, like -cvJpf (see below scripts).
and Oh, is it fast on an 8 core 16 thread box! almost unbelievable compared to the old days.
Use -1 to -6 for increasing compression level, or -9 for max and slowest... XZ_OPT="-T0 -9"

Note that xz may result in larger files but the archives it makes are more robust and hence good for system backups.

Or you can compress with Zstd (install zstd) and run it with: ZSTD_CLEVEL=19 ZSTD_NBTHREADS=0 tar -I zstd -cvpf ...
Zstd is ultra fast (even mind-blowing!), although its file sizes are about 20% larger than what XZ makes.

I scripted the process for ease - check out my two Tar scripts, for system and home backup.





Test the xz tar ball integrity with:
 
Or
 

Decompress an archive made with xz threading with...
 

Or with zstd with...
 


Update option

If you leave out the date-time parameter so that the file name is the same with each run of the script, and you add the update option 'u', then you'll be able to keep a copy of the system in a tar archive, and just keep updating it. After that you compress it and rename it with the time stamp.

Note that tar will append files to the archive, not replace them inline. So it's probably better to create a new archive each time.


Restoring the archive

To restore the system from a backup tar file run..

 

The -C option changes to DIR before performing the operation.
WARNING ! this command will overwrite every file on specified target with the files from the backup tar file!

  • In a Live CD environment (or another running system) make three partitions, system (e.g. sda1), home (e.g. sda2) and a temp store area (e.g. sda5), and swap if necessary (if there's no other Linux system installed)
  • copy the archives to the temp area and run the restore process, creating a new user dir (eg. bobdevuan)

The preparation..
 

The restore operations..
 


Note: if restoring to a different machine, you'll need to amend the /etc/fstab file, so that the system finds its disks, and update Grub, either by chrooting into the system and running update-grub or using Boot restore disk. If it's an extra install on a box with a running system, just run update-grub on that box to find and add the new one.

You'll also need to add the user bobdevuan to the system so that you can login, with adduser bobdevuan (in chroot), or try to boot and login as root and run adduser from root, logout of root and in again as bobdevuan. If you fail to restore home before booting up the new system, just run adduser and then unpack the home backup to a tmp dir and rsync all the files (including dot files) into the new user's home dir.


Check out my two Tar restore scripts for system and home archive restore!!





FSArchiver

This is a file system archiver, as the name suggests, with checksums built in for safety, to prevent file corruption... fsarchiver.org

Best to install it via `sudo apt-install fsarchiver`
It has a frontend called fsarchiver_dialog, http://zolli.altervista.org/fsa_dial/

Or you can just type fsarchiver in a terminal and it will give you the commands to use (or better, do man fsarchiver)

The basic usage is:
sudu fsarchiver savefs {options} {location for archive} {target partition} {excludes}

Also restfs: restore filesystems, savedir: save directories, restdir: restore data from an archive, archinfo: show information

For example...
 

Where -A works on a mounted system, -v verbose, -o overwrites previous, -j is cpu threads, -Z is zstd compression (over 20 means high mem usage, 22 is max).


Check out my two fsarchiver scripts for system and home backup.



And after that, check out my Backup beast script which has options for both Tar and fsarchiver, system, home or dir backup!!





Test the fsarchiver file with..
 


Restore an archive made of a file system with..
 

And to format the partition at the same time (I didn't test it yet)..
 

Or, to unpack an archive of files...
 


Check out my two Fsarchiver restore scripts, for system and home restore!!



And after that, check out my other beast, the one and only Backup beast restore script which has options for both Tar and fsarchiver, system, home or dir restore!!





Partclone

Use this to image any disk, and restore the image to another box (make an exact clone).  You need to use this tool from a live CD (e.g. gparted or your own with partclone installed).

more details here https://wiki.debian.org/Backup/Clone

and here https://wiki.archlinux.org/index.php/Partclone
 

to clone a partition with compression:
 

to restore the image to a partition (with great care!):
 

As this tool images whole partitions, without any excludes, you better tidy up the disk especially in user home.

Note: the image can only be restored to a partition equal or larger than the source partition. I tried the option -C to skip checking part space (used if we know that all the data could fit) but the result was a fail.



Clonezilla

http://clonezilla.org/

This is the GUI partclone tool. see /how-to-use-clonezilla-tutorial/

They recommend tuxboot (find the .deb here) to install the clonezilla iso to USB.


Making an image

  • 1st screen hit enter
  • select lang, keybd, and start CZ
  • select device-image
  • select type of image dest - local-dev for a disk or USB device
  • select disk (to mount on CZ's /home/partimag/)
  • select dir of image
  • select beginner/expert
  • select savedisk/saveparts
  • enter image name
  • select source (disk/parts) to image
  • check and run


Restore an Image

  • Go through like above, select image location as before
  • select restoredisk/restoreparts
  • select image to use
  • select the target disk/parts
  • check and run

Note: restoring an image from e.g. a 20Gb hdd source onto a 50Gb target, might render the target as a 20Gb disk - gparted will be needed in that case to expand the part to fill the 50Gb.


Make a Live USB

Clonezilla can make a Live CD iso of your cloned image, see here

you need an image already
  • go through the steps above to restore an image, but after select beginner,
  • select recovery-iso-zip
  • select image
  • select target for restoring from the iso! - change to "ask_user"
  • select lang, keymap
  • select iso or zip


Burn the USB

try unetbootin or tuxboot with iso file

or use the zip file with a command like:
 
and make it bootable with:
 



Penguins eggs

So, time has moved on and the great Remastersys has not been maintained for many years (andLinux Respin only good for Ubuntu). So then comes along Piero Proietti's Penguins eggs, which can create a Live ISO from your running system - very handy to take around and boot on any PC when you are away from the office, or to install your spin or a distributable spin that you put together for people.

To install, you can follow the instructions on the Eggs site or download a .deb from here.

The dependencies I needed were quite a few, 40 of them including the secondary dependencies of these...
cryptsetup cryptsetup-bin cryptsetup-initramfs grub-efi-amd64-bin ipxe jq live-boot-doc live-tools lvm2 nodejs pxelinux shim-signed

Try to sort them out with
 
Edit the configuration file...
 
and the excludes file...
 

You might want to unhash the var cache lines and also hash /etc/fstab (will need to edit after install) and /etc/apt
There is another file called home.list, and two others, usr.list and var.list

I add these
 


And then run it with
 

Adding --clone after the command above includes your user data (non-encrypted).
Whereas --cryptedclone includes your user data (encrypted and not accessible to the Live session).
To include a gui installer in the built ISO, add the option `calamares` after `eggs`.

Make a Live USB.... check out Ventoy on my USB installer page.