Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Recover your files from Btrfs snapshots

#1
Recover your files from Btrfs snapshots

<div><p>As you have seen <a href="https://fedoramagazine.org/btrfs-snapshots-backup-incremental/">in a previous article</a>, Btrfs snapshots are a convenient and fast way to make backups. Please note that these articles do not suggest that you avoid backup software or well-tested backup plans. Their goals are to show a great feature of this file system, snapshots, and to inspire curiosity and invite you to explore, experiment and deepen the subject. Read on for more about how to recover your files from Btrfs snapshots.</p>
<p> <span id="more-31967"></span> </p>
<h2>A subvolume for your project</h2>
<p>Let’s assume that you want to save the documents related to a project inside the directory <em>$HOME/Documents/myproject</em>.</p>
<p>As you have seen, a Btrfs subvolume, as well as a snapshot, looks like a normal directory. Why not use a Btrfs subvolume for your project, in order to take advantage of snapshots? To create the subvolume, use this command:</p>
<pre class="wp-block-preformatted">btrfs subvolume create $HOME/Documents/myproject</pre>
<p>You can create an hidden directory where to arrange your snapshots:</p>
<pre class="wp-block-preformatted">mkdir $HOME/.snapshots</pre>
<p>As you can see, in this case there’s no need to use <em><a href="https://fedoramagazine.org/howto-use-sudo/">sudo</a></em>. However, <em>sudo</em> is still needed to list the subvolumes, and to use the <em>send</em> and <em>receive</em> commands.</p>
<p>Now you can start writing your documents. Each day (or each hour, or even minute) you can take a snapshot just before you start to work:</p>
<pre class="wp-block-preformatted">btrfs subvolume snapshot -r $HOME/Documents/myproject $HOME/.snapshots/myproject-day1</pre>
<p>For better security and consistency, and if you need to send the snapshot to an external drive as shown in the previous article, remember that the snapshot must be read only, using the <em>-r</em> flag.</p>
<p>Note that in this case, a snapshot of the <em>/home</em> subvolume <strong>will not</strong> snapshot the <em>$HOME/Documents/myproject</em> subvolume.</p>
<h2>How to recover a file or a directory</h2>
<p>In this example let’s assume a classic error: you deleted a file by mistake. You can recover it from the most recent snapshot, or recover an older version of the file from an older snapshot. Do you remember that a snapshot appears like a regular directory? You can simply use the <em>cp</em> command to restore the deleted file:</p>
<pre class="wp-block-preformatted">cp $HOME/.snapshots/myproject-day1/filename.odt $HOME/Documents/myproject</pre>
<p>Or restore an entire directory:</p>
<pre class="wp-block-preformatted">cp -r $HOME/.snapshots/myproject-day1/directory $HOME/Documents/myproject</pre>
<p>What if you delete the entire <em>$HOME/Documents/myproject</em> directory (actually, the subvolume)? You can recreate the subvolume as seen before, and again, you can simply use the <em>cp</em> command to restore the entire content from the snapshot:</p>
<pre class="wp-block-preformatted">btrfs subvolume create $HOME/Documents/myproject<br />cp -rT $HOME/.snapshots/myproject-day1 $HOME/Documents/myproject</pre>
<p>Or you could restore the subvolume by using the btrfs snapshot command (yes, a snapshot of a snapshot):</p>
<pre class="wp-block-preformatted">btrfs subvolume snapshot $HOME/.snapshots/myproject-day1 $HOME/Documents/myproject</pre>
<h2>How to recover btrfs snapshots from an external drive</h2>
<p>You can use the <em>cp</em> command even if the snapshot resides on an external drive. For instance:</p>
<pre class="wp-block-preformatted">cp /run/media/user/mydisk/bk/myproject-day1/filename.odt $HOME/Documents/myproject</pre>
<p>You can restore an entire snapshot as well. In this case, since you will use the <em>send</em> and <em>receive</em> commands, you must use <em>sudo</em>. In addition, consider that the restored subvolume will be created as read only. Therefore you need to also set the read only property to <em>false</em>:</p>
<pre class="wp-block-preformatted">sudo btrfs send /run/media/user/mydisk/bk/myproject-day1 | sudo btrfs receive $HOME/Documents/<br />mv Documents/myproject-day1 Documents/myproject<br />btrfs property set Documents/myproject ro false</pre>
<p>Here’s an extra explanation. The command <em>btrfs subvolume snapshot</em> will create an exact copy of a subvolume in the same device. The destination has to reside in the same btrfs device. You can’t use another device as the destination of the snapshot. In that case you need to take a snapshot and use the <em>send</em> and <em>receive</em> commands.</p>
<p>For more information, refer to some of the online documentation:</p>
<pre class="wp-block-preformatted">man btrfs-subvolume
man btrfs-send
man btrfs-receive</pre>
</div>


https://www.sickgaming.net/blog/2020/10/...snapshots/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016