-
-
Save frippe75/6adb2d474cc2b64994ed456110f7ae40 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 60 # 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 | |
backup_name=$(date +%Y%m%d-%T) | |
# Exit in error if given arg is not a subvolume | |
sudo btrfs subvolume show $1 1>/dev/null || exit 1 | |
if [ ! -d $1/.snapshots ] | |
then | |
echo Missing intermidiate dir | |
sudo mkdir $1/.snapshots | |
fi | |
# Run the backup | |
btrfs subvolume snapshot -r $1 $1/.snapshots/$backup_name | |
# Delete all but the newest n backups in the snapshots directory | |
for snapshot in $(ls -1t $1/.snapshots | head -n -$2) | |
do | |
btrfs subvolume delete $1/.snapshots/$snapshot | |
done |
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=10 | |
[Install] | |
WantedBy=timers.target |
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
# crontab entry for every 5th minutes | |
*/5 * * * * /usr/local/bin/btrfs_backup /home 60 |
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/bash | |
# Use crontab or systemd | |
USE_CRONTAB=false | |
# Copy into place | |
sudo cp btrfs_backup.sh /usr/local/bin/btrfs_backup | |
if [[ "$USE_CRONTAB" ]] | |
then | |
echo Installing using crontab | |
crontab crontab-entry-5th-min | |
else | |
echo Installing using systemd | |
sudo cp btrfs_backup.service /etc/systemd/system/ | |
sudo cp btrfs_backup.timer /etc/systemd/system/ | |
sudo systemctl enable btrfs_backup.service | |
sudo systemctl start btrfs_backup.service | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment