LaunchAgents don't inherit PATH, so use explicit paths: - /opt/homebrew/opt/mise/bin/mise x -- osxphotos (like borgmatic) - /usr/local/bin/docker for Docker commands Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
112 lines
2.7 KiB
Django/Jinja
112 lines
2.7 KiB
Django/Jinja
#!/bin/bash
|
|
# {{ ansible_managed }}
|
|
#
|
|
# Immich photo sync script
|
|
# Exports photos from macOS Photos Library and uploads to Immich
|
|
#
|
|
# Prerequisites:
|
|
# - osxphotos (installed via mise/pipx)
|
|
# - Docker (for immich-cli)
|
|
# - API key at ~/.immich-api-key
|
|
|
|
set -euo pipefail
|
|
|
|
# Explicit paths for LaunchAgent context (no PATH inheritance)
|
|
MISE=/opt/homebrew/opt/mise/bin/mise
|
|
DOCKER=/usr/local/bin/docker
|
|
|
|
EXPORT_DIR="{{ immich_sync_export_dir }}"
|
|
IMMICH_URL="{{ immich_sync_url }}"
|
|
API_KEY_FILE="$HOME/.immich-api-key"
|
|
LOG_PREFIX="[immich-sync]"
|
|
|
|
log() {
|
|
echo "$LOG_PREFIX $(date '+%Y-%m-%d %H:%M:%S') $*"
|
|
}
|
|
|
|
error() {
|
|
echo "$LOG_PREFIX $(date '+%Y-%m-%d %H:%M:%S') ERROR: $*" >&2
|
|
}
|
|
|
|
# Check prerequisites
|
|
if [[ ! -x "$MISE" ]]; then
|
|
error "mise not found at $MISE"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -x "$DOCKER" ]]; then
|
|
error "Docker not found at $DOCKER"
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -f "$API_KEY_FILE" ]]; then
|
|
error "Immich API key not found at $API_KEY_FILE"
|
|
exit 1
|
|
fi
|
|
|
|
IMMICH_API_KEY=$(cat "$API_KEY_FILE")
|
|
|
|
# Ensure export directory exists
|
|
mkdir -p "$EXPORT_DIR"
|
|
|
|
log "Starting photo export from Photos Library to $EXPORT_DIR"
|
|
|
|
# Export photos using osxphotos
|
|
# --directory: export directory structure by date
|
|
# --update: only export new/changed photos (incremental)
|
|
# --exiftool: write metadata to EXIF tags
|
|
# --edited-suffix _edited: suffix for edited versions
|
|
# --download-missing: download from iCloud if needed
|
|
OSXPHOTOS_ARGS=(
|
|
export "$EXPORT_DIR"
|
|
--directory "{created.year}/{created.mm}-{created.mon}"
|
|
{% if immich_sync_update_mode %}
|
|
--update
|
|
{% endif %}
|
|
--exiftool
|
|
--download-missing
|
|
{% if immich_sync_export_edited %}
|
|
--export-edited
|
|
--edited-suffix "_edited"
|
|
{% endif %}
|
|
{% if immich_sync_export_originals %}
|
|
--export-originals
|
|
{% endif %}
|
|
)
|
|
|
|
log "Running: mise x -- osxphotos ${OSXPHOTOS_ARGS[*]}"
|
|
if ! "$MISE" x -- osxphotos "${OSXPHOTOS_ARGS[@]}"; then
|
|
error "osxphotos export failed"
|
|
exit 1
|
|
fi
|
|
|
|
log "Photo export complete"
|
|
log "Starting upload to Immich at $IMMICH_URL"
|
|
|
|
# Upload to Immich using immich-cli in Docker
|
|
# -r: recursive upload
|
|
# -a: create albums from folder names
|
|
# --concurrency: parallel uploads
|
|
IMMICH_CLI_ARGS=(
|
|
upload
|
|
--recursive
|
|
{% if immich_sync_create_albums %}
|
|
--album
|
|
{% endif %}
|
|
--concurrency {{ immich_sync_concurrency }}
|
|
/import
|
|
)
|
|
|
|
log "Running: docker run immich-cli ${IMMICH_CLI_ARGS[*]}"
|
|
if ! "$DOCKER" run --rm \
|
|
-v "$EXPORT_DIR:/import:ro" \
|
|
-e IMMICH_INSTANCE_URL="$IMMICH_URL" \
|
|
-e IMMICH_API_KEY="$IMMICH_API_KEY" \
|
|
ghcr.io/immich-app/immich-cli:latest \
|
|
"${IMMICH_CLI_ARGS[@]}"; then
|
|
error "immich-cli upload failed"
|
|
exit 1
|
|
fi
|
|
|
|
log "Upload to Immich complete"
|
|
log "Sync finished successfully"
|