How To Backup and Restore an ext4 Filesystem

By | 2019-05-18

While they aren’t always installed by default, the programs dump and restore are great tools for backing up and restoring ext4 filesystems. They are flexible and easy to use.

dump/restore features:

  • Support for full, differential, and incremental backups
  • Can read from and write to standard input and standard ouput
  • When writing to removable media, it can span multiple volumes
  • Can backup part of a filesystem
  • Remote backups

Backups

Backups are created with the dump command. In most cases, it needs the following three things:

  1. Dump Level – The dump level is an integer used to determine whether it is a full, differential, or incremental backup. The default is a level 0, or full backup.
  2. Destination File – The path to the file to write the backup to. This can be a regular file, device file, or a - for standard output. If no file is specified, dump tries to use /dev/tape
  3. Files to Dump – The path to the file(s) or directory(ies) you wish to backup. You can specify a block device if it contains an ext2/3/4 filesystem.

As you can see, the only mandatory argument is what you want to backup. In practice you will probably at least specify the output file.

Creating a Full Backup

To create a full backup of a filesystem, create a level 0 dump. The example below demonstrates creating a full backup of /boot to the dump file /backups/boot.0.

dump -0uf /backups/boot.0 /boot

The following table describes the meaning of the options.

Option Explanation
0 An integer that is part of the options is used as the dump level. Historically only 0-9 are supported. Newer versions can go higher. Level 0 is the default.
u This instructs dump to update /var/lib/dumpdates with the time, source, and level of this backup. This file is read when performing incremental or differential backups to determine which files need to be included.
f The path to the file to write the dump to. Use a - for standard output.

Partial Backup

As mentioned earlier, you are not limited to backing up a whole filesystem, you can backup subdirectories that you specify. There is a catch– with a partial backup, you are limited to dump level 0.

To backup only part or parts of a filesystem, pass dump a list of directories you wish to backup. Here is an example of a full backup of a few key configuration directories on my Debian desktop:

dump 0f /root/configs.dump /etc/default /etc/network

Notice how I left out the -? This is actually fine. dump and restore do not require it when you pass them your options.

Viewing a Backup File Contents

If you do regular backups, especially incremental or differential backups, you may not know which backup file something you wish to restore is in. You can view the contents of a dump file with restore -tf and, optionally, a list of file names. If you omit a list of file names, the entire contents of the file will be output to your terminal.

Here are the contents of two dump files I made on my example system:

# restore -tf /dumps/stuff.0
Dump   date: Wed May 15 04:52:58 2019
Dumped from: the epoch
Level 0 dump of /stuff on dump:/dev/sdb1
Label: none
         2	.
        11	./lost+found
        12	./0
# restore -tf /dumps/stuff.1
Dump   date: Wed May 15 04:54:52 2019
Dumped from: Wed May 15 04:52:58 2019
Level 1 dump of /stuff on dump:/dev/sdb1
Label: none
         2	.
        13	./1

The first one has files and directories named ., ./lost+found, and ./0. The second contains just . and ./1.

Here I specify a list of files:

# restore -tf /dumps/stuff.0 0 1
Dump   date: Wed May 15 04:52:58 2019
Dumped from: the epoch
Level 0 dump of /stuff on dump:/dev/sdb1
Label: none
        12	./0
./1 is not on the tape

In the last example, the file 0 is in the dump file, but 1 is not. Don’t worry about it if dump and restore mention tapes and you aren’t using a tape drive. If this happens, as it does in the example, they are probably working fine– they were originally written in the times when tapes were the only economical way to store backups.

Incremental and Differential Backups

This is where the flexibility of dump and restore starts to shine. The type of backup depends entirely on the dump level. This means the only difference in the command used for a full, incremental, or differential backup is the dump level.

As with any backup rotation, you will create a full backup (dump level 0, remember) first. To create an incremental or differential backup, you create a higher level dump. The dump program backs up the changes, and only the changes since the most recent dump of a lower level. That is all there is to it.

In the examples below, I am going to create 5 backups, in the order listed, with the levels 0, 1, 2, 1, and 2. These will be a full, incremental, incremental, differential, and incremental backups, respectively.

echo 0 > 0
# dump 0uf /dumps/stuff.0 /stuff 2> /dev/null
# echo 1 > 1
# dump 1uf /dumps/stuff.1 /stuff 2> /dev/null
# echo 2 > 2
# dump 2uf /dumps/stuff.2 /stuff 2> /dev/null
# echo 3 > 3
# dump 1uf /dumps/stuff.1-1 /stuff 2> /dev/null
# echo 4 > 4
# dump 2uf /dumps/stuff.2-1 /stuff 2> /dev/null

If you are new to Linux and UNIX, 2> /dev/null discards all output to standard error. Normally, dump and restore output messages to standard error, but I chose to omit them for brevity. In practice, DON’T do this. If you discard standard error, you will not know if a backup or restore is successful or not.

Here I list the contents of these files:

# restore -tf stuff.0 | tail -n +5
         2	.
    262145	./lost+found
        11	./0
# restore -tf stuff.1 | tail -n +5
         2	.
        12	./1
# root@dump:/dumps# restore -tf stuff.2 | tail -n +5
         2	.
        13	./2
# root@dump:/dumps# restore -tf stuff.1-1 | tail -n +5
         2	.
        12	./1
        13	./2
        14	./3
# root@dump:/dumps# restore -tf stuff.2-1 | tail -n +5
         2	.
        15	./4

In case you don’t know, | tail -n +5 cuts off the first 5 lines of output. In practice, you can leave this out.

Again, dump backs up all changes since the last lower dump level, including which files have been removed.

Restoring From Backups

As you probably figured out, you use restore to restore a backup. A full restore from a full backup is pretty straightforward. It is possible to restore just a few files. That will be covered in the next section.

To do a full restore, follow these steps:

  1. Unmount the filesystem.
  2. Format the applicable device.
  3. Mount the device.
  4. Change directories to the mount point.
  5. Restore the backup file(s) in order of dump level.

Let’s restore using the files I created in the section on incremental and differential backups.

In the examples, I use a file system on partition /dev/sdb1 mounted on /stuff.

# cd /dumps
# umount /stuff
# mkfs.ext4 /dev/sdb1
# mount /dev/sdb1 /stuff
# cd /stuff
# restore -rf /dumps stuff.0 2> /dev/null
# restore -rf /dumps stuff.1-1 2> /dev/null
# restore -rf /dumps stuff.2-1 2> /dev/null

Remember how each backup captures all changes since the most recent backup of the level below it? In this case, the second level one captured all changes since the full, thus restoring the first level 1 and level 2 backups wasn’t necessary.

Restore Individual Files

I rarely have to restore entire filesystems. Whenever I need to restore from backup, it is usually a single file or directory. To restore a particular file or files, use restore -xf or, if you prefer, you can use interactive mode. I will cover interactive mode in the next section.

Suppose I accidentally delete or change the file named /stuff/1. If you scroll up, you will see it was create after the level 0 backup, but before the level 1. Remember, you would use restore -tf if you don’t know which backup file it is in.

Here is an example of the scenario:

# pwd
/stuff
# ls
0  1  2  3  4  lost+found
# rm 1
# ls
0  2  3  4  lost+found
# restore -xf /dumps/stuff.1-1 ./1
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
set owner/mode for '.'? [yn] y
# ls
0  1  2  3  4  lost+found

Unless you are using some kind of removable media, your backup is written directly to the device specified, and it spans more than one tape, disk, etc, answer 1 to the question of next volume.

Restore Interactive Mode

The restore program has an interactive mode you can use to restore files and directories. To access it, change your working directory to the root of the filesystem the backup applies to and use restore -if dumpfile.

Once you are in interactive mode, the help command will display a list of available commands with short descriptions. The commands cd, ls, and pwd are for navigating the backup file, not the operating system.

Extracting files in interactive mode is easy. Just use the add command to select the files you want to restore, then use the extract command. To see the files on extract list, use ls. Files that are on your extract list will be preceded with a *. If you want to restore the whole thing, just add /. Use the quit command to quit restore.

Restoring the whole thing:

# restore -if /dumps/stuff.0
restore > add /
restore > extract
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
set owner/mode for '.'? [yn] y
restore > quit
# ls
0  lost+found

Restoring a few files:

# ls
0  2  4  lost+found
# restore -tf ../dumps/stuff.0 1 3
Dump   date: Thu May 16 15:54:15 2019
Dumped from: the epoch
Level 0 dump of /stuff on dump:/dev/sdb1
Label: none
./1 is not on the tape
./3 is not on the tape
# restore -tf ../dumps/stuff.1-1 1 3
Dump   date: Thu May 16 15:55:09 2019
Dumped from: Thu May 16 15:54:15 2019
Level 1 dump of /stuff on dump:/dev/sdb1
Label: none
        12	./1
        14	./3
# restore -if ../dumps/stuff.1-1 
restore > add 1 3
restore > extract
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1
set owner/mode for '.'? [yn] y
restore > ls
.:
1 2 3
restore > quit
# ls
0  1  2  3  4  lost+found

References

See Also

  • Linux Filesystem Hierarchy