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)