initial commit

This commit is contained in:
2025-12-20 12:42:19 -06:00
commit 8f28d5c47f
4 changed files with 1224 additions and 0 deletions

468
airgap_backup.sh Normal file
View File

@@ -0,0 +1,468 @@
#!/bin/bash
# ==============================================================================
# Script: airgap_backup.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 to a designated mounted drive using rsync. It
# includes pre-run checks to ensure the source files and destination
# are valid, and sends ntfy notifications for each task.
# Usage: ./airgap_backup.sh
# Notes: This script may require sudo/root privileges to access files.
# ==============================================================================
# Use 'sudo su' to ensure the entire script runs with root privileges.
sudo su
# ==============================================================================
# Define Source and Destination Directories
# ==============================================================================
# This makes the script easier to read and modify.
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="/mnt/airgap_drive"
FSTAB_DESTINATION="$BACKUP_MOUNT_POINT/backup/server/os"
BACKUP_DESTINATION="$BACKUP_MOUNT_POINT/backup"
CADDY_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/caddy"
DDCLIENT_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/ddclient"
DNSMASQ_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/dnsmasq"
HOMEASSISTANT_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/homeassistant"
NEXTCLOUD_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/nextcloud"
PAPERLESS_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/paperless-ngx"
VAULTWARDEN_DESTINATION="$BACKUP_MOUNT_POINT/backup/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/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.
if ! mountpoint -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'..."
# Check if the source file for fstab exists.
if [ ! -f "$FSTAB_SOURCE" ]; then
echo "Error: Source file '$FSTAB_SOURCE' not found." >&2
# Send a failure notification
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Source file '$FSTAB_SOURCE' not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
rsync -av --progress "$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
# ==============================================================================
echo "Starting rsync of '$BACKUP_SOURCE' to '$BACKUP_DESTINATION'..."
# Check if the source directory for the backup exists.
if [ ! -d "$BACKUP_SOURCE" ]; then
echo "Error: Backup source directory '$BACKUP_SOURCE' not found." >&2
# Send a failure notification
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Backup source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# The trailing slash on BACKUP_SOURCE is crucial. It tells rsync to copy the
# *contents* of the directory, not the directory itself.
rsync -av --progress "$BACKUP_SOURCE" "$BACKUP_DESTINATION"
# Post-rsync status check for the backup directory.
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'..."
if [ ! -d "$PHOTOS_BACKUP_SOURCE" ]; then
echo "Error: Photos source directory '$PHOTOS_BACKUP_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Photos source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
# Find the most recent file in the directory.
LATEST_BACKUP_FILE=$(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'." >&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_BACKUP_PATH="$PHOTOS_BACKUP_SOURCE/$LATEST_BACKUP_FILE"
rsync -av --progress "$FULL_BACKUP_PATH" "$PHOTOS_DESTINATION"
if [ $? -eq 0 ]; then
echo "Success: Most recent Photos backup '$LATEST_BACKUP_FILE' copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Most recent Photos backup copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy photos backup file '$LATEST_BACKUP_FILE'. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy most recent Photos backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
fi
fi
# ==============================================================================
# Subtask 3.2: Sync photos/library directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_LIBRARY_SOURCE' to '$PHOTOS_DESTINATION'..."
if [ ! -d "$PHOTOS_LIBRARY_SOURCE" ]; then
echo "Error: Photos source directory '$PHOTOS_LIBRARY_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Photos library source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
rsync -av --progress "$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
fi
# ==============================================================================
# Subtask 3.3: Sync photos/profile directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_PROFILE_SOURCE' to '$PHOTOS_DESTINATION'..."
if [ ! -d "$PHOTOS_PROFILE_SOURCE" ]; then
echo "Error: Photos source directory '$PHOTOS_PROFILE_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Photos profile source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
rsync -av --progress "$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
fi
# ==============================================================================
# Subtask 3.4: Sync photos/upload directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_UPLOAD_SOURCE' to '$PHOTOS_DESTINATION'..."
if [ ! -d "$PHOTOS_UPLOAD_SOURCE" ]; then
echo "Error: Photos source directory '$PHOTOS_UPLOAD_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Photos upload source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
rsync -av --progress "$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
fi
# ==============================================================================
# Task 4: Sync Caddyfile
# ==============================================================================
echo "Starting rsync of '$CADDYFILE_SOURCE' to '$CADDY_DESTINATION'..."
# Check if the source file for the Caddyfile exists.
if [ ! -f "$CADDYFILE_SOURCE" ]; then
echo "Error: Source file '$CADDYFILE_SOURCE' not found." >&2
# Send a failure notification
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Caddyfile source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
rsync -av --progress "$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'..."
# Check if ddclient.conf exists.
if [ ! -f "$DDCLIENT_CONF_SOURCE" ]; then
echo "Error: Source file '$DDCLIENT_CONF_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. ddclient.conf source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
rsync -av --progress "$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
fi
# Check if ddclient default configuration exists.
if [ ! -f "$DDCLIENT_DEFAULT_SOURCE" ]; then
echo "Error: Source file '$DDCLIENT_DEFAULT_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. ddclient default source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
rsync -av --progress "$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
fi
# ==============================================================================
# Task 6: Sync dnsmasq configuration
# ==============================================================================
echo "Starting rsync of dnsmasq configuration file to '$DNSMASQ_DESTINATION'..."
# Check if dnsmasq.conf exists.
if [ ! -f "$DNSMASQ_CONF_SOURCE" ]; then
echo "Error: Source file '$DNSMASQ_CONF_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. dnsmasq.conf source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
rsync -av --progress "$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
fi
# ==============================================================================
# Task 7: Sync Home Assistant backup
# ==============================================================================
echo "Starting rsync of the most recent Home Assistant backup file to '$HOMEASSISTANT_DESTINATION'..."
# Check if the Home Assistant backup source directory exists.
if [ ! -d "$HOMEASSISTANT_BACKUP_SOURCE_DIR" ]; then
echo "Error: Home Assistant backup directory '$HOMEASSISTANT_BACKUP_SOURCE_DIR' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Home Assistant backup directory not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
# Find the most recent file in the directory.
LATEST_BACKUP_FILE=$(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'." >&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_BACKUP_PATH="$HOMEASSISTANT_BACKUP_SOURCE_DIR/$LATEST_BACKUP_FILE"
rsync -av --progress "$FULL_BACKUP_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
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..."
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'..."
# Check if the Nextcloud backup source directory exists.
if [ ! -d "$NEXTCLOUD_BACKUP_SOURCE" ]; then
echo "Error: Nextcloud backup source directory '$NEXTCLOUD_BACKUP_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Nextcloud backup source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# 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 "$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..."
sudo docker compose exec -T webserver 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'..."
# Check if the Paperless-ngx backup source directory exists.
if [ ! -d "$PAPERLESS_SOURCE" ]; then
echo "Error: Paperless-ngx backup source directory '$PAPERLESS_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Paperless-ngx backup source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
rsync -av --progress "$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..."
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 "$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