#!/bin/bash # {{ ansible_managed }} set -euo pipefail BACKUP_DIR="{{ bookstack_backup_dir }}" RETENTION_DAYS={{ bookstack_backup_retention_days }} APPDATA_DIR="{{ bookstack_appdata_dir }}" STAMP="$(date +%Y%m%d-%H%M%S)" mkdir -p "$BACKUP_DIR" # --- DB dump (mariadb-dump from inside the DB container) --- # Use the app user via TCP because root@localhost is unix_socket-auth only # in the LSIO MariaDB image and root@% does not exist. docker exec {{ bookstack_service_name }}-db \ mariadb-dump \ --protocol=tcp -h 127.0.0.1 \ -u "{{ bookstack_db_user }}" -p"{{ bookstack_db_password }}" \ --single-transaction --routines --triggers --quick \ "{{ bookstack_db_name }}" \ | gzip -9 > "$BACKUP_DIR/bookstack-db-$STAMP.sql.gz" # --- File uploads (images, attachments) --- # LSIO BookStack stores user uploads under /config/www/{uploads,storage/uploads,files}. tar --warning=no-file-changed \ -czf "$BACKUP_DIR/bookstack-files-$STAMP.tar.gz" \ -C "$APPDATA_DIR/www" \ uploads storage/uploads files 2>/dev/null || true # --- APP_KEY backup (critical for restore!) --- install -m 0600 "{{ bookstack_docker_volume_dir }}/.app_key" \ "$BACKUP_DIR/bookstack-appkey-$STAMP.txt" # --- Retention --- find "$BACKUP_DIR" -type f \ \( -name 'bookstack-db-*.sql.gz' \ -o -name 'bookstack-files-*.tar.gz' \ -o -name 'bookstack-appkey-*.txt' \) \ -mtime +"$RETENTION_DAYS" -delete echo "Backup complete: $STAMP"