A simple shell script for automatically creating and cleaning BTRFS subvolume snapshots. Works on my Arch Linux. Please carefully review the code before using it, as it might corrupt your data!
- BTRFS filesystem + Systemd-boot
subvolume | mountpoint |
---|---|
@ | / |
@home | /home |
@log | /var/log |
@pkg | /var/cache/pacman/pkg |
@.snapshots | /.snapshots |
@docker | /var/lib/docker |
You can change the following variables at the beginning of the script.
TIMEZONE="Asia/Shanghai"
MOUNT_POINT="/.snapshots/"
SNAPSHOT_DIR="/.snapshots/autosnap"
SUBVOLUME_NAME="@.snapshots"
BOOT_FILE="/boot/loader/entries/arch.conf"
- Automatically mount
@.snapshots
subvolume to/.snapshots
. - Create a directory using today's date (e.g.
/.snapshots/autosnap/2023-09-30
). - If
$SNAPSHOT_DIR/$CURRENT_DATE
contains a snapshots created within the past hour, it will unmount@.snapshots
and exit without any operation. - Otherwise, Create a snapshot using
btrfs subvolume snapshot / "$SNAPSHOT_DIR/$CURRENT_DATE/@$CURRENT_TIME"
(e.g./.snapshots/autosnap/2023-09-30/@09-00-00
). - Unmount
@.snapshots
subvolume and exit.
Skip step 3 above and create a snapshot now.
Print all snapshots in /.snapshots/autosnap
using tree
.
Print all snapshots in /.snapshots/autosnap
as a list.
Delete all snapshots created a week ago.
Delete all snapshots except the one currently mounted at /
.
Check the number of snapshots in yesterday's directory, and if it is greater than 1, keep the oldest one and delete all the rest.
Mount @.snapshots
subvolume to /.snapshots
and exit. It will not automatically unmount the snapshots subvolume so that the user can manually inspect it.
Mount a specified snapshot.
Check if the snapshot specified in the argument exists. If it does, create a Systemd-boot config file backup (e.g. /boot/loader/entries/arch.conf.202309301800.bak
) and then modify the mount options in the config file so that the snapshot will be mounted on the next reboot.
Restore to a specified snapshot.
- Create a snapshot. (
./autosnap.sh now
) - Backup the snapshot specified in the argument. (e.g.
btrfs subvolume snapshot "2023-09-30/@09-00-00" "2023-09-30/@09-00-00.bak"
) - Mount to the specified snapshot. (e.g.
./autosnap.sh mount 2023-09-30/@09-00-00
)