Skip to content

Commit

Permalink
Add database backups
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtrystram committed Jul 15, 2024
1 parent 2fae24b commit e7f1ef6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
18 changes: 18 additions & 0 deletions database_backup/immich-database-backup.service
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions database_backup/immich-database-backup.timer
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions immich-database.container
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e7f1ef6

Please sign in to comment.