Btrfs: Difference between revisions

imported>Privacy1st
use single BTRFS command to create swapfile
imported>Sirphobos
changed/expanded snapshot section
Line 97: Line 97:
=== Snapshots ===
=== Snapshots ===


Taking a read-only (<code>-r</code>) snapshot called <code>nixos_snapshot_202302</code> of the subvolume mounted at <code>/</code>
A snapshot in btrfs is simply a subvolume that shares its data (and metadata) with some other subvolume, using btrfs's COW capabilities.
 
Because of that, there is no special location for snapshots - you need to decide where you want to store them for yourself. It can be a simple directory inside root subvolume, or a directory inside a dedicated "snapshots" subvolume.
 
For this example we are going to store snapshots in a simple directory <code>/snapshots</code>, that has to be created beforehand with <code>sudo mkdir /snapshots</code>
 
Taking a read-only (<code>-r</code>) snapshot called <code>home_snapshot_202302</code> of the subvolume mounted at <code>/home</code>
 
<syntaxhighlight lang="bash">
btrfs subvolume snapshot -r /home /snapshots/home_snapshot_202302
</syntaxhighlight>
 
You can also snapshot the root subvolume. But keep in mind, that nested subvolumes are '''not''' part of a snapshot. So if you have subvolumes <code>/nix /home</code>, taking snapshot of <code>/</code> will not include them.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
btrfs subvolume snapshot -r / /mnt/@nixos_snapshot_202302
btrfs subvolume snapshot -r / /snapshots/nixos_snapshot_202302
</syntaxhighlight>
</syntaxhighlight>


Line 106: Line 118:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
btrfs property set -ts /mnt/@nixos_snapshot_202302 ro false
btrfs property set -ts /snapshots/home_snapshot_202302 ro false
</syntaxhighlight>
 
However, changing read-only property of a snapshot in-place may [//lore.kernel.org/linux-btrfs/06e92a0b-e71b-eb21-edb5-9d2a5513b718@gmail.com/ causes issues] with any future incremental send/receive.
 
Instead, a read-only snapshot itself (being a simple subvolume) can be snapshoted again as a read-write snapshot like this:
<syntaxhighlight lang="bash">
btrfs subvolume snapshot /snapshots/home_snapshot_202302 /snapshots/home_snapshot_202302_rw
</syntaxhighlight>
</syntaxhighlight>


List snapshots for <code>/</code>
Or it can be restored directly to <code>/home</code> straight away like this:
{{warning|1=this will delete current <code>/home</code> and restore the snapshot! <code>/home</code> must be unmounted for this operation}}


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo btrfs subvolume list /
btrfs subvolume delete /home
btrfs subvolume snapshot /snapshots/home_snapshot_202302 /home
</syntaxhighlight>
</syntaxhighlight>
After this you can mount <code>/home</code> again.


=== Transfer snapshot ===
=== Transfer snapshot ===


Sending the snapshot <code>/mnt/@nixos_snapshot_202302</code> compressed to a remote host via ssh at <code>root@192.168.178.110</code> and saving it to a subvolume mounted or directory at <code>/mnt/nixos</code>
Sending the snapshot <code>/snapshots/nixos_snapshot_202302</code> compressed to a remote host via ssh at <code>root@192.168.178.110</code> and saving it to a subvolume mounted or directory at <code>/mnt/nixos</code>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo btrfs send /mnt/@nixos_snapshot_202302 | zstd | ssh root@192.168.178.110 'zstd -d | btrfs receive /mnt/nixos'
sudo btrfs send /snapshots/nixos_snapshot_202302 | zstd | ssh root@192.168.178.110 'zstd -d | btrfs receive /mnt/nixos'
</syntaxhighlight>
</syntaxhighlight>


[[Category: Configuration]]
[[Category: Configuration]]
[[Category:Filesystem]]
[[Category:Filesystem]]