#!/bin/sh # Nightly logical backup of the lacuna database. # # Runs *inside* the mysql container, invoked on a schedule by Ofelia # (see the [job-exec "mysql-dump"] block in schedule.ini). Output and retention # live on the host-mounted ./data/backups tree. # # /bin/sh in the mysql:5.5 image is dash, which has no `set -o pipefail`, so the # dump is written to disk first and only compressed/published once mysqldump has # exited 0 -- a failed run never leaves a half-written .sql.gz under the real name. set -eu BACKUP_DIR=/backups KEEP_DAYS=7 STAMP=$(date +%Y%m%d-%H%M%S) RAW="$BACKUP_DIR/.lacuna-$STAMP.sql" DEST="$BACKUP_DIR/lacuna-$STAMP.sql.gz" mkdir -p "$BACKUP_DIR" mysqldump -u root -p"$MYSQL_ROOT_PASSWORD" \ --single-transaction --quick \ --routines --triggers --events \ --add-drop-database --databases lacuna > "$RAW" gzip "$RAW" # -> $RAW.gz (still hidden) mv "$RAW.gz" "$DEST" # publish atomically under the real name # Retention: drop published backups older than KEEP_DAYS, plus any stale work # files left behind by an interrupted run. find "$BACKUP_DIR" -maxdepth 1 -name 'lacuna-*.sql.gz' -type f -mtime "+$KEEP_DAYS" -delete find "$BACKUP_DIR" -maxdepth 1 -name '.lacuna-*.sql*' -type f -mtime +1 -delete echo "mysql-dump: wrote $DEST ($(du -h "$DEST" | cut -f1))"