Btrbk machine started snapshots automatically - nutthawit/alpine-dotfile GitHub Wiki
Create btrfs configurations for / (root_fs) and /home
cat << EOF > /etc/btrbk/[email protected]
compat busybox
timestamp_format long
snapshot_dir /mnt/btr_pool/btrbk_snapshots
subvolume /mnt/btr_pool/@
EOF
cat << EOF > /etc/btrbk/btrbk-home.conf
compat busybox
timestamp_format long
snapshot_dir /mnt/btr_pool/btrbk_snapshots
subvolume /mnt/btr_pool/home
EOFCreating a snapshot script for system startup
This script will create a snapshot for / and /home and rename them to @.machine_start.20251006T0809 and home.machine_start.20251006T0809 respectively.
The snapshots will be stored across reboots and will be deleted on the next machine startup.
cat << 'EOF' > /etc/local.d/create-machine-start-snapshot.start
#!/bin/sh
TARGET_DIR="${1:-/mnt/btr_pool/btrbk_snapshots}"
# Delete last machine start snapshots
find "$TARGET_DIR" -maxdepth 1 -name "*machine_start*" -type d -print -exec btrfs subvolume del {} \;
# Create snapshots
snap @ machine_start
snap home machine_start
EOF
chmod +x /etc/local.d/create-machine-start-snapshot.startCreate a wrapper script
cat << 'EOF' > /usr/local/bin/snap
#!/bin/ash
# snap
show_example() {
echo "Usage: $0 <subvol_name> <description>"
echo "Example: $0 @ firefox_installed"
}
if [ $# -eq 0 ]; then
show_example
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run with 'sudo' or as the 'root' user."
exit 1
fi
subvol_name="$1"
description="$2"
if [ "$subvol_name" == "@" ]; then
btrbk snapshot -c /etc/btrbk/[email protected] @ | rename-snapshot "$description"
elif [ "$subvol_name" == "home" ]; then
btrbk snapshot -c /etc/btrbk/btrbk-home.conf home | rename-snapshot "$description"
else
show_example
exit 1
fi
EOF
chmod +x /usr/local/bin/snapCreate script that used to the rename snapshot
cat << 'EOF' > /usr/local/bin/rename-snapshot
#!/bin/sh
# rename-snapshot
# Check if description parameter is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <description>"
echo "Example: btrbk snapshot -c /etc/btrbk/[email protected] @ | $0 firefox_installed"
exit 1
fi
description="$1"
# Read from stdin if piped, otherwise use first argument
if [ ! -t 0 ]; then
# Reading from pipe
input=$(cat)
fi
# Extract just the snapshot name (home.20251006T0809 or @.20251006T0809)
snapshot_name=$(echo "$input" | grep -oE '(home|@)\.[0-9]{8}T[0-9]{4}')
if [ -z "$snapshot_name" ]; then
echo "Error: Could not extract snapshot name from input"
exit 1
fi
subvol="${snapshot_name%.*}" # Everything before the last dot
timestamp="${snapshot_name#*.}" # Everything after the first dot
# New name format with custom description
new_name=${subvol}.${description}.${timestamp}
# Rename the snapshot
mv "/mnt/btr_pool/btrbk_snapshots/$snapshot_name" "/mnt/btr_pool/btrbk_snapshots/$new_name"
echo "Renamed: $snapshot_name -> $new_name"
EOF
chmod +x /usr/local/bin/rename-snapshotSee also
https://wiki.gentoo.org/wiki//etc/local.d