Skip to content

Instantly share code, notes, and snippets.

@JSH32
Created April 7, 2024 20:08
Show Gist options
  • Save JSH32/7857ef813d82618353bd2855c7bae461 to your computer and use it in GitHub Desktop.
Save JSH32/7857ef813d82618353bd2855c7bae461 to your computer and use it in GitHub Desktop.
Immich backup+db script designed to work with Backblaze B2 and rclone
#!/bin/bash
# Modify with the path to your Immich data/upload directory
IMMICH_DATA_DIR="/mnt/media/immich"
# Modify with the path to your local db backup directory
DB_BACKUP_DIR="/mnt/media/immich_db_backups"
# Your configured rclone path to B2 bucket
B2_BUCKET="backblaze:hydronbackup"
# Database container name
DB_CONTAINER_NAME="immich_postgres"
DATE=$(date +%Y-%m-%d)
# Step 1: Database dump
echo "Creating database dump..."
docker exec -t $DB_CONTAINER_NAME pg_dumpall --clean --if-exists --username=postgres | gzip > "$DB_BACKUP_DIR/$DATE.dump.sql.gz"
# Step 2: Upload the database dump
echo "Uploading database dump to B2..."
rclone copy "$DB_BACKUP_DIR/$DATE.dump.sql.gz" "${B2_BUCKET}/immich/database" --progress
# Step 3: Sync the Immich data directory
echo "Syncing immich data directory to B2..."
rclone sync $IMMICH_DATA_DIR "${B2_BUCKET}/immich/storage" --progress
# Step 4: Cleanup old dumps
echo "Cleaning up old database dumps..."
# We list the dumps sorted by date, skip the latest 10, and remove the rest
rclone lsf --files-only "${B2_BUCKET}/immich/database" | sort | head -n -10 | while read -r old_dump; do
rclone delete "${B2_BUCKET}/immich/database/${old_dump}"
rm "$DB_BACKUP_DIR/${old_dump}"
echo "Removed old dump: $old_dump"
done
echo "Backup and sync process completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment