How to Create Backups with Fsarchiver on Linux

Fsarchiver is a free software utility that let us create file-level backups of one or multiple filesystems in a single archive. One big advantage of this kind of backup is that we can restore it on a filesystem smaller than the original one (but of course large enough to contain all the files); this is usually impossible when performing block-level backups, using tools like partclone or dd. In this article we will learn how to install and use the application and its main features.

In this tutorial you will learn:

  • How to create a backup using fsarchiver
  • How to run fsarchiver in multi-threaded mode
  • How to backup multiple filesystems with a single command
  • How to encrypt a backup
  • How to inspect a backup archive
  • How to restore a backup
How to Create Backups with Fsarchiver on Linux

How to Create Backups with Fsarchiver on Linux

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution independent
Software The fsarchiver utility
Other Root permissions to create and restore the backups
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Fsarchiver installation



Installing fsarchiver is very easy. The application is packaged and available in the repositories of the most used Linux distributions, ready to be installed. The latest version of the program is 0.8.5; to install it on Fedora we run the following command:

$ sudo dnf install fsarchiver

On Debian and derivatives, depending on our preferences, we can use aptitude or apt-get to install the package; The apt utility is basically a wrapper around the latter (and apt-cache):

$ sudo apt install fsarchiver

Fsarchiver is available in the Extra repository of the ArchLinux distribution; we can use pacman to install it:

$ sudo pacman -S fsarchiver

Creating a backup



First of all, before creating a backup of a filesystem we must be sure that it is not mounted, or at least it is mounted in read-only mode. If the appropriate option is provided, fsarchiver is able to work also on mounted filesystems, but if we want our backup to be consistent, we should absolutely avoid that scenario: If we use lvm logical volumes we can create a snapshot of the filesystem and run fsarchiver on it, otherwise we should create the backup from a “live” environment.

Fsarchiver supports several filesystem types, like ext4, ext3, xfs, btrfs, reiserfs. The application can run in multi-threaded mode and is set, by default, to preserve standard and extended file attributes as the ones used by SELinux and ACL (Access control list). The files contained in the archive can be verified by checksum.

To create an archive we need to use the savefs subcommand and specify the name of the archive which will be created and the source filesystem we want to backup. To backup an ext4 filesystem, on the /dev/sda3 device we would run:

$ sudo fsarchiver savefs -v /path/to/backup.fsa /dev/sda3

The syntax of the program is very simple. The only option we provided in the example above is -v: this will cause the program to run in verbose mode, and we will be able to see the operation performed on every file and its progress.

As the first argument of the command, we specified the path of the backup archive and used the .fsa suffix after the its name. The use of the suffix is arbitrary: it is just a convention, but can be useful to easily remember how the file was created.

Once the command is launched, the first thing the program will do is to analyze the filsystem. It will then proceed with the actual backup operation, as we can see from the output:

Analysing filesystem on /dev/sda3...
====================== archiving filesystem /dev/sda3 ======================
-[00][  0%][DIR     ] /
-[00][  0%][DIR     ] /lost+found
-[00][  0%][DIR     ] /egdoc
-[00][  0%][DIR     ] /egdoc/Templates
-[00][  0%][DIR     ] /egdoc/Documents
-[00][  0%][DIR     ] /egdoc/Music
[...]


Storing multiple filesystems in the same archive

One nice feature of fsarchiver is the ability to store multiple filesystems in the same archive. If something bad happens and we need to restore from the backup, all we have to do is to reference the filesystem by its index inside the archive. To backup multiple filesystems at once we simply specify them one after the other:

$ sudo fsarchive savefs -v /path/to/backup.fsa /dev/sda2 /dev/sda3

Using compression

We almost always want our backup archive to be compressed, in order to save space. Fsarchiver supports two option to manage compression:

  • -z (–compress)
  • -Z (–zstd)

Both these options needs an argument which specifies the compression level. The range of available levels varies depending on what option we use. Let’s see what are the difference between them.



Compression with the -z option

The --compress option (-z) accepts a compression level from 0 to 9. Each level correspond to a compression algorithm used with a specific mode:

Level Meaning
0 Use the lz4 algorithm
1 Use lzo with compression level -3
2 Use gzip with compression level -3
3 Use gzip with compression level -6
4 Use gzip with compression level -9
5 Use bzip2 algorithm with level -2
6 Use bzip2 algorithm with level -5
7 Use lzma with compression level -1
8 Use lzma with compression level -6
9 Use lzma with compression level -9

The higher value we provide to the -z option, the better compression we will obtain, at the cost of an increased time and memory usage. Smaller values, instead will create larger archives in less time, with less resource usage.



Compression with the -Z option

The other option we can use to specify a compression level with fsarchiver is --zstd. This option accepts a value range from 0 to 22. The level passed to the option will not determine what algorithm is used, but it will be simply used as the zstd compression level.

Exclude files from the backup

In some cases we may want to exclude certain files from the backup, for various reason. When creating a backup of the system root partition, for example, we want to exclude the so called pseudo-filesystems like /dev and /proc, which are representations of devices and processes managed by the kernel. To exclude files from a backup we must use the -e option (short for --exclude), and provide an exclusion pattern. The files and directories matching that pattern will be excluded from the backup.

Say for example we want to exclude any file with the “.bk” extension from our backup, we would run:

$ sudo fsarchiver savefs -v /path/to/backup.fsa /dev/sda3 --exclude="*.bk"

Running in multi-threaded mode

As already mentioned above, fsarchiver, can create or restore backups in multi-threaded mode on machines with multiple CPU cores. The option which controls this feature is -j (--jobs): it takes the number of threads to use as argument. The recommended value is usually equal to the number of available logical processors – 1. Running in multi-threaded mode has the obvious advantage of using more processing power for the backup or restore task, which will be completed in less time.



Encrypting the backup archive

Fsarchiver has the ability to encrypt data saved in the backup archive. To make use of this feature we must use the -c option or its long form: --cryptpass, and pass the encryption password as argument. The password can be from 6 to 64 character long or a single dash character (-). If the latter is provided a prompt will be displayed for the user to enter the password interactively:

$ sudo fsarchiver savefs -v /path/to/backup.fsa /dev/sda3 --cryptpass -
Enter password:
Confirm password:
Analysing filesystem on /dev/sda3...
[...]

The same option and password must be used when restoring the backup.

Inspect an existing archive

Until now we saw how to create a backup archive. Once an archive is created we can inspect its content using the fsarchiver dedicated subcommand: archinfo. All we must do is to pass the path of the archive as argument. Following our previous example, we would run:

$ fsarchiver archinfo /path/to/backup.fsa

The output of the command would be something like the following:

====================== archive information ======================
Archive type:           filesystems
Filesystems count:      2
Archive id:             5e7934e4
Archive file format:        FsArCh_002
Archive created with:       0.8.5
Archive creation date:      2020-03-20_19-51-05
Archive label:          <none>
Minimum fsarchiver version: 0.6.4.0
Compression level:      8 (zstd level 8)
Encryption algorithm:       none

===================== filesystem information ====================
Filesystem id in archive:   0
Filesystem format:      ext4
Filesystem label:
Filesystem uuid:        69d250a7-16d0-47fd-8ca2-6513d32c1e5a
Original device:        /dev/sda2
Original filesystem size:   34.20 GB (36722737152 bytes)
Space used in filesystem:   6.15 GB (6608547840 bytes)

===================== filesystem information ====================
Filesystem id in archive:   1
Filesystem format:      ext4
Filesystem label:
Filesystem uuid:        ec7d21e9-56b8-4fef-abc7-d9da2a4ad45c
Original device:        /dev/sda3
Original filesystem size:   14.70 GB (15786254336 bytes)
Space used in filesystem:   3.29 GB (3536240640 bytes)


In the first section of the output we find general information about the archive, as the creation date and compression level; after it, we can retrieve information for each filesystem saved in the archive. Among the other things we can see the numeric id assigned to the filesystem, its UUID, its format (ext4 in this case), the path of the original device, the filesystem total size, and the space used on it. Being able to identify the filesystems by id is necessary to restore a backup: we will see how to do that in the next section.

Restoring a backup

To restore a backup created with fsarchiver, we must use the restfs subcommand. We provide the path of the archive backup as first argument, the archive id of the filesystem we want to restore, and the destination device. Following the above example to restore the backup we made for the filesystem on the /dev/sda2 device we would run:

$ sudo fsarchiver restfs /path/to/backup.fsa id=0,dest=/dev/sda2

To restore multiple backups to their respective destination we only need to repeat the id,dest pattern:

$ sudo fsarchiver restfs /path/to/backup.fsa id=0,dest=/dev/sda2 id=1,dest=/dev/sda3

With the command above we would restore the first backup in the archive to the /dev/sda2 partition, and the second to /dev/sda3.

A very important thing to notice is that on restoring, fsarchiver not only does restore the files, but also recreates the original filesystem. This is very useful, because, for example, eliminates the need to adjust the /etc/fstab file, which normally should be updated with the new filesystems UUID.

Conclusions

There are many open source backup solutions on Linux; in this article we talked about fsarchiver. We saw ho to install the program in the most common Linux distributions, and how to use it to create a backup which optionally can also be encrypted. We saw how to inspect the backup archive and how to restore the filesystems contained in it. This is the fsarchiver, homepage. Take a look at it if you want to know more about the program, it’s definitely worth it!



Comments and Discussions
Linux Forum