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.0 KB
Raw Blame
// The offsite backup's bucket key can delete every backup, so it must never reach a container: it lives in
// backup.env, not in .env (which the API reads whole), and goes to rclone's container by name, not on the
// command line. scripts/backup.sh runs here with a fake `docker` that records what it was given.
import { spawnSync } from 'node:child_process'
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, expect, test } from 'vitest'

const root = new URL('../../../', import.meta.url).pathname
const SECRET = 'bucket-secret-0123456789abcdef'

let dir: string
beforeEach(() => {
  dir = mkdtempSync(join(tmpdir(), 'linedesk-backup-test-'))
  // The fake docker: its arguments in one file, the secret it got through the environment in another.
  writeFileSync(
    join(dir, 'docker'),
    `#!/usr/bin/env bash\nprintf '%s\\n' "$@" > "${dir}/args"\nprintf '%s' "\${RCLONE_CONFIG_B_SECRET_ACCESS_KEY:-}" > "${dir}/env-secret"\n`,
  )
  chmodSync(join(dir, 'docker'), 0o755)
})
afterEach(() => rmSync(dir, { recursive: true, force: true }))

function writeSettings(mode: number) {
  const file = join(dir, 'backup.env')
  writeFileSync(
    file,
    [
      'BACKUP_S3_ENDPOINT=https://example.r2.cloudflarestorage.com',
      'BACKUP_S3_BUCKET=backups',
      'BACKUP_S3_ACCESS_KEY_ID=key-id',
      `BACKUP_S3_SECRET_ACCESS_KEY=${SECRET}`,
    ].join('\n'),
  )
  chmodSync(file, mode)
  return file
}

/** backup.sh --list with only PATH, HOME and BACKUP_ENV_FILE set: nothing comes from the developer's environment. */
const listBackups = (settingsFile: string) =>
  spawnSync('bash', [join(root, 'scripts/backup.sh'), '--list'], {
    env: { PATH: `${dir}:${process.env.PATH}`, HOME: dir, BACKUP_ENV_FILE: settingsFile },
    encoding: 'utf8',
  })

test('the bucket key reaches rclone by name only, never on the docker command line', () => {
  const run = listBackups(writeSettings(0o600))
  expect(run.status, run.stderr).toBe(0)
  const args = readFileSync(join(dir, 'args'), 'utf8')
  expect(args).not.toContain(SECRET)
  expect(args.split('\n')).toContain('RCLONE_CONFIG_B_SECRET_ACCESS_KEY')
  expect(args).toContain('b:backups/linedesk')
  expect(readFileSync(join(dir, 'env-secret'), 'utf8')).toBe(SECRET)
})

test('a settings file that others can read is refused before anything runs', () => {
  const run = listBackups(writeSettings(0o644))
  expect(run.status).toBe(1)
  expect(run.stderr).toContain('chmod 600')
  expect(existsSync(join(dir, 'args'))).toBe(false)
})

test('a named settings file that is missing is refused', () => {
  const run = listBackups(join(dir, 'missing.env'))
  expect(run.status).toBe(1)
  expect(existsSync(join(dir, 'args'))).toBe(false)
})

test('the backup settings are not offered for .env, which the API container reads whole', () => {
  const compose = readFileSync(join(root, 'docker-compose.yml'), 'utf8')
  expect(compose).toMatch(/env_file: \[\{ path: \.env/)
  expect(compose).not.toContain('backup.env')
  expect(readFileSync(join(root, '.env.example'), 'utf8')).not.toMatch(/^#?BACKUP_S3_/m)
  expect(readFileSync(join(root, '.gitignore'), 'utf8')).toMatch(/^backup\.env$/m)
  expect(readFileSync(join(root, '.dockerignore'), 'utf8')).toMatch(/^backup\.env$/m)
})

// WAL archiving keeps base/ and wal/ next to the dumps, and rclone lists folders whatever --include says: the
// restore took 'wal/' for the newest backup (prod restore test, 2026-11-22). The fake rclone lists like the real
// one: the folders unless --files-only, and their contents unless --max-depth 1.
test('the restore picks the newest dump, not the base/ or wal/ folder next to it', () => {
  writeFileSync(
    join(dir, 'docker'),
    [
      '#!/usr/bin/env bash',
      `printf '%s\\n' "$*" >> "${dir}/calls"`,
      'case " $* " in *" lsf "*) ;; *) exit 0 ;; esac',
      'echo linedesk-20261120T090000Z.dump',
      'echo linedesk-20261127T215859Z.dump.age',
      'case " $* " in *" --files-only "*) ;; *) echo base/; echo wal/ ;; esac',
      'case " $* " in *" --max-depth 1 "*) ;; *) echo wal/000000010000000000000001.gz ;; esac',
      '',
    ].join('\n'),
  )
  const run = spawnSync('bash', [join(root, 'scripts/restore-backup.sh')], {
    env: { PATH: `${dir}:${process.env.PATH}`, HOME: dir, BACKUP_ENV_FILE: writeSettings(0o600) },
    encoding: 'utf8',
  })
  expect(run.stderr).not.toContain('no backup found')
  expect(run.stdout).toContain('downloading linedesk-20261127T215859Z.dump.age')
  // Encrypted and no private key here: it stops right after the download, before any Postgres.
  expect(run.stderr).toContain('BACKUP_AGE_IDENTITY')
})

test('the backup list and the cleanup of old backups stay out of base/ and wal/', () => {
  const run = listBackups(writeSettings(0o600))
  expect(run.status, run.stderr).toBe(0)
  expect(readFileSync(join(dir, 'args'), 'utf8').split('\n')).toContain('--max-depth')
  expect(readFileSync(join(root, 'scripts/backup.sh'), 'utf8')).toMatch(/rclone delete --max-depth 1 /)
})