Btrfs subvolumes and snapshots
Info 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:
Tip 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/print:::warning In 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=BtrfsDisk:::info Subvolumes 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.
Warning
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.
Note
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 # NAME 0 BtrfsDisk 1 BtrfsDisk/Documents 2 BtrfsDisk/Photos 3 BtrfsDisk/Documents/document1.txt 4 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 # NAME 0 BtrfsDisk 1 BtrfsDisk/Documents 2 BtrfsDisk/Photos 3 BtrfsDisk/Snapshots 4 BtrfsDisk/Documents/document1.txt 5 BtrfsDisk/Photos/photo1.jpg 6 BtrfsDisk/Snapshots/Documents-22012025 7 BtrfsDisk/Snapshots/Photos-22012025 8 BtrfsDisk/Snapshots/Documents-22012025/document1.txt 9 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 # NAME 0 BtrfsDisk 1 BtrfsDisk/Documents 2 BtrfsDisk/Photos 3 BtrfsDisk/Snapshots 4 BtrfsDisk/Documents/document1.txt 5 BtrfsDisk/Documents/document2.txt 6 BtrfsDisk/Photos/photo1.jpg 7 BtrfsDisk/Photos/photo2.jpg 8 BtrfsDisk/Snapshots/Photos-22012025 9 BtrfsDisk/Snapshots/Documents-22012025 10 BtrfsDisk/Snapshots/Documents-22012025/document1.txt 11 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 # NAME 0 BtrfsDisk 1 BtrfsDisk/Documents 2 BtrfsDisk/Photos 3 BtrfsDisk/Snapshots 4 BtrfsDisk/Documents/document1.txt 5 BtrfsDisk/Documents/document2.txt 6 BtrfsDisk/Photos/photo1.jpg 7 BtrfsDisk/Photos/photo2.jpg 8 BtrfsDisk/Snapshots/Photos-22012025 9 BtrfsDisk/Snapshots/Documents-22012025 10 BtrfsDisk/Snapshots/Documents-23012025 11 BtrfsDisk/Snapshots/Photos-23012025 12 BtrfsDisk/Snapshots/Documents-22012025/document1.txt 13 BtrfsDisk/Snapshots/Documents-23012025/document1.txt 14 BtrfsDisk/Snapshots/Documents-23012025/document2.txt 15 BtrfsDisk/Snapshots/Photos-22012025/photo1.jpg 16 BtrfsDisk/Snapshots/Photos-23012025/photo1.jpg 17 BtrfsDisk/Snapshots/Photos-23012025/photo2.jpg:::note
Multiple 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.
Info
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.
Tip
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-key:::warning
Regenerating 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=write:::warning
While 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.