I've been trying to understand some of the methods for doing incremental backups with btrfs but I'm not that good of a bash shell programmer. I have managed to create some scripts that work for me and thought I'd document them here in case they might be helpful for others.
My goal was to run daily snapshots and then create a backup from those snapshots so I can recover from a disaster. I also manually create snapshots and backups before major upgrade events in case I need to roll back those changes, but that is independent of my daily stuff.
Assumptions:
1. In my /etc/fstab I have an entry to mount my root (/) at /subvol, and an entry for my external drive to mount at /data.
2. The external drive is formatted as btrfs
3. My snapshots are stored in /subvol/snapshots.
4. My backups are stored in /data/snapshots.
To get the whole thing started I needed an initial script to create the first snapshot and backup.
create-initial-snapshot-backup.sh
Next I needed a script to do the daily incremental stuff
incremental-snapshot-backup.sh
For my special snapshot-backups before major events I use the following script.
create-date-tagged-snapshot-backup.sh
To run the daily script I use a systemd timer and service. So in /etc/systemd/system I have the following files:
kubuntu-btrfs-snapshot.service
kubuntu-btrfs-snapshot.timer
Once those files are created you just need to enable the timer.
My goal was to run daily snapshots and then create a backup from those snapshots so I can recover from a disaster. I also manually create snapshots and backups before major upgrade events in case I need to roll back those changes, but that is independent of my daily stuff.
Assumptions:
1. In my /etc/fstab I have an entry to mount my root (/) at /subvol, and an entry for my external drive to mount at /data.
2. The external drive is formatted as btrfs
3. My snapshots are stored in /subvol/snapshots.
4. My backups are stored in /data/snapshots.
To get the whole thing started I needed an initial script to create the first snapshot and backup.
create-initial-snapshot-backup.sh
Code:
#!/bin/bash # if [[ $EUID -ne 0 ]]; then echo -e "must be run as root. use sudo" exit 1 fi btrfs su sn -r /subvol/@ /subvol/snapshots/@_backup btrfs su sn -r /subvol/@home /subvol/snapshots/@home_backup sync btrfs send /subvol/snapshots/@_backup | btrfs receive /data/snapshots btrfs send /subvol/snapshots/@home_backup | btrfs receive /data/snapshots
incremental-snapshot-backup.sh
Code:
#!/bin/bash # exec 1> >(logger -s -t $(basename $0)) 2>&1 # Log the script activity if [[ $EUID -ne 0 ]]; then echo -e "must be run as root. use sudo" exit 1 fi btrfs su sn -r /subvol/@ /subvol/snapshots/@_backup-new sync btrfs su sn -r /subvol/@home /subvol/snapshots/@home_backup-new sync btrfs send -p /subvol/snapshots/@_backup /subvol/snapshots/@_backup-new | btrfs receive /data/snapshots sync btrfs send -p /subvol/snapshots/@home_backup /subvol/snapshots/@home_backu p-new | btrfs receive /data/snapshots sync btrfs su de /subvol/snapshots/@_backup btrfs su de /subvol/snapshots/@home_backup mv /subvol/snapshots/@_backup-new /subvol/snapshots/@_backup mv /subvol/snapshots/@home_backup-new /subvol/snapshots/@home_backup btrfs su de /data/snapshots/@_backup btrfs su de /data/snapshots/@home_backup mv /data/snapshots/@_backup-new /data/snapshots/@_backup mv /data/snapshots/@home_backup-new /data/snapshots/@home_backup
create-date-tagged-snapshot-backup.sh
Code:
#!/bin/bash # if [[ $EUID -ne 0 ]]; then echo -e "must be run as root. use sudo" exit 1 fi snapshot_name=$(date +%Y-%m-%d) btrfs su sn -r /subvol/@ /subvol/snapshots/@_$snapshot_name sync btrfs su sn -r /subvol/@home /subvol/snapshots/@home_$snapshot_name sync btrfs send /subvol/snapshots/@_$snapshot_name | btrfs receive /data/snapshots btrfs send /subvol/snapshots/@home_$snapshot_name | btrfs receive /data/snapshots
kubuntu-btrfs-snapshot.service
Code:
# This service unit is for daily btrfs-snapshots # [Unit] Description=Daily btrfs snapshots [Service] Type=oneshot ExecStart=/usr/local/bin/incremental-snapshot-backup.sh
Code:
# This timer unit is for kubuntu-btrfs-snapshot.service # [Unit] Description=start kubuntu-btrfs-snapshot.service [Timer] Unit=kubuntu-btrfs-snapshot.service OnCalendar=*-*-* 06:00:00 AccuracySec=5minutes RandomizedDelaySec=10minutes Persistent=true [Install] WantedBy=timers.target
Code:
sudo systemctl enable --now kubuntu-btrfs-snapshot.timer
Comment