temporarily disable zfs backups

Signed-off-by: Menno van Leeuwen <menno@vleeuwen.me>
This commit is contained in:
2024-11-15 20:44:35 +01:00
parent 0c92e38370
commit cc7686668c

View File

@ -163,145 +163,145 @@
}; };
}; };
environment.etc."local/bin/zfs-backup.sh" = { # environment.etc."local/bin/zfs-backup.sh" = {
mode = "0755"; # mode = "0755";
text = '' # text = ''
#!/bin/bash # #!/bin/bash
set -euo pipefail # set -euo pipefail
DATE=$(date +%Y%m%d-%H%M) # DATE=$(date +%Y%m%d-%H%M)
DATASETS="music astro photos stash isos ai audiobooks vms old_backups services" # DATASETS="music astro photos stash isos ai audiobooks vms old_backups services"
RETAIN_SNAPSHOTS=24 # RETAIN_SNAPSHOTS=24
BACKUP_POOL="backup" # BACKUP_POOL="backup"
SOURCE_POOL="datapool" # SOURCE_POOL="datapool"
log() { # log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" # echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
} # }
ensure_backup_pool() { # ensure_backup_pool() {
if ! zpool list "$BACKUP_POOL" >/dev/null 2>&1; then # if ! zpool list "$BACKUP_POOL" >/dev/null 2>&1; then
log "ERROR: Backup pool '$BACKUP_POOL' does not exist!" # log "ERROR: Backup pool '$BACKUP_POOL' does not exist!"
return 1 # return 1
fi # fi
} # }
check_dataset_exists() { # check_dataset_exists() {
local pool=$1 # local pool=$1
local dataset=$2 # local dataset=$2
zfs list "$pool/$dataset" >/dev/null 2>&1 # zfs list "$pool/$dataset" >/dev/null 2>&1
return $? # return $?
} # }
create_backup_dataset() { # create_backup_dataset() {
local dataset=$1 # local dataset=$1
local source_pool="$SOURCE_POOL" # local source_pool="$SOURCE_POOL"
local backup_pool="$BACKUP_POOL" # local backup_pool="$BACKUP_POOL"
# Get properties from source dataset # # Get properties from source dataset
local props=$(zfs get -H -o property,value all "$source_pool/$dataset" | \ # local props=$(zfs get -H -o property,value all "$source_pool/$dataset" | \
grep -E '^(compression|recordsize|atime|relatime|xattr|acltype)' | \ # grep -E '^(compression|recordsize|atime|relatime|xattr|acltype)' | \
awk '{printf "-o %s=%s ", $1, $2}') # awk '{printf "-o %s=%s ", $1, $2}')
log "Creating backup dataset $backup_pool/$dataset with matching properties" # log "Creating backup dataset $backup_pool/$dataset with matching properties"
# shellcheck disable=SC2086 # # shellcheck disable=SC2086
zfs create -p ${props} "$backup_pool/$dataset" # zfs create -p ${props} "$backup_pool/$dataset"
# Set some backup-specific properties # # Set some backup-specific properties
zfs set readonly=on "$backup_pool/$dataset" # zfs set readonly=on "$backup_pool/$dataset"
zfs set snapdir=visible "$backup_pool/$dataset" # zfs set snapdir=visible "$backup_pool/$dataset"
log "Successfully created backup dataset $backup_pool/$dataset" # log "Successfully created backup dataset $backup_pool/$dataset"
} # }
get_latest_snapshot() { # get_latest_snapshot() {
local pool=$1 # local pool=$1
local dataset=$2 # local dataset=$2
local snapshot # local snapshot
snapshot=$(zfs list -t snapshot -H -o name "$pool/$dataset" 2>/dev/null | grep backup- | tail -n1) || true # snapshot=$(zfs list -t snapshot -H -o name "$pool/$dataset" 2>/dev/null | grep backup- | tail -n1) || true
echo "$snapshot" # echo "$snapshot"
} # }
# Ensure backup pool exists # # Ensure backup pool exists
ensure_backup_pool # ensure_backup_pool
for ds in $DATASETS; do # for ds in $DATASETS; do
log "Processing dataset $ds" # log "Processing dataset $ds"
# Check if source dataset exists # # Check if source dataset exists
if ! check_dataset_exists "$SOURCE_POOL" "$ds"; then # if ! check_dataset_exists "$SOURCE_POOL" "$ds"; then
log "Skipping $ds - source dataset $SOURCE_POOL/$ds does not exist" # log "Skipping $ds - source dataset $SOURCE_POOL/$ds does not exist"
continue # continue
fi # fi
# Create backup dataset if it doesn't exist # # Create backup dataset if it doesn't exist
if ! check_dataset_exists "$BACKUP_POOL" "$ds"; then # if ! check_dataset_exists "$BACKUP_POOL" "$ds"; then
log "Backup dataset $BACKUP_POOL/$ds does not exist" # log "Backup dataset $BACKUP_POOL/$ds does not exist"
create_backup_dataset "$ds" # create_backup_dataset "$ds"
fi # fi
# Create new snapshot # # Create new snapshot
local snapshot_name="$SOURCE_POOL/$ds@backup-$DATE" # local snapshot_name="$SOURCE_POOL/$ds@backup-$DATE"
log "Creating new snapshot $snapshot_name" # log "Creating new snapshot $snapshot_name"
zfs snapshot "$snapshot_name" # zfs snapshot "$snapshot_name"
LATEST_BACKUP=$(get_latest_snapshot "$BACKUP_POOL" "$ds") # LATEST_BACKUP=$(get_latest_snapshot "$BACKUP_POOL" "$ds")
if [ -z "$LATEST_BACKUP" ]; then # if [ -z "$LATEST_BACKUP" ]; then
log "No existing backup found - performing full backup of $ds" # log "No existing backup found - performing full backup of $ds"
zfs send "$snapshot_name" | zfs receive -F "$BACKUP_POOL/$ds" # zfs send "$snapshot_name" | zfs receive -F "$BACKUP_POOL/$ds"
else # else
LATEST_SOURCE=$(get_latest_snapshot "$SOURCE_POOL" "$ds" | grep -v "backup-$DATE" | tail -n1) # LATEST_SOURCE=$(get_latest_snapshot "$SOURCE_POOL" "$ds" | grep -v "backup-$DATE" | tail -n1)
if [ -n "$LATEST_SOURCE" ]; then # if [ -n "$LATEST_SOURCE" ]; then
log "Performing incremental backup of $ds from $LATEST_SOURCE to backup-$DATE" # log "Performing incremental backup of $ds from $LATEST_SOURCE to backup-$DATE"
zfs send -i "$LATEST_SOURCE" "$snapshot_name" | zfs receive -F "$BACKUP_POOL/$ds" # zfs send -i "$LATEST_SOURCE" "$snapshot_name" | zfs receive -F "$BACKUP_POOL/$ds"
else # else
log "No suitable source snapshot found for incremental backup - performing full backup of $ds" # log "No suitable source snapshot found for incremental backup - performing full backup of $ds"
zfs send "$snapshot_name" | zfs receive -F "$BACKUP_POOL/$ds" # zfs send "$snapshot_name" | zfs receive -F "$BACKUP_POOL/$ds"
fi # fi
fi # fi
log "Cleaning up old snapshots for $ds" # log "Cleaning up old snapshots for $ds"
# Cleanup source snapshots # # Cleanup source snapshots
if snapshots=$(zfs list -t snapshot -H -o name "$SOURCE_POOL/$ds" | grep backup-); then # if snapshots=$(zfs list -t snapshot -H -o name "$SOURCE_POOL/$ds" | grep backup-); then
echo "$snapshots" | head -n -$RETAIN_SNAPSHOTS | while read -r snap; do # echo "$snapshots" | head -n -$RETAIN_SNAPSHOTS | while read -r snap; do
log "Removing source snapshot: $snap" # log "Removing source snapshot: $snap"
zfs destroy "$snap" # zfs destroy "$snap"
done # done
fi # fi
# Cleanup backup snapshots # # Cleanup backup snapshots
if snapshots=$(zfs list -t snapshot -H -o name "$BACKUP_POOL/$ds" | grep backup-); then # if snapshots=$(zfs list -t snapshot -H -o name "$BACKUP_POOL/$ds" | grep backup-); then
echo "$snapshots" | head -n -$RETAIN_SNAPSHOTS | while read -r snap; do # echo "$snapshots" | head -n -$RETAIN_SNAPSHOTS | while read -r snap; do
log "Removing backup snapshot: $snap" # log "Removing backup snapshot: $snap"
zfs destroy "$snap" # zfs destroy "$snap"
done # done
fi # fi
done # done
log "Backup completed successfully" # log "Backup completed successfully"
''; # '';
}; # };
systemd.services.zfs-backup = { # systemd.services.zfs-backup = {
description = "ZFS Backup Service"; # description = "ZFS Backup Service";
requires = [ "zfs.target" ]; # requires = [ "zfs.target" ];
after = [ "zfs.target" ]; # after = [ "zfs.target" ];
path = [ pkgs.zfs ]; # path = [ pkgs.zfs ];
serviceConfig = { # serviceConfig = {
Type = "oneshot"; # Type = "oneshot";
ExecStart = "/etc/local/bin/zfs-backup.sh"; # ExecStart = "/etc/local/bin/zfs-backup.sh";
User = "root"; # User = "root";
}; # };
}; # };
systemd.timers.zfs-backup = { # systemd.timers.zfs-backup = {
description = "Run ZFS backup every 4 hours"; # description = "Run ZFS backup every 4 hours";
wantedBy = [ "timers.target" ]; # wantedBy = [ "timers.target" ];
timerConfig = { # timerConfig = {
OnBootSec = "15min"; # OnBootSec = "15min";
OnUnitActiveSec = "4h"; # OnUnitActiveSec = "4h";
RandomizedDelaySec = "5min"; # RandomizedDelaySec = "5min";
}; # };
}; # };
} }