#!/bin/bash SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")") # shellcheck disable=SC1090 source "$SCRIPT_ABS_LOCATION/immich-backup.env" # shellcheck disable=SC1090 source "$SCRIPT_ABS_LOCATION/../logger.sh" # Container Names DB_CONTAINER="immich_postgres" BACKUP_CONTAINER="duplicati" # Log file LOG_FILE="$SCRIPT_ABS_LOCATION/immich-backup.log" # Database Settings DB_USER="postgres" SOURCE_DIR="/mnt/data/immich" # Dump Database log "Dumping immich postgresql database..." DB_TMP_BAK_NAME="postgres_$(date +"%Y%m%d").bak" docker exec -t "$DB_CONTAINER" pg_dumpall \ --clean \ --if-exists \ --username="$DB_USER" \ --file="/var/lib/postgresql/data/$DB_TMP_BAK_NAME" || { log "Error: Failed to dump the immich database."; exit 1; } # Backup all files to target destination log "Backing up immich files and database..." docker exec "$BACKUP_CONTAINER" duplicati-cli backup "ssh://$BACKUP_DESTINATION" \ "$SOURCE_DIR/data/library" \ "$SOURCE_DIR/data/upload" \ "$SOURCE_DIR/data/profile" \ "$SOURCE_DIR/postgres/$DB_TMP_BAK_NAME" \ --backup-name="immich backup" \ --keep-versions=7 \ --auth-username="$SFTP_USERNAME" \ --auth-password="$SFTP_PASSWORD" \ --passphrase="$BACKUP_ENCR_PASSPHRASE" \ --ssh-fingerprint="$SFTP_FINGERPRINT" \ --prefix="immich" || { log "Error: Failed to backup immich files and database."; exit 1; } # Delete temporary backup files log "Deleting temporary backup files..." docker exec "$DB_CONTAINER" rm "/var/lib/postgresql/data/$DB_TMP_BAK_NAME" || { log "Error: Failed to delete temporary backup files."; exit 1; } log "immich backup completed successfully."