By default, you can store the snapshots you take of your Btrfs subvolumes in the same Btrfs filesystem, but it is not possible to store the snapshots of one Btrfs filesystem directly to another Btrfs filesystem. However, the Btrfs filesystem provides you with the necessary tools to back up snapshots of one Btrfs filesystem to another Btrfs filesystem. This article shows you how to back up Btrfs snapshots to an external Btrfs filesystem on an external drive.
Prerequisites
To try out the examples included in this article, you must fulfill the following prerequisites:
- Have the Btrfs filesystem installed on your computer.
- Have a hard disk or SSD with at least 2 free partitions (of any size).
I have the 20 GB hard disk, sdb, on my Ubuntu machine. I have created two partitions, sdb1 and sdb2, on this hard disk.
Note: Your hard disk or SSD will have a different name than mine, and so will the partitions. So, be sure to replace these names with yours from now on.
I will create Btrfs filesystems on the sdb1 and the sdb2 partitions. The snapshots created on the Btrfs filesystem (sdb1) will be backed up to the Btrfs filesystem created on the sdb2 partition. The Btrfs filesystem created on the sdb2 partition will act as the external drive. You may use a USB thumb drive or an external hard drive, as well; just be sure to format it with the Btrfs filesystem.
For assistance with installing the Btrfs filesystem in Ubuntu, check out my article Install and Use Btrfs on Ubuntu 20.04 LTS.
For assistance with installing the Btrfs filesystem in Fedora, check out my article Install and Use Btrfs on Fedora 33.
Creating Required Btrfs Filesystems
I will format both the sdb1 and sdb2 partitions as Btrfs. I will use the sdb1 partition for storing the data and Btrfs snapshots. I will use the sdb2 partition for backing up the snapshots of the Btrfs filesystem created on the sdb1 partition.
To create a Btrfs filesystem on the sdb1 partition and give it the filesystem label data, run the following command:
A Btrfs filesystem should now be created on the sdb1 partition.
To create a Btrfs filesystem on the sdb2 partition and give it the filesystem label snapshots, run the following command:
A Btrfs filesystem should now be created on the sdb2 partition.
Create the directories /data and /snapshots for mounting the sdb1 and sdb2 partitions, respectively, as follows:
Mount the Btrfs filesystem you have created on the sdb1 partition on the /data directory, as follows:
In the same way, mount the Btrfs filesystem you have created on the sdb2 partition on the /snapshots directory, as follows:
As you can see in the screenshot below, both the Btrfs filesystems (sdb1 and sdb2 partitions) have been mounted correctly.
Taking Snapshots of a Btrfs Filesystem
In this section, we will create the dummy project web1 on the /data/projects/web1 Btrfs subvolume. We will take a snapshot of that subvolume in this section, as well as some other snapshots in later sections of this article.
First, create the new directory /data/projects, as follows:
Next, create the new subvolume web1 in the /data/projects directory, as follows:
Finally, create the new file index.html in the /data/projects/web1 subvolume with the nano text editor, as follows:
Type in the following lines of code in the index.html file:
Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.
In the same way, create the new file style.css in the /data/projects/web1 subvolume as follows:
Type the following lines of code in the style.css file:
color: green;
}
Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the style.css file.
Now, the /data/projects/web1 subvolume contains the index.html and style.css file.
We will keep all the snapshots of this Btrfs filesystem in the /data/.snapshots directory.
First, create the /data/.snapshots directory with the following command:
Next, create the read-only snapshot /data/.snapshots/web1-2020-12-30 of the /data/projects/web1 subvolume with the following command:
As you can see, the new snapshot /data/.snapshots/web1-2020-12-30 has been created.
Backing up Snapshots to External Drive
To back up the snapshot /data/.snapshots/web1-2020-12-30 to another Btrfs filesystem (external drive sdb2, in this case) mounted on the /snapshots directory, run the following command:
The snapshot /data/.snapshots/web1-2020-12-30 should be backed up to the external Btrfs filesystem (sdb2) mounted on the /snapshots directory.
As you can see, the new subvolume web1-2020-12-30 has been created on the external Btrfs filesystem.
The snapshot web1-2020-12-30 should have the same files/directories as the /data/.snapshots/web1-2020-12-30 snapshot.
You can obtain more information about the backed-up snapshot /snapshosts/web1-2020-12-30 as follows:
Incremental Back-up of Snapshots to External Drive
If there are a lot of files in the snapshots to back up to an external drive, then incremental backups will help you speed up the back-up operation. In this case, Btrfs will only update the files that have changed since the last snapshot and copy new files that were not available in the last snapshot.
In this section, I will show you how to perform incremental back-ups of Btrfs snapshots to external Btrfs filesystems.
First, open the index.html file from the /data/projects/web1 subvolume, as follows:
Make any changes you want to the index.html file. Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.
Take a new read-only snapshot of the /data/projects/web1 subvolume, as follows:
As you can see, the new snapshot /data/.snapshots/web1-2020-12-31 of the /data/projects/web1 subvolume has been created.
Now, we are ready to take an incremental backup.
To take an incremental backup, you will need a common snapshot of both the source and the destination (external drive) Btrfs filesystems. The common snapshot is usually the latest snapshot of a Btrfs subvolume. When you take a new snapshot on the source Btrfs filesystem, the new snapshot is compared with the latest snapshot (available on both the source and the destination Btrfs filesystem) of the source Btrfs filesystem. Btrfs will calculate the difference and send only the required data to the destination Btrfs filesystem (the external drive).
For example, to take an incremental backup of the /data/.snapshots/web1-2020-12-31 snapshot, you must specify the parent snapshot (the latest snapshot available on both the source and destination Btrfs filesystems), /data/.snapshots/web1-2020-12-30, as well.
An incremental backup of the /data/.snapshots/web1-2020-12-31 snapshot can be taken to an external Btrfs filesystem, as follows:
An incremental backup of the /data/.snapshots/web1-2020-12-31 snapshot should be taken.
As you can see, the web1-2020-12-31 snapshot has been backed up to the external Btrfs filesystem mounted on the /snapshots directory.
As you can see in the screenshot below, the changes you have made to the index.html file are available in the web1-2020-12-31 snapshot that has been backed up to the external Btrfs filesystem.
In the same way, you may take as many incremental backups of your snapshots as you want.
I will show you how to do an incremental backup one more time. I will not take the time to explain it again. Instead, I will just show you the process for clarity.
Open the index.html file from the /data/projects/web1 subvolume, as follows:
Make any changes you want to the index.html file. Once you are done, press <Ctrl> + X followed by Y and <Enter> to save the index.html file.
Take a new read-only snapshot of the /data/projects/web1 subvolume, as follows:
Take an incremental backup of the /data/.snapshots/web1-2020-12-31_2 snapshot to an external Btrfs filesystem, as follows:
Note: Now, the parent snapshot to which the /data/.snapshots/web1-2020-12-31_2 snapshot will be compared is /data/.snapshots/web1-2020-12-31.
As you can see, the web1-2020-12-31_2 snapshot has been backed up to the external Btrfs filesystem mounted on the /snapshots directory.
As you can see in the screenshot below, the recent changes made to the index.html file are available on the web1-2020-12-31_2 snapshot backed up to the external Btrfs filesystem.
Keeping Things Clean
If you back up your Btrfs snapshots frequently, you will end up with a lot of snapshots, and it may become difficult to manage them. Luckily, you can remove any snapshot from the Btrfs filesystem.
If you are using a large enough external drive for keeping backups of the Btrfs snapshots, then you can keep a few snapshots on your Btrfs filesystem and back up all the snapshots on your external drive.
If you are using a smaller external drive, then you can selectively keep only the most important snapshots backed up on the external drive.
To perform backups of your Btrfs snapshots, you need to keep at least the latest snapshot on both the source (/data/.snapshots) and the destination (/snapshots – external drive) Btrfs filesystems. So, feel free to remove any snapshots other than the latest snapshot on both ends.
For example, in this case, the latest snapshot is web1-2020-12-31_2. So, to perform incremental backups, this snapshot must be kept on the source and the destination (external drive) Btrfs filesystems.
Suppose, you want to remove the /data/.snapshots/web1-2020-12-30 snapshot.
To do this, run the following command:
The Btrfs snapshot /data/.snapshots/web1-2020-12-30 should now be removed.
In the same way, you can remove the /data/.snapshots/web1-2020-12-31 snapshot, as follows:
Now, only the latest snapshot, /data/.snapshots/web1-2020-12-31_2, is available on the Btrfs filesystem, mounted on the /data directory. The other snapshots are backed up on the external drive, mounted on the /snapshots directory.
$ sudo btrfs subvolume list /snapshots
Restoring Snapshots from External Drive
If you have backed up your snapshots on the external drive, you can restore them at any time from the external drive.
For example, I have removed the web1-2020-12-30 snapshot from my Btrfs filesystem, mounted on the /data directory. But, this snapshot is backed up on the external drive, mounted on the /snapshots directory. Let us restore this snapshot.
To restore the web1-2020-12-30 snapshot from the external drive, run the following command:
The snapshot web1-2020-12-30 should be restored on the Btrfs filesystem mounted on the /data directory.
As you can see, the web1-2020-12-30 snapshot is restored on the Btrfs filesystem mounted on the /data directory.
And, as you can see, the contents of the index.html file from the web1-2020-12-30 snapshot. This is the first version of the index.html file from before.
Conclusion
In this article, you learned how to back up snapshots of your Btrfs filesystem to an external drive. You also learned how to take incremental backups of your Btrfs snapshots to an external drive. Finally, you learned how to remove existing snapshots from a Brtfs filesystem and restore snapshots from the external drive, as well.