Files
media-server/airgap_backup_remote.sh
2025-12-20 12:42:19 -06:00

396 lines
22 KiB
Bash

#!/bin/bash
# ==============================================================================
# Script: airgap_backup_remote.sh
# Description: This script securely copies the /etc/fstab file, a backup
# directory, several photo directories, the Caddyfile, and
# ddclient, dnsmasq, home assistant, Nextcloud, and Paperless-ngx
# configurations from a remote server to a designated mounted drive
# using rsync over SSH. It includes pre-run checks to ensure the
# source files and destination are valid, and sends ntfy
# notifications for each task.
# Usage: ./airgap_backup_remote.sh
# Notes: This script requires SSH access to the remote server without a
# password prompt (e.g., using SSH keys). It also requires the
# 'mattspeer' user to be able to run rsync with sudo on the remote
# host without a password.
# ==============================================================================
# ==============================================================================
# Define Source and Destination Directories
# ==============================================================================
# This makes the script easier to read and modify.
REMOTE_USER="mattspeer"
REMOTE_HOST="192.168.4.20"
FSTAB_SOURCE="/etc/fstab"
BACKUP_SOURCE="/mnt/5TB-Disk1/backup/" # Trailing slash is important to sync contents
CADDYFILE_SOURCE="/home/mattspeer/docker/caddy/Caddyfile"
DDCLIENT_CONF_SOURCE="/etc/ddclient.conf"
DDCLIENT_DEFAULT_SOURCE="/etc/default/ddclient"
DNSMASQ_CONF_SOURCE="/home/mattspeer/docker/dnsmasq/dnsmasq.conf"
HOMEASSISTANT_BACKUP_SOURCE_DIR="/home/mattspeer/docker/homeassistant/backups"
NEXTCLOUD_BACKUP_SOURCE="/mnt/5TB-Disk1/backup/nextcloud"
PAPERLESS_SOURCE="/doc-archive/export"
VAULTWARDEN_SOURCE_DIR="/home/mattspeer/docker/vaultwarden/data/"
BACKUP_MOUNT_POINT="/Volumes/2TB SSD"
FSTAB_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/os/fstab/"
BACKUP_DESTINATION="$BACKUP_MOUNT_POINT/backup"
CADDY_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/caddy/"
DDCLIENT_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/ddclient/"
DNSMASQ_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/dnsmasq/"
HOMEASSISTANT_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/homeassistant"
NEXTCLOUD_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/nextcloud"
PAPERLESS_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/paperless-ngx"
VAULTWARDEN_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/vaultwarden/"
# Photo backup directories
PHOTOS_BACKUP_SOURCE="/photos/backups"
PHOTOS_LIBRARY_SOURCE="/photos/library"
PHOTOS_PROFILE_SOURCE="/photos/profile"
PHOTOS_UPLOAD_SOURCE="/photos/upload"
PHOTOS_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/services/immich/" # Destination for all photo directories
# ==============================================================================
# ntfy Notification Configuration
# ==============================================================================
# To use this feature, you must have the 'curl' command installed.
# NTFY_TOPIC is the topic you wish to send notifications to.
# NTFY_SERVER is the URL of your ntfy server.
# NTFY_TOKEN is the authorization token for your ntfy server.
NTFY_TOPIC="Server"
NTFY_SERVER="https://ntfy.speerfam.net"
NTFY_TOKEN="tk_xmr066ooldbydhbuolymobfn9b27f"
# ==============================================================================
# Pre-flight Checks (for the destination drive)
# ==============================================================================
# Check if the mount point directory exists and is a mounted filesystem.
# This is a critical check to ensure we are writing to the intended air-gapped
# drive and not to a local directory if the mount failed. This version uses
# a command compatible with macOS.
if ! mount | grep -q "$BACKUP_MOUNT_POINT"; then
echo "Error: '$BACKUP_MOUNT_POINT' is not a mount point. Please ensure the drive is mounted." >&2
# Send a failure notification
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Destination '$BACKUP_MOUNT_POINT' is not mounted." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# Check if the destination directories exist and create them if they don't.
# This prevents rsync from failing on the first run.
mkdir -p "$FSTAB_DESTINATION"
mkdir -p "$BACKUP_DESTINATION"
mkdir -p "$PHOTOS_DESTINATION"
mkdir -p "$CADDY_DESTINATION"
mkdir -p "$DDCLIENT_DESTINATION"
mkdir -p "$DNSMASQ_DESTINATION"
mkdir -p "$HOMEASSISTANT_DESTINATION"
mkdir -p "$NEXTCLOUD_DESTINATION"
mkdir -p "$PAPERLESS_DESTINATION"
mkdir -p "$VAULTWARDEN_DESTINATION"
# ==============================================================================
# Task 1: Sync /etc/fstab
# ==============================================================================
echo "Starting rsync of '$FSTAB_SOURCE' to '$FSTAB_DESTINATION'..."
# Use -e "ssh -t sudo rsync" to run rsync with sudo privileges on the remote host.
# The --rsync-path flag allows you to specify a different rsync binary on the remote
# host. Here we use 'sudo rsync' to gain elevated permissions.
rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$FSTAB_SOURCE" "$FSTAB_DESTINATION"
# Post-rsync status check for fstab.
if [ $? -eq 0 ]; then
echo "Success: fstab has been successfully copied to '$FSTAB_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: fstab copied to $FSTAB_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy fstab. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy fstab." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# ==============================================================================
# Task 2: Sync backup directory
# ==============================================================================
# This task has been commented out. To re-enable it, remove the '#' from the
# beginning of each line below.
# echo "Starting rsync of '$BACKUP_SOURCE' to '$BACKUP_DESTINATION'..."
# rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$BACKUP_SOURCE" "$BACKUP_DESTINATION"
# if [ $? -eq 0 ]; then
# echo "Success: Backup directory has been successfully copied to '$BACKUP_DESTINATION'."
# curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Backup copied to $BACKUP_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# else
# echo "Error: rsync failed to copy the backup directory. Please check permissions." >&2
# curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy backup directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# exit 1
# fi
# ==============================================================================
# Task 3: Sync photo directories
# ==============================================================================
# ==============================================================================
# Subtask 3.1: Sync most recent file in photos/backups directory
# ==============================================================================
echo "Starting rsync of the most recent file in '$PHOTOS_BACKUP_SOURCE' to '$PHOTOS_DESTINATION..."
# Use ssh to run the 'ls' command on the remote host to find the most recent file.
LATEST_BACKUP_FILE=$(ssh "$REMOTE_USER@$REMOTE_HOST" "ls -t '$PHOTOS_BACKUP_SOURCE' | head -n 1")
# Check if a file was found.
if [ -z "$LATEST_BACKUP_FILE" ]; then
echo "Error: No files found in Photos backup directory '$PHOTOS_BACKUP_SOURCE' on the remote host." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: No files found in Photos backup directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
FULL_REMOTE_PATH="$PHOTOS_BACKUP_SOURCE/$LATEST_BACKUP_FILE"
rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$FULL_REMOTE_PATH" "$PHOTOS_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: Most recent Immich database backup '$LATEST_BACKUP_FILE' copied to '$PHOTOS_DESTINATION/backups'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Most recent Immich database backup copied to $PHOTOS_DESTINATION/backups" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy Immich database backup file '$LATEST_BACKUP_FILE'. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy most recent Immich database backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
fi
# ==============================================================================
# Subtask 3.2: Sync photos/library directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_LIBRARY_SOURCE' to '$PHOTOS_DESTINATION'..."
# rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$PHOTOS_LIBRARY_SOURCE" "$PHOTOS_DESTINATION"
rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$PHOTOS_LIBRARY_SOURCE" "$PHOTOS_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: Photos library directory copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Photos library copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy photos library directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy photos library directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
# ==============================================================================
# Subtask 3.3: Sync photos/profile directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_PROFILE_SOURCE' to '$PHOTOS_DESTINATION'..."
# rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$PHOTOS_PROFILE_SOURCE" "$PHOTOS_DESTINATION"
rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$PHOTOS_PROFILE_SOURCE" "$PHOTOS_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: Photos profile directory copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Photos profile copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy photos profile directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy photos profile directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
# ==============================================================================
# Subtask 3.4: Sync photos/upload directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_UPLOAD_SOURCE' to '$PHOTOS_DESTINATION'..."
# rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$PHOTOS_UPLOAD_SOURCE" "$PHOTOS_DESTINATION"
rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$PHOTOS_UPLOAD_SOURCE" "$PHOTOS_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: Photos upload directory copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Photos upload copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy photos upload directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy photos upload directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
# ==============================================================================
# Task 4: Sync Caddyfile
# ==============================================================================
echo "Starting rsync of '$CADDYFILE_SOURCE' to '$CADDY_DESTINATION'..."
rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$CADDYFILE_SOURCE" "$CADDY_DESTINATION"
# Post-rsync status check for the Caddyfile.
if [ $? -eq 0 ]; then
echo "Success: Caddyfile has been successfully copied to '$CADDY_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Caddyfile copied to $CADDY_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy Caddyfile. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Caddyfile." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# ==============================================================================
# Task 5: Sync ddclient configuration
# ==============================================================================
echo "Starting rsync of ddclient configuration files to '$DDCLIENT_DESTINATION'..."
rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$DDCLIENT_CONF_SOURCE" "$DDCLIENT_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: ddclient.conf copied to '$DDCLIENT_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: ddclient.conf copied to $DDCLIENT_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy ddclient.conf. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy ddclient.conf." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
rsync -av --progress --rsync-path="sudo rsync" "$REMOTE_USER@$REMOTE_HOST:$DDCLIENT_DEFAULT_SOURCE" "$DDCLIENT_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: ddclient default configuration copied to '$DDCLIENT_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: ddclient default copied to $DDCLIENT_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy ddclient default configuration. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy ddclient default." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
# ==============================================================================
# Task 6: Sync dnsmasq configuration
# ==============================================================================
echo "Starting rsync of dnsmasq configuration file to '$DNSMASQ_DESTINATION'..."
rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$DNSMASQ_CONF_SOURCE" "$DNSMASQ_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: dnsmasq.conf copied to '$DNSMASQ_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: dnsmasq.conf copied to $DNSMASQ_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy dnsmasq.conf. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy dnsmasq.conf." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
# ==============================================================================
# Task 7: Sync Home Assistant backup
# ==============================================================================
echo "Starting rsync of the most recent Home Assistant backup file to '$HOMEASSISTANT_DESTINATION'..."
# Use ssh to run the 'ls' command on the remote host to find the most recent file.
LATEST_BACKUP_FILE=$(ssh "$REMOTE_USER@$REMOTE_HOST" "ls -t '$HOMEASSISTANT_BACKUP_SOURCE_DIR' | head -n 1")
# Check if a file was found.
if [ -z "$LATEST_BACKUP_FILE" ]; then
echo "Error: No files found in Home Assistant backup directory '$HOMEASSISTANT_BACKUP_SOURCE_DIR' on the remote host." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: No files found in Home Assistant backup directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
FULL_REMOTE_PATH="$HOMEASSISTANT_BACKUP_SOURCE_DIR/$LATEST_BACKUP_FILE"
rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$FULL_REMOTE_PATH" "$HOMEASSISTANT_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: Most recent Home Assistant backup '$LATEST_BACKUP_FILE' copied to '$HOMEASSISTANT_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Home Assistant backup copied to $HOMEASSISTANT_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy Home Assistant backup file '$LATEST_BACKUP_FILE'. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Home Assistant backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
fi
# ==============================================================================
# Task 8: Sync Nextcloud backup
# ==============================================================================
echo "Starting Nextcloud backup process..."
# Step 1: Execute the Docker command to create the backup.
echo "Running Nextcloud AIO daily backup script via Docker..."
ssh "$REMOTE_USER@$REMOTE_HOST" "sudo docker exec --env DAILY_BACKUP=1 nextcloud-aio-mastercontainer /daily-backup.sh"
if [ $? -ne 0 ]; then
echo "Error: Docker command failed to create Nextcloud backup." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Docker command failed to create Nextcloud backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
echo "Nextcloud backup script completed successfully."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Nextcloud daily backup created. Starting rsync." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# Step 2: Rsync the Nextcloud backup directory to the destination.
echo "Starting rsync of '$NEXTCLOUD_BACKUP_SOURCE' to '$NEXTCLOUD_DESTINATION'..."
# The trailing slash on NEXTCLOUD_BACKUP_SOURCE is crucial. It tells rsync to
# copy the *contents* of the directory, not the directory itself.
rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$NEXTCLOUD_BACKUP_SOURCE" "$NEXTCLOUD_DESTINATION"
# Post-rsync status check for the Nextcloud backup directory.
if [ $? -eq 0 ]; then
echo "Success: Nextcloud backup directory has been successfully copied to '$NEXTCLOUD_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Nextcloud backup copied to $NEXTCLOUD_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy the Nextcloud backup directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Nextcloud backup directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# ==============================================================================
# Task 9: Sync Paperless-ngx backup
# ==============================================================================
echo "Starting Paperless-ngx backup process..."
# Step 1: Execute the Docker command to create the backup.
echo "Running Paperless-ngx document exporter via Docker..."
ssh "$REMOTE_USER@$REMOTE_HOST" "sudo docker compose exec -T paperless-ngx-webserver-1 document_exporter ../export -z"
if [ $? -ne 0 ]; then
echo "Error: Docker command failed to create Paperless-ngx backup." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Docker command failed to create Paperless-ngx backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
echo "Paperless-ngx backup script completed successfully."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Paperless-ngx backup created. Starting rsync." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# Step 2: Rsync the Paperless-ngx backup directory to the destination.
echo "Starting rsync of '$PAPERLESS_SOURCE' to '$PAPERLESS_DESTINATION'..."
rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$PAPERLESS_SOURCE" "$PAPERLESS_DESTINATION"
# Post-rsync status check for the Paperless-ngx backup directory.
if [ $? -eq 0 ]; then
echo "Success: Paperless-ngx backup directory has been successfully copied to '$PAPERLESS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Paperless-ngx backup copied to $PAPERLESS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy the Paperless-ngx backup directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Paperless-ngx backup directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# ==============================================================================
# Task 10: Sync Vaultwarden backup
# ==============================================================================
echo "Starting Vaultwarden backup process..."
# Step 1: Execute the Docker command to create the database backup.
echo "Running Vaultwarden backup via Docker..."
ssh "$REMOTE_USER@$REMOTE_HOST" "sudo docker exec -it vaultwarden /vaultwarden backup"
if [ $? -ne 0 ]; then
echo "Error: Docker command failed to create Vaultwarden database backup." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Docker command failed to create Vaultwarden database backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
echo "Vaultwarden database backup created successfully."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Vaultwarden database backup created. Starting rsync." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# Step 2: Rsync the Vaultwarden directories and files to the destination.
echo "Starting rsync of Vaultwarden data to '$VAULTWARDEN_DESTINATION'..."
# Define the Vaultwarden source files and directories to be backed up
# This approach makes the script more readable and scalable.
VAULTWARDEN_SOURCES=(
"$VAULTWARDEN_SOURCE_DIR/attachments"
"$VAULTWARDEN_SOURCE_DIR/sends"
"$VAULTWARDEN_SOURCE_DIR/rsa_key.pem"
"$VAULTWARDEN_SOURCE_DIR/rsa_key.pub"
"$VAULTWARDEN_SOURCE_DIR/icon_cache"
)
# Check if the Vaultwarden source directory exists.
if [ ! -d "$VAULTWARDEN_SOURCE_DIR" ]; then
echo "Error: Vaultwarden backup source directory '$VAULTWARDEN_SOURCE_DIR' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Vaultwarden backup source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# Loop through each source and rsync it.
for source in "${VAULTWARDEN_SOURCES[@]}"; do
rsync -av --progress "$REMOTE_USER@$REMOTE_HOST:$source" "$VAULTWARDEN_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: '$source' copied to '$VAULTWARDEN_DESTINATION'."
else
echo "Error: rsync failed to copy '$source'. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Vaultwarden data." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
done
echo "Success: Vaultwarden backup has been successfully copied to '$VAULTWARDEN_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Vaultwarden backup copied to $VAULTWARDEN_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
echo "All rsync tasks completed."
exit 0