# Backup + WAL archiving bundle

This bundle sets up:
- pgBackRest backup repository host
- pgBackRest client config on DB nodes
- WAL archiving settings for Patroni/PostgreSQL
- cron-based full + incremental backups
- a simple restore example script

## Files

- `scripts/setup-backup.sh`
- `scripts/restore-example.sh`
- `examples/repo.env`
- `examples/db.env`
- `examples/restore.env`

## Recommended layout

- `backup1` = dedicated backup repository host
- `db1`, `db2`, `db3` = Patroni/PostgreSQL nodes

## Order

### 1. On backup1
Edit `examples/repo.env`, then run:

```bash
sudo bash scripts/setup-backup.sh examples/repo.env
```

### 2. On each DB node
Edit `examples/db.env` for that node, then run:

```bash
sudo bash scripts/setup-backup.sh examples/db.env
```

Then on each DB node:
```bash
sudo -u postgres ssh-keygen -t ed25519 -N '' -f /var/lib/postgresql/.ssh/id_ed25519
sudo -u postgres ssh-copy-id postgres@backup1
sudo /usr/local/bin/apply-patroni-backup-settings.sh
cd /opt/db-stack && docker compose restart patroni
```

Restart one DB node at a time.

### 3. Back on backup1
Create the stanza and first backup:

```bash
sudo -u postgres pgbackrest --stanza=main stanza-create
sudo -u postgres pgbackrest --stanza=main backup --type=full
```

## Backup schedule

- full backup every day at 02:15
- incremental backup every 6 hours
- check every day at 01:40

## Notes

- This bundle assumes PostgreSQL data dir is `/var/lib/postgresql/data`
- It patches the Patroni YAML to add:
  - `archive_mode = on`
  - `archive_command = pgbackrest --stanza=... archive-push %p`
  - `restore_command = pgbackrest --stanza=... archive-get %f %p`
- Test restore regularly.
- This is a practical starter setup, not a full DR automation framework.
