Last active
September 14, 2022 01:27
-
-
Save MolarFox/eda95b5f9b4d25d5f39cd86c24ffa524 to your computer and use it in GitHub Desktop.
btrfs snapshot automation script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# systemd main unit file for btrfs incremental snapshotting | |
[Unit] | |
Description=Execute btrfs snapshot routine for given volumes | |
[Service] | |
Type=oneshot | |
ExecStart=/usr/local/bin/btrfs_backup /home 48 # Choose partition mount point and num backups to keep here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# MolarFox 2021 | |
# Shell script to run basic btrfs snapshot backup routine on a btrfs volume - does not perform incremental sending to external btrfs partition | |
# btrfs_backup.sh <btrfs_volume> <num_backups_to_keep> | |
# Expects base directory of btrfs (sub)volume to be backed up in argv[1] / $1 | |
# Expects number of recent backups to keep in argv[2] / $2 | |
pacman -Q > /root/installed_packages # keep a list of installed stuff, in case fresh re-install needed (for arch linux, replace w/ package manager for whichever distro) | |
backup_name=$(date --utc | sed -r 's/ /_/g') # name backup by current UTC time | |
# Run the backup | |
btrfs subvolume snapshot -r $1 $1/.snapshots/$backup_name | |
# Delete all but the newest n backups in the snapshots directory | |
old_backups=$( ls -1t $1/.snapshots | head -n -$2 ) | |
if [ $(echo -n $old_backups | grep -c '^') -gt 0 ] # if there are backups to delete | |
then | |
while IFS= read -r line; do # iterate and delete the backup subvolumes | |
btrfs subvolume delete $1/.snapshots/$line | |
done <<< $old_backups | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# systemd timer unit file for btrfs incremental snapshotting | |
[Unit] | |
Description=Run btrfs snapshot on selected drives every 5 mins | |
[Timer] | |
OnBootSec=5min | |
OnUnitActiveSec=5min # modify these to change snapshot frequency | |
RandomizedDelaySec=20 | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment