|
1 | 1 | #!/usr/bin/env bash |
2 | | -# Dump the PostgreSQL database (custom format, compressed) and archive the |
3 | | -# uploaded files (members' CVs, Docker volume "uploads" mounted on |
4 | | -# /app/storage), and keep the last BACKUP_KEEP_DAYS days of both. Run from |
5 | | -# the repository root's deploy/ dir by the jscm-backup systemd timer (see |
6 | | -# deploy/README section in README.md). |
| 2 | +# Nightly backups of every environment present on the server (staging in |
| 3 | +# ~/apps/javascript.cm, production in ~/apps/javascript.cm-prod): |
| 4 | +# 1. PostgreSQL dump (custom format, compressed) |
| 5 | +# 2. uploaded files (CVs, images: Docker volume mounted on /app/storage) |
| 6 | +# kept locally for BACKUP_KEEP_DAYS days, then — when RESTIC_REPOSITORY is |
| 7 | +# configured (systemd EnvironmentFile /etc/jscm/backup.env) — pushed off-site |
| 8 | +# with restic: encrypted client-side, deduplicated, retention 7 daily / |
| 9 | +# 8 weekly / 12 monthly. |
7 | 10 | # |
8 | | -# Restore: |
| 11 | +# Restore (from the environment's directory): |
9 | 12 | # docker compose exec -T db pg_restore -U <user> -d <db> --clean --if-exists < file.dump |
10 | 13 | # docker compose exec -T app tar -C /app/storage -xzf - < file-uploads.tgz |
| 14 | +# restic restore latest --tag <project> --target /tmp/restore (off-site copy) |
11 | 15 | set -euo pipefail |
12 | 16 |
|
13 | | -main() { |
14 | | - cd "$(dirname "$0")/.." |
15 | | - # Dumps and archives contain personal data: owner-only from the start. |
16 | | - umask 077 |
17 | | - |
18 | | - local dir="${BACKUP_DIR:-$HOME/backups/jscm}" |
19 | | - local keep="${BACKUP_KEEP_DAYS:-14}" |
20 | | - local db_user db_name |
| 17 | +backup_environment() { |
| 18 | + local project_dir="$1" dir="$2" |
| 19 | + cd "$project_dir" |
| 20 | + local name db_user db_name |
| 21 | + name="$(grep -E '^COMPOSE_PROJECT_NAME=' .env | cut -d= -f2- || true)" |
| 22 | + name="${name:-jscm}" |
21 | 23 | db_user="$(grep -E '^DB_USER=' .env | cut -d= -f2-)" |
22 | 24 | db_name="$(grep -E '^DB_DATABASE=' .env | cut -d= -f2-)" |
23 | 25 |
|
24 | | - install -d -m 700 "$dir" |
25 | | - local file="$dir/jscm-$(date -u +%Y%m%dT%H%M%SZ).dump" |
| 26 | + local stamp file uploads |
| 27 | + stamp="$(date -u +%Y%m%dT%H%M%SZ)" |
| 28 | + file="$dir/$name-$stamp.dump" |
| 29 | + uploads="$dir/$name-$stamp-uploads.tgz" |
26 | 30 |
|
27 | 31 | docker compose exec -T db pg_dump -U "$db_user" -d "$db_name" --format=custom --no-owner > "$file.tmp" |
28 | 32 | mv "$file.tmp" "$file" |
29 | | - chmod 600 "$file" |
30 | 33 | echo "backup: $file ($(du -h "$file" | cut -f1))" |
31 | 34 |
|
32 | | - # Uploaded files (CVs contain personal data: same 600 permissions). |
33 | | - local uploads="${file%.dump}-uploads.tgz" |
34 | 35 | docker compose exec -T app tar -C /app/storage -czf - . > "$uploads.tmp" |
35 | 36 | mv "$uploads.tmp" "$uploads" |
36 | | - chmod 600 "$uploads" |
37 | 37 | echo "backup: $uploads ($(du -h "$uploads" | cut -f1))" |
| 38 | +} |
| 39 | + |
| 40 | +main() { |
| 41 | + # Dumps and archives contain personal data: owner-only from the start. |
| 42 | + umask 077 |
| 43 | + local dir="${BACKUP_DIR:-$HOME/backups/jscm}" |
| 44 | + local keep="${BACKUP_KEEP_DAYS:-14}" |
| 45 | + install -d -m 700 "$dir" |
| 46 | + |
| 47 | + local project |
| 48 | + for project in "$HOME/apps/javascript.cm" "$HOME/apps/javascript.cm-prod"; do |
| 49 | + if [[ -f "$project/.env" ]] && (cd "$project" && docker compose ps --status running -q db | grep -q .); then |
| 50 | + backup_environment "$project" "$dir" |
| 51 | + fi |
| 52 | + done |
| 53 | + |
| 54 | + find "$dir" -type f \( -name '*.dump' -o -name '*-uploads.tgz' \) -mtime +"$keep" -delete |
38 | 55 |
|
39 | | - find "$dir" -type f \( -name 'jscm-*.dump' -o -name 'jscm-*-uploads.tgz' \) -mtime +"$keep" -delete |
| 56 | + if [[ -n "${RESTIC_REPOSITORY:-}" ]]; then |
| 57 | + restic snapshots --latest 1 > /dev/null 2>&1 || restic init |
| 58 | + restic backup --quiet --tag jscm --host jscm "$dir" |
| 59 | + restic forget --quiet --tag jscm --keep-daily 7 --keep-weekly 8 --keep-monthly 12 --prune |
| 60 | + echo "off-site: snapshot pushed to $RESTIC_REPOSITORY" |
| 61 | + else |
| 62 | + echo "off-site: skipped (RESTIC_REPOSITORY not configured in /etc/jscm/backup.env)" |
| 63 | + fi |
40 | 64 | } |
41 | 65 |
|
42 | 66 | main "$@" |
0 commit comments