I've installed Ubuntu 20.04 on a Btrfs root-partition for its snapshot functionality.
To keep it as simple as possible, I would like to integrate the creation of a Btrfs snapshot into my upgrade
-alias command, which currently looks like this:
sudo apt update && sudo apt upgrade -y && sudo flatpak update -y && sudo snap refresh
How would I best add a snapshot before the updates so I can roll back if anything goes wrong?
Is there also a possiblity to remove older snapshots at the same time? (My root-partition is filled less than 10%, so I could copy my entire system multiple times, but I suppose it will fill up quickly with weekly updates?)
I would build on the nice script by Ignacio Nunez Hernanz:
Relevant usage info:
Your upgrade alias would need to look like this:
btrfs-snp / syschanges 3 600 && ...
which generates a snapshot with the tag syschanges in/.snapshots
, but not if there is already one in the last 5 minutes, and keeps maximum 3 of these.This gives you a 5-minute window to do repeat operations without cluttering, for example, if you want to install from different repos or ppas in one install step, not only upgrades.
Then, you can use and restore these snapshots as per btrfs best practice.
It is quite easy to make a snapshot in
btrfs
.First mount your partitition containing the
btrfs
filesystem to e.g./mnt
. We are assuming it is/dev/sda1
.If you have a standard Ubuntu install with
/
at@
and/home
at@home
, runningls
will show two items:@
and@home
.Also if you previosly created snapshots, they will be shown there too.
To create snapshots of your
/
and/home
, run the command:If you want to remove existing backups before you create a new one the command will be:
As simple as that.
After you finish with that unmount your partition from
/mnt
by:In addition I can add that you can create snapshots with timestamp or do incremental backups. But it is a bit out of scope of the question.
You can combine these commands into a text file like
backup.sh
.Example:
The script should be run with
sudo
.I have several ideas for you. Pick, choose and combine.
btrfs sub snapshot -r /mnt/btrfsroot/@/ /mnt/btrfsroot/snapshots/root-$(date +%y%m%d)
as well./mnt/btrfsroot/snapshots/@*-apthook-YYMMDDHH
then you would for example run a cronjob on every 12th of the month as34 03 12 * * btrfs sub delete /mnt/btrfsroot/snapshots/@*-apthook-$(date --date='15 days ago' +\%y\%m)*
. Check the man pages forman 5 crontab
andman date
for further info.I hope this gets you going in the right direction. Again, I would suggest simply going with
apt-btrfs-snapshot
and be done with it. Please be aware that as of now,apt-btrfs-snapshot
assumes that your root partition is named@
. This is the default for Ubuntu and a number of other distributions.Feel free to ask follow-up questions in case something is unclear.
PS: Do you understand the difference between
/
(the root of your running system) and the btrfs-root?You can easily do this with this shell script.
Create a shell script with this content:
After creating the file, replace /snapshots with your desired snapshot directory where snapshots will be saved. Then place it anywhere.
Now make it executable by executing:
Now change the command for your upgrade alias so it points the script.
Now running your alias will first delete snapshots with
backup_
prefix then it will take a snapshot of the filesystem saved with name starting withbackup_
.Note that when first time run, it can show error. But ignore it as first time when run, there is no backup so there is nothing to delete. Also don't make subvolumes or snapshot with name starting with
backup_
at at the directory where backup snapshot will be saved. It will cause that to be deleted when the script is run. Also snapshot will not include files from other snapshots, subvolumes and mounted partitions.