#!/bin/bash # ============================================================================== # Script: paperless_backup.sh # Description: This script securely backs up Paperless-ngx 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: sudo ./hass_backup.sh # Notes: This script may require sudo/root privileges to access files. # ============================================================================== # ============================================================================== # Define Variables for Source and Destination # ============================================================================== # This makes the script easier to read and modify. BACKUP_MOUNT_POINT="/mnt/5TB-Disk1" BACKUP_DESTINATION="$BACKUP_MOUNT_POINT/backup" # Paperless backup directories PAPERLESS_SOURCE="/srv/doc-archive/export" PAPERLESS_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/paperless-ngx" # 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_3xo9mejjtgemyowzckqw2souo3mux" # ============================================================================== # 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 # ============================================================================== # 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 -f /srv/docker/dockhand/data/stacks/Local/paperless-ngx/compose.yaml 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