From a447a20b8c88c74d46e2e96f390a7ed4728b5a02 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 16 Dec 2024 01:41:22 +0100 Subject: [PATCH] Update backup script --- .../software/cli/scripts/btrfs-backup.sh | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/nixos/common/software/cli/scripts/btrfs-backup.sh b/nixos/common/software/cli/scripts/btrfs-backup.sh index 0a70b683..eb53a2c6 100644 --- a/nixos/common/software/cli/scripts/btrfs-backup.sh +++ b/nixos/common/software/cli/scripts/btrfs-backup.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +#!/bin/bash # Check for required argument if [ $# -ne 1 ]; then @@ -9,11 +10,25 @@ fi # Configuration SNAPPER_CONFIG="$1" -SOURCE_PATH="/.snapshots" + +# Get the actual snapshot location from snapper config +SOURCE_PATH=$(snapper -c "$SNAPPER_CONFIG" get-config | grep '^SUBVOLUME' | cut -d'=' -f2 | tr -d '"'| awk {'print $3'}) +if [ -z "$SOURCE_PATH" ]; then + echo "ERROR: Could not determine snapshot path for config '$SNAPPER_CONFIG'" + exit 1 +fi + +# Convert subvolume path to snapshot path +SNAPSHOT_PATH="$SOURCE_PATH/.snapshots" +if [ ! -d "$SNAPSHOT_PATH" ]; then + echo "ERROR: Snapshot directory '$SNAPSHOT_PATH' does not exist" + exit 1 +fi + REMOTE_HOST="root@synology" -DEST_PATH="/volume1/backups/${SNAPPER_CONFIG}" # Added config name to path -STATE_FILE="/var/lib/snapper-backup-${SNAPPER_CONFIG}.state" # Made state file unique per config -LOG_FILE="/var/log/snapper-backup-${SNAPPER_CONFIG}.log" # Made log file unique per config +DEST_PATH="/volume1/backups/btrfs_`hostname`_${SNAPPER_CONFIG}" +STATE_FILE="/var/lib/snapper-backup-${SNAPPER_CONFIG}.state" +LOG_FILE="/var/log/snapper-backup-${SNAPPER_CONFIG}.log" KEEP_SNAPSHOTS=5 # Ensure we exit on any error @@ -33,7 +48,7 @@ log() { # Function to verify snapshot exists verify_snapshot() { local snapshot_num="$1" - if [ ! -d "$SOURCE_PATH/$snapshot_num/snapshot" ]; then + if [ ! -d "$SNAPSHOT_PATH/$snapshot_num/snapshot" ]; then log "ERROR: Snapshot $snapshot_num does not exist" return 1 fi @@ -73,6 +88,7 @@ cleanup_remote_snapshots() { # Start backup process log "Starting backup for snapper config: $SNAPPER_CONFIG" +log "Using snapshot path: $SNAPSHOT_PATH" # Verify remote connectivity first verify_remote @@ -102,7 +118,7 @@ ssh "$REMOTE_HOST" "mkdir -p '$DEST_PATH'" if [ -z "$LAST_TRANSFERRED" ]; then # First time backup - full send log "Performing full send of snapshot $LATEST_SNAPSHOT" - sudo btrfs send "$SOURCE_PATH/$LATEST_SNAPSHOT/snapshot" | \ + sudo btrfs send "$SNAPSHOT_PATH/$LATEST_SNAPSHOT/snapshot" | \ pv -bytes | \ ssh "$REMOTE_HOST" "btrfs receive '$DEST_PATH'" && { echo "$LATEST_SNAPSHOT" > "$STATE_FILE" @@ -114,8 +130,8 @@ if [ -z "$LAST_TRANSFERRED" ]; then else # Incremental send log "Performing incremental send from $LAST_TRANSFERRED to $LATEST_SNAPSHOT" - sudo btrfs send -p "$SOURCE_PATH/$LAST_TRANSFERRED/snapshot" \ - "$SOURCE_PATH/$LATEST_SNAPSHOT/snapshot" | \ + sudo btrfs send -p "$SNAPSHOT_PATH/$LAST_TRANSFERRED/snapshot" \ + "$SNAPSHOT_PATH/$LATEST_SNAPSHOT/snapshot" | \ pv -bytes | \ ssh "$REMOTE_HOST" "btrfs receive '$DEST_PATH'" && { echo "$LATEST_SNAPSHOT" > "$STATE_FILE"