The Quick Guide to Using btrfs with TimeShift
Recently, I moved to a new workstation with a new computer and two monitors. I am well aware of the importance of system backups, but due to sunk cost, I never fully implemented btrfs backup and recovery. Since the new system has nothing on it yet, I can experiment freely. Here's a record of how I set up Timeshift.
This article focuses on practicality and does not delve into the underlying principles.
Creating a btrfs Partition
For a new hard drive (mine is /dev/nvme0n1
):
- First, create an EFI partition (512M) and mount it to
/boot/efi
. - If your RAM is less than 16G, it is recommended to create a swap partition (8G). (But my new computer has 32G of RAM, so I did not create a swap partition.)
- Allocate the remaining space to btrfs.
The corresponding commands are:
sudo fdisk /dev/nvme0n1
# Create a GPT partition table
g
# Create the EFI partition
n
# Partition number
<enter>
# Start position
<enter>
# End position
+512M
# Partition type
t
1
# Remove signature
y
# Create the btrfs partition
n
# Partition number
<enter>
# Start position
<enter>
# End position
<enter>
# Remove signature
y
# Write changes
w
Formatting
Now there are two partitions: an EFI partition /dev/nvme0n1p1
and a btrfs partition /dev/nvme0n1p2
. We need to format them:
sudo mkfs.fat -F32 /dev/nvme0n1p1
sudo mkfs.btrfs -L arch /dev/nvme0n1p2
Creating btrfs Subvolumes (Important)
To ensure Timeshift works correctly, we need to achieve the following:
> sudo btrfs subvolume list /
[sudo] password for junyu33:
ID 256 gen 3073 top level 5 path @
ID 257 gen 3073 top level 5 path @home
Specific commands:
sudo mount /dev/nvme0n1p2 /mnt
sudo btrfs subvolume create /mnt/@
sudo btrfs subvolume create /mnt/@home
sudo umount /mnt
sudo mount -o subvol=@ /dev/nvme0n1p2 /mnt
sudo mkdir /mnt/home
sudo mount -o subvol=@home /dev/nvme0n1p2 /mnt/home
sudo mkdir -p /mnt/boot/efi
sudo mount /dev/nvme0n1p1 /mnt/boot/efi
Installing the System
I'm not sure if Xubuntu will format the btrfs partition (I just tested it, and it seems it does—it can't be canceled). If so, my previous steps would be in vain. So I chose Arch Linux. I won't go into detail about the installation process; you can refer to the Arch Linux Installation Guide.
When you reach this step:
genfstab -U /mnt >> /mnt/etc/fstab
Check the content of the fstab
file and, combined with the output of blkid
, ensure the EFI partition and btrfs subvolumes are correctly mounted. Mine looks similar to:
# Static information about the filesystems.
# See fstab(5) for details.
# <file system> <dir> <type> <options> <dump> <pass>
# /dev/nvme0n1p2 LABEL=arch
UUID=d6806b41-d69c-4f72-ba9f-a00a197729ab / btrfs rw,relatime,ssd,discard=async,space_cache=v2,subvolid=256,subvol=/@ 0 0
# /dev/nvme0n1p2 LABEL=arch
UUID=d6806b41-d69c-4f72-ba9f-a00a197729ab /home btrfs rw,relatime,ssd,discard=async,space_cache=v2,subvolid=257,subvol=/@home 0 0
# /dev/nvme0n1p1
UUID=EB74-8512 /boot/efi vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2
At this point, your /mnt
directory should resemble a normal Linux root directory, not @
and @home
.
Then add the bootloader:
arch-chroot /mnt
pacman -S grub efibootmgr
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB
grub-mkconfig -o /boot/grub/grub.cfg
Then exit chroot and reboot into the new system.
Installing and Configuring Timeshift
# Install cronie and enable the cron service
sudo pacman -S cronie
sudo systemctl enable cronie
# Install Timeshift and the grub-btrfs plugin
sudo pacman -S timeshift grub-btrfs
yay -S timeshift-systemd-timer
# Configure grub-btrfs to work with Timeshift
sudo systemctl edit --full grub-btrfsd
# Change `grub-btrfsd --syslog /.snapshots` to `grub-btrfsd --syslog -t`
Launch timeshift
and configure it. Done.