3

I am planning to utilize btrfs for a NAS system.

Now I am wondering whether it is possible to roll back things to snapshots on a file / folder level. I ask this because typically one only wants to roll back single items while keeping the state of the rest of the drive.

Is there some native way to do this or if not, how to best achieve this? I read Restore single files from a btrfs snapshot without doing a full copy which seems to be a way but not that convenient. Might be the best way to do this, though.

3 Answers 3

2

Taking a snapshot basically creates a subvolume that can be accessed in the directory structure of the base volume. From there, given one has the appropriate rights, one can browse it and copy files or folders over. The --reflink option for copying does not seem to be neded for that, although that is nicer, so things are not duplicated when copying them.

with btrfs tools

Optionally creating a subvolume for the snapshots:

btrfs subvolume create .my_snapshots

Creating the snapshot:

btrfs subvolume snapshot -r /mnt_point /mnt_point/.my_snapshots/snapshot1

Using the -r flag makes the snapshot read only (https://wiki.archlinux.org/index.php/btrfs#Snapshots).

Without creating a subvolume first and not making it read only, it could simply look like this:

btrfs subvolume snapshot /mnt_point /mnt_point/snapshot1

Restoring can be achieved then with copying the file(s) or folder(s) from the snapshot:

cp /mnt_point/.my_snapshots/snapshot1/sub/dir/file /sub/dir/file

(Derived from https://lore.kernel.org/linux-btrfs/[email protected]/t/#m16db5d9f29d26f41a8adb4499ffe3e220a85d9b8)

external tools (snapper used as an example)

Using external tools like snapper has some benefits, since they make handling snapshots a bit more convenient and come with useful functionality.

First a config file needs to be created for the (sub)volume one wants to create snapshots for:

snapper -c config-name create-config /mnt_point

This creates a configuration file named config-name. Note that this will also usually enable creation of automatic time-based snapshots as well as rules for cleaning those after some logic. Note that manually created snapshots won't get removed by such rules.

A snapshot can then be taken manually with:

snapper -c config-name create --description description-content

where config-name is the name of the previously created config and description-content can be filled with some description for the snapshot.

(Derived from https://wiki.archlinux.org/index.php/Snapper)

Restoring then works similarly as above for the btrfs tools section with accessing the mounted snapshot and copying things over.

Additionally snapper provides a command for that which can also be used to undo changes for the current volume according to the openSUSE documentation. Note though that this might lead to Data inconsistencies. Also the Red Hat documentation states to not use that command with the root file system.

In theory, files can be restored with:

 snapper -c config-name -v undochange SNAPSHOT_ID..0 FILENAME

where SNAPSHOT_ID is the ID of the snapshot to restore from (and ..0 indicates that the files are restored to the main volume) and FILENAME is the path to the file to restore. One can also specify more than one file.

(Derived from https://doc.opensuse.org/documentation/leap/archive/15.0/reference/html/book.opensuse.reference/cha.snapper.html#proc.snapper.restore.cmdl)

1

From the terminal

Assuming you are using BTRFS native snapshots

Browse to the /.snapshot/@timestamp_folder/subfolder where the file lives in the snapshots. Then run the following and 'copy/sync' the single file to a new location.

sudo rsync myfile.txt /shares/backups/restored/restored.txt

May also work on folders (haven't tested).

0

BTRFS snapshots work on the subvolume level. This means you can "roll back" subvolumes, but not individual files.

Since subvolumes appear as directories in the filesystem, you could say it's possible to roll back directories.

To roll back individual files I use a reflink copy, as shown in the link you provided. However, I have the relevant subvolumes mounted automatically via /etc/fstab so there's no incovenience.

1
  • Hm, that seems a bit complicated. Basically one would mount both relevant subvolumes and copy the file over? Today I read about snapper, which seems to offer more convenient handling for this.
    – clel
    Commented Dec 10, 2020 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .