Add stage parameter for backup hooks and add immich_upload backup

This commit is contained in:
Tobias Reisinger 2025-11-15 21:46:32 +01:00
parent 4db5c1878b
commit 759368b6d6
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
8 changed files with 49 additions and 11 deletions

View file

@ -1,5 +1,11 @@
import copy import copy
def hooks_add_or_create(location, key, value):
if key in location["hooks"]:
location["hooks"][key].append(value)
else:
location["hooks"][key] = [value]
class FilterModule(object): class FilterModule(object):
def filters(self): def filters(self):
return { return {
@ -22,16 +28,17 @@ class FilterModule(object):
if location["type"] == "docker": if location["type"] == "docker":
new_location["from"] = name new_location["from"] = name
new_location["type"] = "volume" new_location["type"] = "volume"
if location["type"] == "docker_remote":
new_location["from"] = name
new_location["type"] = "volume"
if location["type"] == "hook": if location["type"] == "hook":
backup_dir = f"/opt/services/_backup/{name}" backup_dir = f"/opt/services/_backup/{name}"
new_location["from"] = backup_dir new_location["from"] = backup_dir
if not "before" in new_location["hooks"]:
new_location["hooks"]["before"] = [] for hook_type in ["prevalidate", "before", "after", "failure", "success"]:
new_location["hooks"]["before"].append(f"/opt/services/backup/hooks/{name} '{backup_dir}'") hooks_add_or_create(
new_location,
hook_type,
f"/opt/services/backup/hooks/{name} '{backup_dir}' {hook_type}"
)
if location["type"] == "directory": if location["type"] == "directory":
new_location["from"] = location["path"] new_location["from"] = location["path"]

View file

@ -2,5 +2,9 @@
backup_path="$1" backup_path="$1"
if [ "$2" != "before" ]; then
exit 0
fi
cd /opt/services/immich || exit cd /opt/services/immich || exit
docker compose exec database sh -c 'pg_dump -U "$DB_USERNAME" "$DB_DATABASE"' | gzip >"$backup_path/immich.sql.gz" docker compose exec database sh -c 'pg_dump -U "$DB_USERNAME" "$DB_DATABASE"' | gzip >"$backup_path/immich.sql.gz"

View file

@ -0,0 +1,12 @@
#!/usr/bin/env bash
backup_path="$1"
stage="$2"
if [ "$stage" == "before" ]; then
rclone mount --config /opt/services/backup/rclone.conf --daemon immich_upload: "$backup_path"
fi
if [ "$stage" == "after" ]; then
unmount "$backup_path"
fi

View file

@ -2,4 +2,8 @@
export MAILCOW_BACKUP_LOCATION="$1" export MAILCOW_BACKUP_LOCATION="$1"
if [ "$2" != "before" ]; then
exit 0
fi
/opt/mailcow-dockerized/helper-scripts/backup_and_restore.sh backup all --delete-days 1 /opt/mailcow-dockerized/helper-scripts/backup_and_restore.sh backup all --delete-days 1

View file

@ -2,6 +2,10 @@
cd "$1" cd "$1"
if [ "$2" != "before" ]; then
exit 0
fi
postgres_tables=$(sudo -u postgres psql -Atc "SELECT datname FROM pg_database WHERE datistemplate = false;") postgres_tables=$(sudo -u postgres psql -Atc "SELECT datname FROM pg_database WHERE datistemplate = false;")
for i in $postgres_tables for i in $postgres_tables

View file

@ -25,6 +25,12 @@
content: '{{ backup_yml_all | to_nice_yaml }}' content: '{{ backup_yml_all | to_nice_yaml }}'
mode: "0644" mode: "0644"
- name: Create rclone.conf
ansible.builtin.copy:
dest: "{{ (service_path, 'rclone.conf') | path_join }}"
content: '{{ vault_backup.rclone }}'
mode: "0600"
- name: Import tasks specific to the hooks scripts - name: Import tasks specific to the hooks scripts
ansible.builtin.import_tasks: hooks.yml ansible.builtin.import_tasks: hooks.yml
- name: Import tasks specific to the recovery scripts - name: Import tasks specific to the recovery scripts

View file

@ -5,11 +5,12 @@
update_cache: true update_cache: true
become: true become: true
- name: Install jq and bzip2 - name: Install wanted software
ansible.builtin.apt: ansible.builtin.apt:
pkg: pkg:
- jq - jq
- bzip2 - bzip2
- rclone
state: present state: present
update_cache: true update_cache: true
become: true become: true

View file

@ -282,10 +282,10 @@ services = {
domain = "gallery.serguzim.me" domain = "gallery.serguzim.me"
}] }]
backup = [ backup = [
#{ {
# name = "immich_upload" name = "immich_upload"
# type = "docker_remote" type = "hook"
#}, },
{ {
name = "immich_database" name = "immich_database"
type = "hook" type = "hook"