Btrfs subvolumes and snapshots
This feature requires the Storage package.
Creating subvolumes
The main benefit of creating subvolumes is to organize data on your Btrfs main (root) subvolume. Consider subvolumes as folders with features of a partition, while still sharing the total disk space between all subvolumes. You can later use these subvolumes for much more advanced tasks and it is recommended to create subvolumes when you have large amounts of different types of data, especially data that requires frequent backups. Follow the guide to set up a few example subvolumes:
Subvolumes are most useful when used together with snapshots. Be sure to check out the snapshot feature as well.
-
Find the disk name of your disk that you want to use as your Btrfs disk:
/disk/printwarningIn this example, the disk used is going to be called
<disk-name-1>, make sure you replace the placeholder with your actual disk name! -
Format the disk to Btrfs, in this case
<disk-name-1>:/disk/format <disk-name-1> file-system=btrfs -
Add a label for the Btrfs disk for simplicity:
/disk/btrfs/filesystem/set [find where present-devs=<disk-name-1>] label=BtrfsDisk -
You can also change the Btrfs disk mount point for simplicity:
/disk/set <disk-name-1> mount-point-template=BtrfsDisk -
Create a new subvolume called
DocumentstoBtrfsDisk:/disk/btrfs/subvolume/add name=Documents fs=BtrfsDiskinfoSubvolumes are also snapshots. You might encounter both of these names in various menus. In simple terms, snapshots are subvolumes created at a specific time and contain data from that time point.
-
Create another subvolume called
PhotostoBtrfsDisk:/disk/btrfs/subvolume/add name=Photos fs=BtrfsDisk -
You can view the currently available subvolumes:
/disk/btrfs/subvolume/print -
You can now access these subvolumes as
/BtrfsDisk/Documentsand/BtrfsDisk/Photos.
Creating snapshots
Snapshots are a space efficient way to create backups for your data. By creating a snapshot you save the current state of your data which you can later access.
Snapshots don't create a copy of your data, they save the current state of your data while allowing you to make changes to your current data. Snapshots contain the information on how to revert the current data to a state that was present at the time when the snapshot was created. Snapshots don't create multiple copies of your data like, for example, a full backup does.
While you can create snapshots of a root subvolume, it is recommended to create a new subvolume for your data and then use the snapshot feature. This is only a preference when managing many snapshots.
-
Create subvolumes (or use the root subvolume, see above) and put data inside these subvolumes, for example:
[admin@MikroTik] > /file/print# NAME0 BtrfsDisk1 BtrfsDisk/Documents2 BtrfsDisk/Photos3 BtrfsDisk/Documents/document1.txt4 BtrfsDisk/Photos/photo1.jpg -
In this example, we have created
/BtrfsDisk/Documentsand/BtrfsDisk/Photos/subvolumes and you can view them with:/disk/btrfs/subvolume/print -
In order to make snapshots more organized, create a new subvolume called
Snapshots:/disk/btrfs/subvolume/add name=Snapshots fs=BtrfsDisk -
Create a snapshot for
DocumentsandPhotos:/disk/btrfs/subvolume/add read-only=yes parent=Documents fs=BtrfsDisk name=Snapshots/Documents-22012025/disk/btrfs/subvolume/add read-only=yes parent=Photos fs=BtrfsDisk name=Snapshots/Photos-22012025 -
You should now have new subvolumes that are read-only and contain your files:
[admin@MikroTik] > /file/print# NAME0 BtrfsDisk1 BtrfsDisk/Documents2 BtrfsDisk/Photos3 BtrfsDisk/Snapshots4 BtrfsDisk/Documents/document1.txt5 BtrfsDisk/Photos/photo1.jpg6 BtrfsDisk/Snapshots/Documents-220120257 BtrfsDisk/Snapshots/Photos-220120258 BtrfsDisk/Snapshots/Documents-22012025/document1.txt9 BtrfsDisk/Snapshots/Photos-22012025/photo1.jpg -
For testing purposes, you can add more data to your subvolumes and you should notice that newly added files do not appear in the snapshots, but only in the subvolumes:
[admin@infra1.mikrotikls.lv] > /file/print# NAME0 BtrfsDisk1 BtrfsDisk/Documents2 BtrfsDisk/Photos3 BtrfsDisk/Snapshots4 BtrfsDisk/Documents/document1.txt5 BtrfsDisk/Documents/document2.txt6 BtrfsDisk/Photos/photo1.jpg7 BtrfsDisk/Photos/photo2.jpg8 BtrfsDisk/Snapshots/Photos-220120259 BtrfsDisk/Snapshots/Documents-2201202510 BtrfsDisk/Snapshots/Documents-22012025/document1.txt11 BtrfsDisk/Snapshots/Photos-22012025/photo1.jpg -
You can now create a new snapshot:
/disk/btrfs/subvolume/add read-only=yes parent=Documents fs=BtrfsDisk name=Snapshots/Documents-23012025/disk/btrfs/subvolume/add read-only=yes parent=Photos fs=BtrfsDisk name=Snapshots/Photos-23012025 -
After a new snapshot, you will have 2 snapshots for each subvolume. One contains older files and another one contains older and newer files:
[admin@MikroTik] > /file/print# NAME0 BtrfsDisk1 BtrfsDisk/Documents2 BtrfsDisk/Photos3 BtrfsDisk/Snapshots4 BtrfsDisk/Documents/document1.txt5 BtrfsDisk/Documents/document2.txt6 BtrfsDisk/Photos/photo1.jpg7 BtrfsDisk/Photos/photo2.jpg8 BtrfsDisk/Snapshots/Photos-220120259 BtrfsDisk/Snapshots/Documents-2201202510 BtrfsDisk/Snapshots/Documents-2301202511 BtrfsDisk/Snapshots/Photos-2301202512 BtrfsDisk/Snapshots/Documents-22012025/document1.txt13 BtrfsDisk/Snapshots/Documents-23012025/document1.txt14 BtrfsDisk/Snapshots/Documents-23012025/document2.txt15 BtrfsDisk/Snapshots/Photos-22012025/photo1.jpg16 BtrfsDisk/Snapshots/Photos-23012025/photo1.jpg17 BtrfsDisk/Snapshots/Photos-23012025/photo2.jpgnoteMultiple snapshots do not create multiple copies of each file, but if a file has been deleted and it still exists in a snapshot, then the deleted file will take up space. If a file exists in multiple snapshots, then it will take up space for only 1 file.
-
In case you don't need an older snapshot, you can delete it:
/disk/btrfs/subvolume/remove [find where name=Documents-22012025]/disk/btrfs/subvolume/remove [find where name=Photos-22012025]
Transfer your snapshots
Btrfs allows you to easily send a snapshot between two devices that are using Btrfs. In this example we will use two RouterOS devices running the Storage package. The RouterOS device containing snapshots that need to be backed up is going to be called RouterA and the RouterOS device that is going to receive backups is going to be called RouterB.
This guide only shows an example of how to use Btrfs transfer between RouterOS devices, but you can transfer snapshots between RouterOS and Linux devices as well. If you require such functionality, then make sure to check the documentation of your Linux distribution on how to use Btrfs transfer on it.
This guide will use the SSH host key for an SSH user key on the same RouterOS device. If you wish to use your own key for the SSH user, then you can use these commands on a Linux computer and later import the key pair under /user/ssh-keys :
openssl genpkey -outform PEM -out btrfstransfer_key.pem -algorithm ED25519
openssl pkey -in btrfstransfer_key.pem -pubout -out btrfstransfer_key_pub.pem
-
OPTIONAL: Increase the security level of your SSH server, run the following commands on
RouterAandRouterB:/ip/ssh/set host-key-type=ed25519 strong-crypto=yes/ip/ssh/regenerate-host-keywarningRegenerating the host key will create an error message next time you try to connect to the RouterOS device using SSH. You will need to adjust your SSH client's configuration (usually in
~/.ssh/known_hosts) to trust the new host key. -
On
RouterAexport the SSH private and public key:/ip/ssh/export-host-key key-file-prefix=admin -
On
RouterAimport the SSH private key for useradmin:/user/ssh-keys/private/import private-key-file=admin_ed25519.pem user=admin -
Upload the SSH public key from
RouterAtoRouterB:/file/sync/add local-path=admin_ed25519_pub.pem remote-address=RouterB user=admin mode=upload/file/sync/remove [find where local-path=admin_ed25519_pub.pem] -
On
RouterBcreate a new user, for examplebtrfstransferand set a secure password for it:/user/add name=btrfstransfer group=writewarningWhile the password is not going to be used for the Btrfs transfer feature, you still need to use a secure password to prevent unauthorized access to your device.
-
On
RouterBimport the uploaded SSH public key and set it for userbtrfstransfer:/user/ssh-keys/import public-key-file=admin_ed25519_pub.pem user=btrfstransfer -
On
RouterAset up typesend:/disk/btrfs/transfer/add type=send fs=BtrfsDisk ssh-address=RouterB send-subvolumes=Documents-23012025 ssh-user=btrfstransfer ssh-receive-mount=BackupBtrfsDisk/SnapshotsWhere:
BtrfsDiskis the Btrfs disk label found under/disk/btrfs/filesystem/printonRouterA.Documents-23012025is the snapshot name (not the path).btrfstransferis the SSH user onRouterB.BackupBtrfsDiskis the Btrfs disk label found under/disk/btrfs/filesystem/printonRouterB.Snapshotsis the subvolume for your snapshots underBackupBtrfsDisk.
-
On
RouterBset up typereceive:/disk/btrfs/transfer/add fs=BackupBtrfsDisk type=receive file=BackupBtrfsDisk/SnapshotsWhere:
BackupBtrfsDiskis the Btrfs disk label found under/disk/btrfs/filesystem/printonRouterBBackupBtrfsDisk/Snapshotsis the mount path forSnapshotssubvolume.