#!/usr/bin/env bash # Back up Ghost: the MySQL database, the content directory (images, themes, # settings, adapters) and the settings in .env, ghost.env, compose.yaml and caddy/, # into ~/ghost/backups/. Keeps the 7 newest. From https://exe.dev/deploy/ghost set -euo pipefail umask 077 cd "$(dirname "$(readlink -f "$0")")" keep=7 mkdir -p backups out="backups/ghost-$(date -u +%Y%m%d-%H%M%S).tar.gz" work=$(mktemp -d) trap 'rm -rf "$work"' EXIT docker compose exec -T db sh -c 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mysqldump -uroot \ --single-transaction --routines --triggers --hex-blob --no-tablespaces \ --default-character-set=utf8mb4 ghost' > "$work/ghost.sql" tail -n 1 "$work/ghost.sql" | grep -q '^-- Dump completed' || { echo "database dump incomplete" >&2; exit 1; } tar -czf "$out.part" --exclude=content/logs \ -C "$work" ghost.sql -C "$PWD" .env ghost.env compose.yaml caddy content mv "$out.part" "$out" ls -1 backups/ghost-*.tar.gz | head -n -"$keep" | xargs -r rm -- echo "$out"