---
title: "Backup and Restore"
description: "Implement backup and restore strategies using Xec"
---

# Backup and Restore

## Problem

Implementing reliable backup and restore procedures for databases, files, configurations, and application state across different environments while ensuring data integrity and minimal downtime.

## Solution

Xec orchestrates the database tools and application acceptance checks. This recipe creates a PostgreSQL archive, records its digest after successful completion, and verifies it before restoration to a separate empty database. It does not claim automatic cross-database consistency or provide a retention scheduler.

## Quick Example

```typescript
const source = process.env.PGSERVICE;
if (!source) throw new Error('Select a PostgreSQL connection service with PGSERVICE');
const file = `backup-${new Date().toISOString().replace(/[:.]/g, '-')}.dump`;
await $`pg_dump --dbname ${`service=${source}`} --format=custom --file ${file}`;
await $`pg_restore --list ${file}`;
// Listing confirms archive readability, not application-level restore acceptance.
```

## Complete Backup and Restore Recipes

### Configuration

```yaml
version: '1.0'
tasks:
  backup:
    description: Create and verify a PostgreSQL archive
    command: xec run scripts/backup.ts
  restore:
    description: Restore a verified archive into a separate empty test database
    params:
      - name: directory
        required: true
    command: xec run scripts/restore.ts ${params.directory}
```

### Comprehensive Backup Script

```typescript
// scripts/backup.ts — pg_service.conf and credentials are provisioned separately.
import { mkdir, mkdtemp, writeFile, rename } from 'node:fs/promises';
import { createReadStream } from 'node:fs';
import { createHash } from 'node:crypto';
import path from 'node:path';

const source = process.env.PGSERVICE;
if (!source) throw new Error('PGSERVICE is required');
const root = path.resolve('backups');
await mkdir(root, { recursive: true, mode: 0o700 });
const directory = await mkdtemp(path.join(root, 'postgres-'));
const pending = path.join(directory, 'postgres.dump.pending');
await $`pg_dump --dbname ${`service=${source}`} --format=custom --file ${pending}`.timeout(3_600_000);
await $`pg_restore --list ${pending}`.quiet();
const hash = createHash('sha256');
for await (const chunk of createReadStream(pending)) hash.update(chunk);
await rename(pending, path.join(directory, 'postgres.dump'));
const manifest = {
  protocol: 1,
  source,
  file: 'postgres.dump',
  sha256: hash.digest('hex'),
  createdAt: new Date().toISOString()
};
await writeFile(path.join(directory, 'manifest.json'), JSON.stringify(manifest, null, 2), { mode: 0o600, flag: 'wx' });
console.log(`Verified archive: ${directory}`);
// An interrupted backup has no manifest. Retention/deletion is a separate policy.
const destination = process.env.BACKUP_S3_URI;
if (destination) await $`aws s3 cp ${directory} ${destination} --recursive`;
```

### Restore Script

```typescript
// scripts/restore.ts — the operator provisions a separate empty destination.
import { readFile } from 'node:fs/promises';
import { createReadStream } from 'node:fs';
import { createHash } from 'node:crypto';
import path from 'node:path';

const directory = args[0];
const target = process.env.RESTORE_SERVICE;
if (!directory || !target) throw new Error('Pass a backup directory and RESTORE_SERVICE');
const manifest: unknown = JSON.parse(await readFile(path.join(directory, 'manifest.json'), 'utf8'));
if (!manifest || typeof manifest !== 'object' || !('protocol' in manifest) || manifest.protocol !== 1 ||
    !('file' in manifest) || manifest.file !== 'postgres.dump' ||
    !('source' in manifest) || typeof manifest.source !== 'string' ||
    !('sha256' in manifest) || typeof manifest.sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(manifest.sha256)) {
  throw new Error('Invalid backup manifest');
}
if (target === manifest.source) throw new Error('Restore acceptance must use a separate destination');
const file = path.join(directory, manifest.file);
const hash = createHash('sha256');
for await (const chunk of createReadStream(file)) hash.update(chunk);
if (hash.digest('hex') !== manifest.sha256) throw new Error('Archive checksum mismatch');
// Verify before touching the destination. No DROP, --clean or blanket extraction.
await $`pg_restore --list ${file}`.quiet();
await $`pg_restore --exit-on-error --single-transaction --no-owner --dbname ${`service=${target}`} ${file}`.timeout(3_600_000);
// This application-owned script verifies rows, invariants and compatibility.
await $`npm run verify:restored-db`.env({ PGSERVICE: target });
console.log('Restore and application verification completed');
```

## Usage Examples

```bash
PGSERVICE=source-readonly xec run scripts/backup.ts
RESTORE_SERVICE=isolated-restore xec run scripts/restore.ts backups/postgres-example
```

Configure the PostgreSQL service names and the application's `verify:restored-db` command first. Schedule the backup command through an existing service scheduler. Store an independently authenticated manifest or trusted digest when detecting malicious archive replacement matters; a colocated checksum alone is not a signature.

## PostgreSQL service preset

`PostgreSQLFluentAPI.backup(path)` writes a plain SQL dump inside its container.
`restore(path)` reads that container path into the configured, existing database
using `psql -X -v ON_ERROR_STOP=1 --single-transaction -f`. It ignores `.psqlrc`,
rejects SQL errors and disables inherited automatic retries and `nothrow`.
Ordinary dumps produced by `backup()` commit together or roll back on SQL errors.

Restore only trusted dumps into a destination you have prepared for restoration;
the preset does not stop application writers or verify destination ownership.
Custom-format archives require `pg_restore`, as in the recipe above. Scripts with
`CREATE DATABASE` or `VACUUM` are incompatible with this transaction. Scripts that
issue their own `BEGIN`/`COMMIT`, switch connections, or change `ON_ERROR_STOP` are
unsupported and can defeat its atomicity or error detection. There is no fallback
to a nontransactional restore. See the [psql transaction contract](https://www.postgresql.org/docs/current/app-psql.html).

A lost connection or reply does not prove that the database rolled back: the
commit may already have completed. Inspect the destination before repeating a
restore. This method has no durable restore journal or automatic resume.

## Best Practices

1. **Test restore procedures** regularly
2. **Use 3-2-1 backup rule** (3 copies, 2 different media, 1 offsite)
3. **Encrypt sensitive backups** before storage
4. **Monitor backup success/failure**
5. **Document restore procedures**
6. **Implement backup rotation** policies
7. **Verify backup integrity** with checksums

## Related Topics

- [Health Checks](/docs/recipes/maintenance/health-checks)
- [Certificate Renewal](/docs/recipes/maintenance/certificate-renewal)
- [Log Aggregation](/docs/recipes/maintenance/log-aggregation)
- [Health Checks](/docs/recipes/maintenance/health-checks)