Skip to content
Sign inDashboard

Backups and restore

The backup role (opdns-cp backup run) takes the backups on a schedule. It also applies retention and runs restore tests. Its other subcommands do these tasks one time, from a shell:

opdns-cp backup run schedule backups, retention and restore tests (long-running role)
opdns-cp backup now take a backup now (--store postgres|clickhouse|all)
opdns-cp backup verify restore a backup into a scratch database and check it (--store, --id latest|random|<id>, --keep)
opdns-cp backup restore restore into an empty target (--store postgres --target-database-url URL,
or --store clickhouse --target-database NAME) (--id latest|<id>)
opdns-cp backup list list complete backups (--store)
opdns-cp backup prune apply retention now (--store)
Store How Where (backup bucket)
Postgres (every table of the control plane database) one read-consistent snapshot: a COPY of every table in foreign-key order into a gzip tar, encrypted with AES-256-GCM. A plaintext manifest next to it holds the schema version, the row counts and a digest for each table postgres/<id>/
ClickHouse (query logs and aggregates) the native BACKUP DATABASE ... TO S3(...) of the server, with the row counts before and after in opdns-manifest.json clickhouse/<id>/

The backup bucket (--backup-bucket, default opdns-backups) is separate from the opdns bucket.

opdns encrypts Postgres archives with OPDNS_BACKUP_KEY (or OPDNS_BACKUP_KEY_FILE). The value is a comma-separated list of <id>:<base64 of 32 bytes> entries. The first entry encrypts, and all entries decrypt. To rotate a key, put the new key first. Keep the old key until its archives expire. Generate a key with openssl rand -base64 32.

The key is required outside env=dev. opdns does not encrypt ClickHouse backups. They rely on the encryption of the bucket.

Postgres ClickHouse
Interval --postgres-backup-interval, 1 h --clickhouse-backup-interval, 24 h
Keep every backup for --postgres-keep-all, 48 h --clickhouse-keep-all, 14 days
Keep the first of each day for --postgres-keep-daily, 30 days --clickhouse-keep-daily, off
Keep the first of each month for --postgres-keep-monthly, off --clickhouse-keep-monthly, 120 days
Always keep the newest --postgres-keep-min, 3 --clickhouse-keep-min, 3

If a run fails, the role tries again after --backup-retry-interval (5 minutes). The Postgres interval is the recovery point. There is no point-in-time recovery: a restore goes back to the last archive. WAL archiving is planned.

Every --verify-interval (30 days), the role picks a retained backup (--verify-pick random, or latest). It restores the backup into a scratch database (a temporary database for the test). It compares the row counts, the content digests and the schema version with the manifest. Then it drops the scratch database and writes the result to verify/<store>.json.

The time that the restore took (restore_seconds) is the estimate of the recovery time. opdns-cp backup verify runs one test now. --keep keeps the scratch database so that you can examine it.

The role creates scratch Postgres databases on --scratch-database-url. The default is the main database server, and the user needs CREATEDB. A separate scratch server in production is planned.

Restore after a real loss of the control plane database or of the log store, or to rehearse. If time allows, rehearse first: opdns-cp backup verify --store postgres --id <id>.

  1. Pick the backup: opdns-cp backup list --store postgres.

  2. Create an empty database. Then restore into it and migrate:

    Terminal window
    opdns-cp backup restore --store postgres --id <id|latest> \
    --target-database-url postgres://.../opdns_restored
    opdns-cp migrate # with OPDNS_DATABASE_URL pointing at the target

    The restore builds the schema again from the migrations in the binary, up to the version of the backup. Use an opdns-cp of the same release or of a newer one. The restore loads every table in one transaction. It makes sure that the counts and the digests match, and it restores the sequences. It refuses a target that is not empty.

  3. Point OPDNS_DATABASE_URL of every role at the restored database. Then restart the roles.

  4. Bring the profile stream and the snapshot in line with the restored state:

    Terminal window
    opdns-cp admin profiles reconcile --fix --yes

    See Profile propagation. The command lists any drift that it cannot repair, such as ahead. Examine that drift yourself.

Terminal window
opdns-cp backup restore --store clickhouse --id <id> --target-database opdns_restored

The target database must not exist. After the restore, rename it, or point OPDNS_CLICKHOUSE_URL of the ingest and api roles at it. Records ingested after the backup are lost, unless the logs stream still holds them. The stream keeps 24 hours. Ingest continues from its consumer position. Restart ingest after the change, and watch the aggregates.

Record the backup id, the recovery time, the rows restored and the data that was lost. If the loss affected user data, put this record in the transparency report.

Alert Severity Fires when
PostgresBackupStale page no successful Postgres backup for 2 hours
ClickHouseBackupStale warn no successful ClickHouse backup for 26 hours
RestoreTestFailing warn a scheduled restore test failed in the last day
RestoreTestStale warn no successful restore test for 32 days