backup-automation/immich/immich-backup.sh

56 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2024-08-04 23:10:36 +02:00
#!/bin/bash
SCRIPT_ABS_LOCATION=$(realpath "$(dirname "${0}")")
source $SCRIPT_ABS_LOCATION/immich-backup.env
source $SCRIPT_ABS_LOCATION/../logger.sh
# Container Names
APP_CONTAINER="immich_server"
DB_CONTAINER="immich_postgres"
BACKUP_CONTAINER="duplicati"
# Database Settings
DB_USER="postgres"
SOURCE_DIR="/mnt/data/immich"
BACKUP_DESTINATION="$BACKUP_DESTINATION"
BACKUP_ENCR_PASSPHRASE="$BACKUP_ENCR_PASSPHRASE"
# Cloud Storage Authentication
SFTP_USERNAME="$SFTP_USERNAME"
SFTP_PASSWORD="$SFTP_PASSWORD"
SFTP_FINGERPRINT="$SFTP_FINGERPRINT"
# Log file
LOG_FILE="$SCRIPT_ABS_LOCATION/immich-backup.log"
# 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=postgres \
--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."