Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

5.6 KB
Raw Blame
# shellcheck shell=bash
# Shared by scripts/wal-archive.sh, wal-base-backup.sh and wal-restore.sh (docker-compose.wal.yml): the backup
# bucket's settings (scripts/backup-settings.sh, backup.env), where WAL and base backups go in it, a lock so a restore
# and the timers never run at the same time, and a private work directory removed however the script ends.
# shellcheck source=scripts/backup-settings.sh
. scripts/backup-settings.sh

for name in BACKUP_S3_ENDPOINT BACKUP_S3_BUCKET BACKUP_S3_ACCESS_KEY_ID BACKUP_S3_SECRET_ACCESS_KEY; do
  [ -n "$(setting "$name")" ] || { echo "wal: $name is not set (backup.env, docs/self-hosting.md \"Backups\")" >&2; exit 1; }
done
# Encrypted backups (BACKUP_AGE_RECIPIENT, the age public key, as for the dumps): every WAL segment and base backup is
# encrypted on the host before it leaves (`*.gz.age`, `base-*.tar.gz.age`); only the private key (off the server)
# reads them. Without it, everything is uploaded as it is.
age_recipient=$(setting BACKUP_AGE_RECIPIENT)
[ -z "$age_recipient" ] || command -v age >/dev/null || { echo "wal: BACKUP_AGE_RECIPIENT is set, but age is not installed (apt install age)" >&2; exit 1; }
prefix=$(setting BACKUP_S3_PREFIX)
prefix=${prefix:-linedesk/}
# shellcheck disable=SC2034 # used by the scripts that source this
wal_target="b:$(setting BACKUP_S3_BUCKET)/${prefix%/}/wal"
# shellcheck disable=SC2034
base_target="b:$(setting BACKUP_S3_BUCKET)/${prefix%/}/base"
wal_keep_days=$(setting BACKUP_WAL_KEEP_DAYS)
wal_keep_days=${wal_keep_days:-7}
[[ "$wal_keep_days" =~ ^[0-9]+$ ]] && [ "$wal_keep_days" -gt 0 ] || { echo "wal: BACKUP_WAL_KEEP_DAYS must be a positive number" >&2; exit 1; }
pg_user=$(sed -n 's/^POSTGRES_USER=//p' .env 2>/dev/null | tail -n 1 | sed "s/^[\"']//; s/[\"']\$//")
pg_user=${pg_user:-linedesk}

# One at a time: a restore holds the lock for its whole run; the timers skip a turn while it is held.
exec 9> "/var/tmp/linedesk-wal-$(id -u).lock"

find /var/tmp -maxdepth 1 -type d -name 'linedesk-wal.*' -mmin +1440 -exec rm -rf {} + 2>/dev/null || true
work=$(mktemp -d /var/tmp/linedesk-wal.XXXXXX)
chmod 700 "$work"
trap 'rm -rf "$work"' EXIT
trap 'exit 130' INT
trap 'exit 143' TERM

# The postgres container, running or stopped (empty when there is none).
postgres_container() { docker compose ps -aq postgres; }
# The Docker volume mounted at <path> in the postgres container (empty when there is none).
postgres_volume() {
  local id
  id=$(postgres_container)
  [ -n "$id" ] || return 0
  docker inspect -f "{{range .Mounts}}{{if eq .Destination \"$1\"}}{{.Name}}{{end}}{{end}}" "$id"
}
postgres_running() { [ -n "$(docker compose ps -q --status running postgres)" ]; }
psql_stack() { docker compose exec -T postgres psql -U "$pg_user" -d postgres -tAc "$1"; }

# The bucket holds the WAL of exactly one database cluster: a new database (initdb, another install with the same
# prefix) would mix its WAL in. Its system identifier is noted next to the WAL; a different one stops everything.
check_system_id() {
  local id noted
  id=$(psql_stack 'select system_identifier from pg_control_system()')
  noted=$(rclone cat "$wal_target/SYSTEM_IDENTIFIER" 2>/dev/null || true)
  if [ -z "$noted" ]; then
    printf '%s\n' "$id" > "$work/SYSTEM_IDENTIFIER"
    rclone copyto /work/SYSTEM_IDENTIFIER "$wal_target/SYSTEM_IDENTIFIER"
  elif [ "$noted" != "$id" ]; then
    echo "wal: $wal_target holds the WAL of database $noted, this is $id: use another BACKUP_S3_PREFIX" >&2
    exit 1
  fi
}

# Encrypted WAL waits here between "out of the volume" and "in the bucket", so a failed upload loses nothing (the next
# run tries again). It holds only encrypted files. The timer's state directory; a run by hand uses the same one.
wal_spool=${STATE_DIRECTORY:-/var/lib/linedesk-wal}/spool
mkdir -p "$wal_spool" 2>/dev/null || { wal_spool=/var/tmp/linedesk-wal-spool-$(id -u); mkdir -p "$wal_spool"; }
chmod 700 "$wal_spool"

# A shell command in a throwaway container (the rclone image, already on the host) with a volume mounted (`name:/path`).
in_volume() { docker run --rm --network none -v "$1" --entrypoint sh rclone/rclone:1.68.2 -c "$2"; }

# Every finished segment in the volume `walarchive` to the bucket, then gone from the volume. Plain: one rclone move
# (deleted once uploaded and its checksum matches). Encrypted: each segment is read out of the volume, encrypted into
# the spool on the host, and only then removed from the volume; then the spool is moved up the same way.
# $1: the volume. Returns rclone's exit code.
upload_archived_wal() {
  local volume=$1 rc=0 f
  if [ -z "$age_recipient" ]; then
    rclone_volume="$volume:/wal"
    rclone move /wal "$wal_target" --include '*.gz' --checksum || rc=$?
    rclone_volume=
    return "$rc"
  fi
  # Called as `upload_archived_wal ... || rc=$?`, so `set -e` does not apply in here: every step checks itself, and a
  # segment leaves the volume only once its encrypted copy is complete in the spool.
  local names
  # shellcheck disable=SC2016 # expanded by the container's shell
  names=$(in_volume "$volume:/wal:ro" 'cd /wal && for f in *.gz; do [ -f "$f" ] && echo "$f"; done; true') || return 1
  for f in $names; do
    [[ "$f" =~ ^[0-9A-Za-z._-]+\.gz$ ]] || { echo "wal: unexpected file in the archive volume: $f" >&2; return 1; }
    in_volume "$volume:/wal:ro" "cat /wal/$f" | age -r "$age_recipient" -o "$wal_spool/$f.age.tmp" || return 1
    mv "$wal_spool/$f.age.tmp" "$wal_spool/$f.age" || return 1
    in_volume "$volume:/wal" "rm -f /wal/$f" || return 1
  done
  rm -f "$wal_spool"/*.tmp
  rclone_volume="$wal_spool:/spool"
  rclone move /spool "$wal_target" --include '*.gz.age' --checksum || rc=$?
  rclone_volume=
  return "$rc"
}