Update backups cript

This commit is contained in:
albert 2024-12-15 19:43:40 -06:00
parent 799c417f90
commit 47ae1f2930
Signed by: albert
GPG key ID: 3895DD267CA11BA9
2 changed files with 15 additions and 15 deletions

28
nixos/common/software/cli/scripts/btrfs-backup.sh Normal file → Executable file
View file

@ -13,7 +13,7 @@ SNAPPER_CONFIG="$1"
# Get the actual snapshot location from snapper config # Get the actual snapshot location from snapper config
SOURCE_PATH=$(sudo snapper -c "$SNAPPER_CONFIG" get-config | grep '^SUBVOLUME' | cut -d'=' -f2 | tr -d '"'| awk {'print $3'}) SOURCE_PATH=$(sudo snapper -c "$SNAPPER_CONFIG" get-config | grep '^SUBVOLUME' | cut -d'=' -f2 | tr -d '"'| awk {'print $3'})
echo "SOURCE_PATH: $SOUCE_PATH" echo "SOURCE_PATH: $SOURCE_PATH"
if [ -z "$SOURCE_PATH" ]; then if [ -z "$SOURCE_PATH" ]; then
echo "ERROR: Could not determine snapshot path for config '$SNAPPER_CONFIG'" echo "ERROR: Could not determine snapshot path for config '$SNAPPER_CONFIG'"
exit 1 exit 1
@ -26,11 +26,17 @@ if [ ! -d "$SNAPSHOT_PATH" ]; then
exit 1 exit 1
fi fi
# Get latest snapshot number from snapper
LATEST_SNAPSHOT=$(sudo snapper -c "$SNAPPER_CONFIG" list | tail -n 1 | awk '{print $1}')
REMOTE_HOST="root@synology" REMOTE_HOST="root@synology"
DEST_PATH="/volume1/backups/btrfs_`hostname`_${SNAPPER_CONFIG}" DEST_PATH="/volume1/BTRFS_Receives/`hostname`/${SNAPPER_CONFIG}/${LATEST_SNAPSHOT}"
STATE_FILE="/var/lib/snapper-backup-${SNAPPER_CONFIG}.state" STATE_FILE="/var/lib/snapper-backup-${SNAPPER_CONFIG}.state"
LOG_FILE="/var/log/snapper-backup-${SNAPPER_CONFIG}.log" LOG_FILE="/var/log/snapper-backup-${SNAPPER_CONFIG}.log"
KEEP_SNAPSHOTS=5 KEEP_SNAPSHOTS=5
# Get latest successful transfer number
LAST_TRANSFERRED=$(cat "$STATE_FILE" 2>/dev/null || echo "")
# Ensure we exit on any error # Ensure we exit on any error
set -e set -e
@ -43,7 +49,7 @@ fi
# Logging function # Logging function
log() { log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | sudo tee -a "$LOG_FILE"
} }
# Function to verify snapshot exists # Function to verify snapshot exists
@ -94,12 +100,6 @@ log "Using snapshot path: $SNAPSHOT_PATH"
# Verify remote connectivity first # Verify remote connectivity first
verify_remote verify_remote
# Get latest successful transfer number
LAST_TRANSFERRED=$(cat "$STATE_FILE" 2>/dev/null || echo "")
# Get latest snapshot number from snapper
LATEST_SNAPSHOT=$(sudo snapper -c "$SNAPPER_CONFIG" list | tail -n 1 | awk '{print $1}')
# Verify snapshots exist # Verify snapshots exist
verify_snapshot "$LATEST_SNAPSHOT" || exit 1 verify_snapshot "$LATEST_SNAPSHOT" || exit 1
if [ -n "$LAST_TRANSFERRED" ]; then if [ -n "$LAST_TRANSFERRED" ]; then
@ -120,9 +120,9 @@ if [ -z "$LAST_TRANSFERRED" ]; then
# First time backup - full send # First time backup - full send
log "Performing full send of snapshot $LATEST_SNAPSHOT" log "Performing full send of snapshot $LATEST_SNAPSHOT"
sudo btrfs send "$SNAPSHOT_PATH/$LATEST_SNAPSHOT/snapshot" | \ sudo btrfs send "$SNAPSHOT_PATH/$LATEST_SNAPSHOT/snapshot" | \
pv -bytes | \ pv --bytes | \
ssh "$REMOTE_HOST" "btrfs receive '$DEST_PATH'" && { ssh "$REMOTE_HOST" "btrfs receive '$DEST_PATH'" && {
echo "$LATEST_SNAPSHOT" > "$STATE_FILE" sudo echo "$LATEST_SNAPSHOT" > "$STATE_FILE"
log "Full send completed successfully" log "Full send completed successfully"
} || { } || {
log "ERROR: Full send failed" log "ERROR: Full send failed"
@ -133,9 +133,9 @@ else
log "Performing incremental send from $LAST_TRANSFERRED to $LATEST_SNAPSHOT" log "Performing incremental send from $LAST_TRANSFERRED to $LATEST_SNAPSHOT"
sudo btrfs send -p "$SNAPSHOT_PATH/$LAST_TRANSFERRED/snapshot" \ sudo btrfs send -p "$SNAPSHOT_PATH/$LAST_TRANSFERRED/snapshot" \
"$SNAPSHOT_PATH/$LATEST_SNAPSHOT/snapshot" | \ "$SNAPSHOT_PATH/$LATEST_SNAPSHOT/snapshot" | \
pv -bytes | \ pv --bytes | \
ssh "$REMOTE_HOST" "btrfs receive '$DEST_PATH'" && { ssh "$REMOTE_HOST" "btrfs receive '$DEST_PATH'" && {
echo "$LATEST_SNAPSHOT" > "$STATE_FILE" sudo echo "$LATEST_SNAPSHOT" > "$STATE_FILE"
log "Incremental send completed successfully" log "Incremental send completed successfully"
} || { } || {
log "ERROR: Incremental send failed" log "ERROR: Incremental send failed"
@ -148,7 +148,7 @@ cleanup_remote_snapshots
# Verify remote snapshots # Verify remote snapshots
log "Current remote snapshots:" log "Current remote snapshots:"
get_remote_snapshots | tee -a "$LOG_FILE" get_remote_snapshots | sudo tee -a "$LOG_FILE"
# Final verification # Final verification
if ! ssh "$REMOTE_HOST" "btrfs subvolume show '$DEST_PATH/snapshot'" &>/dev/null; then if ! ssh "$REMOTE_HOST" "btrfs subvolume show '$DEST_PATH/snapshot'" &>/dev/null; then

@ -1 +1 @@
Subproject commit 8ed82c9afaac6359cf4603196f7a5bd4b399a4b5 Subproject commit 26552d8bade6fa39188ebf68e9ffa1a8dcd75108