Created photos backup script

This commit is contained in:
2026-09-20 10:42:45 -05:00
parent a822f0b2c9
commit f535f56336
3 changed files with 140 additions and 0 deletions
+140
View File
@@ -0,0 +1,140 @@
#!/bin/bash
# ==============================================================================
# Script: photos_backup.sh
# Description: This script securely backs up Immich 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 ./photos_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"
# Photo backup directories
PHOTOS_BACKUP_SOURCE="/srv/photos/backups/"
PHOTOS_LIBRARY_SOURCE="/srv/photos/library/"
PHOTOS_PROFILE_SOURCE="/srv/photos/profile/"
PHOTOS_UPLOAD_SOURCE="/srv/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 "$BACKUP_DESTINATION"
mkdir -p "$PHOTOS_DESTINATION"
# ==============================================================================
# Backup Photos
# ==============================================================================
# ==============================================================================
# Task 1: Sync most recent database backup located in photos/backups directory
# ==============================================================================
echo "Starting rsync of the most recent database backup file in '$PHOTOS_BACKUP_SOURCE' to '$PHOTOS_DESTINATION'..."
if [ ! -d "$PHOTOS_BACKUP_SOURCE" ]; then
echo "Error: Immich database backup source directory '$PHOTOS_BACKUP_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. No database backup directory 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 Immich database backup directory '$PHOTOS_BACKUP_SOURCE'." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: No database backup files found." "$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 Immich database backup '$LATEST_BACKUP_FILE' copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Most recent Immich database backup copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy most recent 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
fi
# ==============================================================================
# Task 2: Sync photos/library directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_LIBRARY_SOURCE' to '$PHOTOS_DESTINATION'..."
if [ ! -d "$PHOTOS_LIBRARY_SOURCE" ]; then
echo "Error: Immich Library source directory '$PHOTOS_LIBRARY_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Immich 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: Immich library directory copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Immich library copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy Immich library directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Immich library directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
fi
# ==============================================================================
# Task 3: Sync photos/profile directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_PROFILE_SOURCE' to '$PHOTOS_DESTINATION'..."
if [ ! -d "$PHOTOS_PROFILE_SOURCE" ]; then
echo "Error: Immich Profile source directory '$PHOTOS_PROFILE_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Immich Profile directory copy failed. Profile directory 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: Immich profile directory copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Immich profile directory copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy Immich profile directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Immich profile directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
fi
# ==============================================================================
# Task 4: Sync photos/upload directory
# ==============================================================================
echo "Starting rsync of '$PHOTOS_UPLOAD_SOURCE' to '$PHOTOS_DESTINATION'..."
if [ ! -d "$PHOTOS_UPLOAD_SOURCE" ]; then
echo "Error: Immich upload source directory '$PHOTOS_UPLOAD_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Immich Upload directory copy failed. Upload directory 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: Immich upload directory copied to '$PHOTOS_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Immich upload directory copied to $PHOTOS_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy Immich upload directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Immich upload directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
fi
fi