How to backup and restore an xfs filesystem using xfsdump and xfsrestore

XFS is a journaled filesystem originally developed by Silicon Graphics in 1993; it was released under the GPL license in the year 2000 and ported to  the Linux kernel in 2001. Due to its high scalability and performances, XFS became the default filesystem in recent versions of Red Hat Enterprise Linux and its clones.

In this tutorial we see how to create and restore XFS backups using the xfsdump and xfsrestore utilities on Linux.

In this tutorial you will learn:

  • How to create and restore full and incremental backups of an XFS filesystem
  • How to exclude/include only a subset of files in a backup
  • How to list files included in a backup
How to backup and restore an xfs filesystem with xfsbackup and xfsrestore
How to backup and restore an XFS filesystem with xfsbackup and xfsrestore

 

Software requirements and conventions used
Category Requirements, Conventions or Software Version Used
System Distribution independent
Software xfsdump, xfsrestore
Other None
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

Installation

The xfsdump and xfsrestore utilities are packaged and included in the official repositories of all the major Linux distributions, therefore we can simply install it with their respective package managers. On distribution which are part of the Red Hat family, such as Fedora or Rocky Linux, for example, we can perform the installation with dnf. Both utilities are included in the “xfsdump” package:

$ sudo dnf install xfsdump

On Debian and Debian-based distributions, instead, we can  use apt:

$ sudo apt install xfsdump

If we are using Archlinux, instead, we can perform the same operation using pacman:

$ sudo pacman -S xfsdump

Once the installation is performed, we can create our first backup. Let’s see how.

Creating a backup with xfsdump

Xfsdump can save backups as files or write to tape devices. In this article we will explore the former approach. Creating a backup of an XFS filesystem with xfsdump is quite simple. In the most basic case we invoke the utility and provide the mountpoint of the filesystem we want to backup (the filesystem must be mounted) and the backup destination. The latter is specified via the -f option, the former is passed as argument to the command.



Suppose we want to create a backup of the boot filesystem, mounted at /boot, to the /data/boot.xfsdumpfile (name and extensions are arbitrary). With administrative privileges, we would launch the following command:

$ sudo xfsdump -f /data/boot.xfsdump /boot

As soon as we issue the command, we will be prompted to provide a label to identify the dump session, and another one to identify the media we are performing the backup on. Labels are recorded in the backup inventory (more on this later), and are especially useful when creating backups on tape drives. For the sake of this example we just use the “session_0” label to identify our session and datapart as media label:

 ============================= dump label dialog ==============================

please enter label for this dump session (timeout in 300 sec)
 -> session_0

============================ media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> datapart

To make the task non-interactive, we can simply provide the session and media labels as arguments to the -Land -M options, respectively:

$ sudo xfsdump -L session_0 -M datapart -f /data/boot.xfsdump /boot

Creating incremental backups

The command we saw in the previous examples produces a full or level 0 backup, which includes all the files in the filesystem. By using xfsdump, however, we can also create incremental backups. An incremental backup includes only changes occurred since the previous backup (full or incremental itself) was performed. For this reason they are usually performed very quickly.

To create an incremental backup, all we have to do is to specify the backup level (0-9) by passing it as argument to the -l option when invoking xfsdump. If the option is omitted, as we saw, a level 0 backup is created. To perform a level 1 backup, we would run:

$ sudo xfsdump -l 1 -L session_1 -M datapart -f /data/boot.xfsdump1 /boot

The level 1 backup is performed against the last level 0 backup recorded in the inventory. It goes by itself that backup levels must be incremented gradually. We can’t, for example, create a level 3 backup if a level 2 backup doesn’t exist.

Excluding files from a backup

Sometimes we may want to include only a subset of files in our backups. In order to achieve our goal we can basically use three strategies:

  1. Exclude files over a specific size
  2. Exclude files tagged with the “no dump” attribute.
  3. Include only files contained in a specific directory

Excluding files bigger than a specific size

To exclude files which exceed a specific size from the filesystem backup, we use the -z option, and pass the size threshold expressed in kilobytes as argument. With the following command we exclude files bigger than 10 Megabytes (10000 Kilobytes):

$ sudo xfsrestore -t -f /data/boot.xfsdump

Excluding arbitrary files

To exclude arbitrary files from the filesystem backup, instead, we must tag them with the no dump attribute. As we discussed in our tutorial about managing filesystem attributes on Linux, we can perform such task by using the chattr utility. Suppose, for example, we want to exclude the /boot/excludeme file from our backup. To set the “no dump” attribute on it, we would run:

$ sudo chattr +d /boot/excludeme



The “no dump” attribute is identified by a d. The “+” sign in front of it we used in the command, means we want to activate the attribute. Now, to exclude tagged files from a backup, all we have to do is to invoke xfsdump with the -e option:

$ sudo xfsdump -e -f /data/boot.xfsdump /boot

Including only files in a specific directory

In order to include only files contained in specific directories in our backup, we use the -s option. For each directory we want to include we must repeat the option, and pass the path of the directory as argument. The path of the directory must be relative to the filesystem mountpoint. Just as an example, to include only the /boot/efi directory and its content in our backup, we would run:

$ sudo xfsdump -s efi -f /data/boot/xfsdump /boot

Restoring a backup with xfsrestore

The xfsrestore utility is used to restore backups created with xfsdump. When invoked with the-I option, xfsdump displays the backups available in the inventory, which is saved in the /var/lib/xfsdump/inventory directory. As you can see below, the output produced by the command includes the backups we performed in the previous examples:

$ xfsrestore -I
file system 0:
        fs id:          e65721d2-d766-4556-85e2-60c36022aea7
        session 0:
                mount point:    localhost.localdomain:/boot
                device:         localhost.localdomain:/dev/vda1
                time:           Wed Nov 23 10:49:56 2022
                session label:  "session_0"
                session id:     8e7bf8a3-ef36-4dbe-ae3c-1b31e6361596
                level:          0
                resumed:        NO
                subtree:        NO
                streams:        1
                stream 0:
                        pathname:       /data/boot.xfsdump
                        start:          ino 133 offset 0
                        end:            ino 1573039 offset 0
                        interrupted:    NO
                        media files:    1
                        media file 0:
                                mfile index:    0
                                mfile type:     data
                                mfile size:     164174336
                                mfile start:    ino 133 offset 0
                                mfile end:      ino 1573039 offset 0
                                media label:    "datapart"
                                media id:       82475520-22d8-40e7-9304-04136f90f680
        session 1:
                mount point:    localhost.localdomain:/boot
                device:         localhost.localdomain:/dev/vda1
                time:           Wed Nov 23 10:52:06 2022
                session label:  "session_1"
                session id:     2d0697d1-9058-4d42-a6b9-5981402013ce
                level:          1
                resumed:        NO
                subtree:        NO
                streams:        1
                stream 0:
                        pathname:       /data/boot.xfsdump1
                        start:          ino 0 offset 0
                        end:            ino 1 offset 0
                        interrupted:    NO
                        media files:    1
                        media file 0:
                                mfile index:    0
                                mfile type:     data
                                mfile size:     20736
                                mfile start:    ino 0 offset 0
                                mfile end:      ino 1 offset 0
                                media label:    "datapart"
                                media id:       2b2414ca-8f16-46bf-bfd7-016220aff8c3
xfsrestore: Restore Status: SUCCESS

Listing the content of a backup

Before proceeding to restore a backup we may want to take a look at its content. To perform such action, we invoke xfsdump with the -t option, and, as usual, we reference the backup by passing its path as argument to -f. To list the content of the backup saved as /data/boot.xfsdump, we would run:

$ xfsrestore -t -f /data/boot.xfsdump

Restoring a full backup

To restore a full backup, we invoke xfsdump and specify the path to the backup and the location we want to restore the filesystem to. For example, to restore the full backup we performed in this article to the /test directory, we would run:

$ sudo xfsrestore -f /data/boot.xfsdump /test



Once the operation is completed, we can see the /test directory contains the files which were part of the backup. As you can see, extended file attributes, as the SELinux context, are preserved:

$ ls -Z /test
      system_u:object_r:boot_t:s0 config-5.14.0-70.13.1.el9_0.x86_64
      system_u:object_r:boot_t:s0 efi
      system_u:object_r:boot_t:s0 grub2
      system_u:object_r:boot_t:s0 initramfs-0-rescue-f533a7d261374b28b305da65ada0f15f.img
      system_u:object_r:boot_t:s0 initramfs-5.14.0-70.13.1.el9_0.x86_64.img
      system_u:object_r:boot_t:s0 initramfs-5.14.0-70.13.1.el9_0.x86_64kdump.img
      system_u:object_r:boot_t:s0 loader
      system_u:object_r:boot_t:s0 symvers-5.14.0-70.13.1.el9_0.x86_64.gz
system_u:object_r:system_map_t:s0 System.map-5.14.0-70.13.1.el9_0.x86_64
      system_u:object_r:boot_t:s0 vmlinuz-0-rescue-f533a7d261374b28b305da65ada0f15f
      system_u:object_r:boot_t:s0 vmlinuz-5.14.0-70.13.1.el9_0.x86_64

Restoring an incremental backup

Restoring an incremental backup requires the restoration of all backups performed before it, starting from the last full backup. When using this strategy, each time we restore a backup, we must use the -r option, which enables the so called “cumulative” mode. Suppose, for example, we want to restore the state of the filesystem as it was when we performed the first incremental backup (level 1). As a first thing we restore the level 0 or full backup, this time adding -r to the command:

$ sudo xfsrestore -r -f /data/boot.xfsdump /test

Than, we proceed to restore the incremental, level 1 backup:

$ sudo xfsrestore -r -f /data/boot.xfsdump1 /test

Conclusions

In this tutorial we learned how to create and restore XFS filesystems backups by using the xfsdump and xfsrestore utilities. We saw how to create full and incremental backups, how to proceed when we want to restore them, and how to exclude/include only a subset of files in our backups.



Comments and Discussions
Linux Forum