2

As I started to use Timeshift on my Linux Mint I saw that it was possible to backup on btrfs, however, with some requirements: that the linux system had to be installed on btrfs as well (and I think also being in @ subvolume as well). That was not my case, I am using ext4. So I was obliged to use rsync method, but that was taking ages to do a backup.

I started to search for tutorials, but I was only finding general articles on btrfs snapshots but not routine backup commands in a real world environment. So after a few tests I got to understand how to make btrfs snapshots on an external btrfs drive even if your system is installed on ext4 (or whatever), and I decided to share the answer here on linux & unix stackexchange.

4
  • I can't understand why somebody is downvoting. I'm new here, and I made a lot of effort to write this tutorial. If something is wrong please explain. Commented Sep 14, 2020 at 14:53
  • 1
    I’m voting to close this question because it's a tutorial rather than a question. Commented Sep 17, 2020 at 11:19
  • 1
    is it bad? I thought it was ok: stackoverflow.blog/2011/07/01/… Commented Sep 18, 2020 at 4:53
  • No, it's not bad, it is actually really good (as you mentioned in your link) Commented Nov 26, 2021 at 6:49

2 Answers 2

3

Setup

Provided that /media/myuser/btrbaks is the mount point of btrfs partition that is on external drive (see the end for details on partitions and mounts), here the commands that have to be launched only the first time.

Make a directory for the backups:

sudo mkdir /media/myuser/btrbaks/BACKUPS 

Create a @ subvolume that will be the destinations of rsync command

sudo btrfs subvolume create /media/myuser/btrbaks/BACKUPS/@ 

Routine backup commands

Here the commands that have to be launched for every backup

Rsync:

sudo rsync -aAXv --delete --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/run/* --exclude=/mnt/* --exclude=/media/* --exclude="swapfile" --exclude="lost+found" --exclude=/home/* --exclude=/root/* / /media/myuser/btrbaks/BACKUPS/@/ 

Btrfs snapshot:

sudo btrfs subvolume snapshot /media/myuser/btrbaks/BACKUPS/@ /media/myuser/btrbaks/BACKUPS/@_bak_`date +%Y.%m.%d_%H.%M.%S` 

Then you can check:

sudo btrfs subvolume list /media/attilio/btrbaks/ ID 279 gen 109 top level 5 path BACKUPS/@ ID 280 gen 109 top level 5 path BACKUPS/@_bak_2020.09.14_08.12.2 

Restoring

If your system boots, you can restore with:

sudo rsync -aAXv --dry-run --delete --exclude=/dev/* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/run/* --exclude=/mnt/* --exclude=/media/* --exclude="swapfile" --exclude="lost+found" --exclude=/home/* --exclude=/root/* /media/attilio/btrbaks/BACKUPS/@/ / 

Remember to delete the --dry-run option to perform a real restore.

Since @ points to your last backup, you can change it with @_bak_2020.09.14_08.12.22 or whichever snapshot.

If your system doesn't boot, you might boot from a live USB and perform the same command, changing the rsync <source> and <destination>

Note on partitions and mounts

This is my hardware configuration:

/dev/sda5 ext4 / # system installation /dev/sdb3 btrfs /media/myuser/btrbaks # external drive has several partitions, one is btrfs and here is the mount point that is automatically assigned by linux mint 
1
  • Is it possible to do this for the whole volume? i.e. make snapshots of the whole of /dev/sdb3 without needing to put things in a sub-directory? I'm not sure it would work because the external drive just mounts the default subvolume (id=5) Commented Jan 29, 2022 at 10:32
0

You can also convert your ext4 to btrfs without reinstalling. The snapshots and the live system swill now all use the same partition.

  • Advantages: Faster and less diskspace needed
  • Disadvantage: When the disk breaks down you also lose the snapshots. This can be countered by either:
    • Combining both methods. (Using btrfs snapshots in timeshift and using rsync to move selected snapshots to another disk)
    • Using RAID ( btrfs itself can be used for RAID although the usual methods are also equally valid)

Converting involves multiple steps:
Note: Test it first in a virtual machine. I didn't yet and i might have forgotten something.

  1. Boot from a live USB-stick (e.g. the one that you used to install Mint) and become root in a new terminal with sudo -i
  2. Convert your ext4 to a btrfs: btrfs-convert /dev/sda1 (assuming /dev/sda1 is your partition)
  3. Mount it so you can start "working": mount /dev/sda1 /mnt
  4. Make sure the root filesystem is in a subvolume called @: btrfs sub snap /mnt /mnt/@
  5. Create a separate one for your home-dirs: btrfs sub create /mnt/@home && rsync -HAXav /mnt/home/ /mnt/@/home/
  6. Search the UUID of your partition: ls -l /dev/disk/by-uuid | grep sda1
  7. Replace the line that mounts / in /mnt/@/etc/fstab into the 2 lines:
 UUID=xxxxxxxx-xxxx-xxxx-xxxxxxxx / btrfs defaults,subvol=@ 0 1 UUID=xxxxxxxx-xxxx-xxxx-xxxxxxxx /home btrfs defaults,subvol=@home 0 2 

and change (xxxxxxxx-xxxx-xxxx-xxxxxxxx by your real UUID).

  1. In the same file remove the line that mentions the swap
  2. Change the first line that contains linux in /mnt/boot/grub/grub.cfg to
    linux /@/boot/vmlinuz-YYYY root=UUID=XXXX ro rootflags=subvol=@ (with YYYY as kernelversion it originally had and XXXX again your UUID) and run cp /mnt/boot/grub/grub.cfg /mnt/@/boot/grub/grub.cfg
  3. Reboot your system and continue as root: sudo -i
  4. Keep only the data in your @ and your @home subvolumes: mkdir /mnt/fullbtrfs && mount /dev/sda1 /mnt/fullbtrfs && rm -rf /mnt/fullbtrfs/[a-z]* && umount /mnt/fullbtrfs
  5. Correct the rest of the grub configuration (a couple of steps ago we only did the bare necessity) : update-grub
  6. Reboot again, and change your timeshift configuration to btrfs

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.