diff --git a/README.md b/README.md index a6bdaf9..4e0bb85 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,15 @@ journalctl -f The containers should start on the next boot automatically. +# Database backup + +The `database_backup` folder suggests a way to dump the database regularly. Make sure to add a volume mount to the +database container and bind it to `/var/db_backup` + +As is, the unit will create gziped SQL dumps named with the date of creation: `YYYYMMDD`. + +To enable it, place the files in `/etc/systemd/system` then enable the timer: `systemctl enable --now immich-database-backup.timer`. + # TODO - write a makefile or a justfile that insert the variables in the unit files maybe ? Right now it requires some copy and pasting. - Contribute it upstream to immich : no longer a goal, they stated they are [not interested](https://github.com/immich-app/immich/discussions/7977). diff --git a/database_backup/immich-database-backup.service b/database_backup/immich-database-backup.service new file mode 100644 index 0000000..85ac6a4 --- /dev/null +++ b/database_backup/immich-database-backup.service @@ -0,0 +1,18 @@ +[Unit] +Description=Dumps immich postgre database for backup +Documentation=https://immich.app/docs/administration/backup-and-restore/ +After=immich-database +Requires=immich-database + +[Service] +Type=oneshot +# Dump the database as SQL +ExecStart=podman exec -t immich_postgres pg_dumpall --clean --if-exists --username=postgres -f /var/db_backup/backup +# Rename the file (double % to escape systemd specifiers) +ExecStart=bash -c "mv /path/to/database_backup/backup /var/hdd-raid/immich/database_backup/$(date +%%Y%%m%%d)" +# Compress the file (double % to escape systemd specifiers) +ExecStart=bash -c "gzip /var/hdd-raid/immich/database_backup/$(date +%%Y%%m%%d)" +RemainAfterExit=true + +[Install] +WantedBy=multi-user.target diff --git a/database_backup/immich-database-backup.timer b/database_backup/immich-database-backup.timer new file mode 100644 index 0000000..fd1c778 --- /dev/null +++ b/database_backup/immich-database-backup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Trigger immich-database-backup service + +[Timer] +# Twice a week : mondays, thursdays, mornings +OnCalendar=Mon,Thu 05:00 Europe/Paris + +[Install] +WantedBy=timers.target diff --git a/immich-database.container b/immich-database.container index ccabc39..04a9d10 100644 --- a/immich-database.container +++ b/immich-database.container @@ -5,6 +5,8 @@ EnvironmentFile=immich.env Image=docker.io/tensorchord/pgvecto-rs:pg14-v0.2.0@sha256:90724186f0a3517cf6914295b5ab410db9ce23190a2d9d0b9dd6463e3fa298f0 Volume=/path/to/database/database:/var/lib/postgresql/data:z Volume=./immich-database-healthcheck:/health.sh +# If you want to use the unit provided in `database_backup` +# Volume=/path/to/db/backups:/var/db_backup/:z Exec= postgres -c shared_preload_libraries=vectors.so -c 'search_path="$$user", public, vectors' -c logging_collector=on -c max_wal_size=2GB -c shared_buffers=512MB -c wal_compression=on HealthCmd=["/bin/bash", "/health.sh"] HealthStartPeriod=5m