Backup and Restore Internals

This chapter describes how backups and restores work in the code: what is copied, in what order, under which locks, how the backup catalog and point-in-time restore (PITR) work, and which commands and HTTP endpoints call them. Operating procedures are in Operations, section 4. The WAL side of backups (the backup hold that keeps WAL segments on disk) is in Write-ahead log; the manifest files that a backup copies are in Manifest.

A backup is a directory of plain file copies: MANIFEST, MANIFEST.log, the encryption key-check file if present, every live SSTable, optionally the WAL directory, and a JSON metadata file. There is no snapshot format, no compression and no remote destination (source: src/storage/backup.rs, BackupManager::create_backup).

Types

Type Purpose Notes
BackupType Full, Incremental, Differential Differential exists in the enum but nothing creates one
BackupMetadata Written to BACKUP_METADATA in each backup version 2, backup_id, backup_type, created_at, start_sequence, end_sequence, sstable_count, total_size, source_data_dir, manifest_checksum, manifest_log_checksum, sstable_files, sstable_checksums, wal_segments, parent_backup_id, is_consistent
WalSegment One copied WAL file filename, start_sequence, end_sequence, size, checksum (CRC32, hex), created_at
BackupOptions Options for a full backup Defaults: verify_checksums true, compress false, include_wal false, parallelism 4. full() and incremental() set include_wal true
RestoreOptions Options for a single-backup restore verify_checksums true, overwrite_existing false
PITROptions Options for a catalog restore target_time, target_sequence, verify_checksums true, overwrite_existing false, apply_wal true (when false, no WAL is copied)
BackupCatalog Index of backups under one root JSON file BACKUP_CATALOG

(source: src/storage/backup.rs, BackupType, BackupMetadata, WalSegment, BackupOptions, RestoreOptions, PITROptions, BackupCatalog)

Not wired: BackupOptions::compress and BackupOptions::parallelism are never read; copies are uncompressed and sequential. PITROptions::verify_checksums is ignored by restore_pitr (source: src/storage/backup.rs, BackupManager::create_backup, BackupManager::restore_pitr).

Checksums are CRC32 from the crc32fast crate, computed while copying in 1 MB chunks. Copied files are flushed but not fsynced, and the catalog is written with fs::write rather than a write-then-rename, so a crash during a backup can leave a partial file or catalog (source: src/storage/backup.rs, copy_file_with_checksum, compute_file_checksum, BackupCatalog::save). Not verified: no test covers a crash during a backup.

Full backup

BackupManager::create_backup(dir, options) runs inside a live engine (source: src/storage/backup.rs, BackupManager::create_backup):

  1. Refuse if dir already exists, then create it.
  2. Flush the memtable and sync the WAL.
  3. Take the compaction execution lock, then the engine's flush lock. While both are held no compaction can delete an SSTable and no flush can add one, so the manifest and the SSTable set stay consistent. The lock order matches StorageEngine::force_compaction.
  4. Copy MANIFEST, then the key-check file ENCRYPTION if present (copy_key_check), then MANIFEST.log.
  5. Load the copied manifest and list its SSTables. If a listed SSTable is missing on disk, fail with a Corruption error and remove the partial backup directory.
  6. Copy each SSTable, recording its checksum, and re-read the copy to verify it.
  7. Release both locks.
  8. If include_wal, copy every file in the WAL directory into dir/wal. Each segment is recorded with start_sequence 0 and end_sequence equal to the engine's current sequence number.
  9. Write BACKUP_METADATA with id backup-<unix ms>-<sequence> and is_consistent: true.
  10. Add the backup to the BACKUP_CATALOG in the parent directory of dir (the backup root).
  11. Call set_wal_backup_retain_seq(end_sequence) so WAL cleanup keeps every segment a later incremental backup will need.
sequenceDiagram participant B as "BackupManager" participant E as "StorageEngine" participant FS as "Backup dir" B->>E: flush, sync B->>E: lock compaction execution, then flush lock B->>FS: copy MANIFEST, ENCRYPTION, MANIFEST.log B->>FS: copy and verify each SSTable B->>E: release locks B->>FS: copy WAL dir (if include_wal) B->>FS: write BACKUP_METADATA B->>FS: update BACKUP_CATALOG in parent B->>E: set_wal_backup_retain_seq(end_sequence)

(source: src/storage/backup.rs, BackupManager::create_backup)

Writers that need the flush lock (for example a memtable rotation) wait during steps 3 to 7. Tested by backup_restores_runtime_flushed_and_wal_only_data, create_backup_registers_full_in_catalog_for_incrementals and restore_refuses_backup_missing_listed_sstable in src/storage/backup.rs. A drill on 2026-11-24 reports byte-identical restores for seven wire protocols and a 78 ms write stall during an online backup (source: docs/status/2026-11-24-backup-restore-drill.md).

Incremental backup

BackupManager::create_incremental_backup(root, parent_id) copies only WAL (source: src/storage/backup.rs, BackupManager::create_incremental_backup):

  • The new backup id is incr-<unix ms>-<sequence> and its files go to root/<id>/wal.
  • The parent is looked up in root/BACKUP_CATALOG. WAL segments whose sequence range reaches past the parent's end_sequence are copied; files whose name is not a recognised segment are copied whole.
  • If WAL cleanup has already removed the segment that starts at the parent's end_sequence, the backup is refused, because the chain would have a gap.
  • The catalog entry gets parent_backup_id set, and the WAL backup hold moves to the new end_sequence.

Tested by incremental_refuses_when_parent_wal_was_reclaimed and wal_retention_reclaims_but_keeps_backup_chain in src/storage/backup.rs, and by backup_wal_hold_survives_restart and corrupt_backup_wal_hold_refuses_to_open in src/storage/mod.rs.

The backup catalog

BackupCatalog is loaded from and saved to <root>/BACKUP_CATALOG (source: src/storage/backup.rs, BackupCatalog::load, BackupCatalog::save).

Method Behaviour
add_backup Records the entry by id; a full backup is appended to the list of full backups, an incremental is listed under its parent_backup_id
get_restore_chain(target_seq) Picks the newest full backup with end_sequence <= target_seq, then follows incrementals whose parent_backup_id links to the chain and whose start_sequence <= target_seq, preferring the one with the largest end_sequence
get_restore_chain_by_time(t) Same, by created_at
latest_backup Newest entry
apply_retention(keep) Drops the oldest full backups beyond keep, and every incremental descending from them; returns the ids dropped

(source: src/storage/backup.rs, BackupCatalog) Tested by test_backup_catalog, test_backup_catalog_retention and restore_chain_never_selects_full_backup_taken_after_target.

BackupManager::apply_retention(root, keep) deletes root/<id> for each id the catalog drops. A full backup created by create_backup into a directory not named after its id (the CLI --to path, or the HTTP default backup_<timestamp>_<n>) is removed from the catalog but its directory is not deleted (source: src/storage/backup.rs, BackupManager::apply_retention). Not verified: no test covers this case.

Restore

There are two restore functions. Both write into empty data and WAL directories, return a StorageConfig for the restored location, and create an empty current.wal (source: src/storage/backup.rs, BackupManager::restore, BackupManager::restore_pitr).

Single backup: restore

restore(backup_dir, data_dir, wal_dir, options) restores one full backup. With verify_checksums set it checks the checksum of MANIFEST, MANIFEST.log, every SSTable and every WAL segment against BACKUP_METADATA before copying. It refuses an existing target data directory unless overwrite_existing is set (source: src/storage/backup.rs, BackupManager::restore). Tested by backup_restores_runtime_flushed_and_wal_only_data.

Catalog restore and PITR: restore_pitr

restore_pitr(root, data_dir, wal_dir, options) restores a full backup plus its incrementals from a backup root (source: src/storage/backup.rs, BackupManager::restore_pitr):

Target given Chain chosen WAL cutoff
target_sequence = N get_restore_chain(N) Exclusive: entries with sequence >= N are dropped
target_time = T Newest consistent backup created at or before T None: time maps to backup boundaries only, not to a WAL position
neither Latest chain None

The base backup's manifest, key check, manifest log and SSTables are copied, then each WAL segment in the chain with start_sequence below the cutoff. Each copied WAL file is then cut by truncate_wal_copy_at_sequence. When the file has commit markers, it is cut at the end of the last whole batch that lies entirely below the cutoff, so a batch is never split; otherwise it is cut before the first entry at or above the cutoff (source: src/storage/backup.rs, truncate_wal_copy_at_sequence; commit markers are described in Write-ahead log).

flowchart TD A["restore_pitr(root, opts)"] --> B{"target?"} B -- "sequence N" --> C["get_restore_chain(N)"] B -- "time T" --> D["newest backup created at or before T"] B -- none --> E["latest chain"] C --> F["copy base MANIFEST, ENCRYPTION, MANIFEST.log, SSTables"] D --> F E --> F F --> G["copy chain WAL segments starting below cutoff"] G --> H["truncate_wal_copy_at_sequence"] H --> I["create current.wal"]

(source: src/storage/backup.rs, BackupManager::restore_pitr)

Recovery of the copied WAL happens the next time the engine opens the restored directory (see Write-ahead log). Tested by pitr_restores_to_sequence_cutoff_not_beyond and restore_chain_never_selects_full_backup_taken_after_target.

Verify, list and delete

verify_backup(dir) recomputes every checksum listed in BACKUP_METADATA and returns Ok(false) on a mismatch rather than an error. list_backups(root) reads the metadata of each subdirectory. delete_backup(dir) removes a backup directory (source: src/storage/backup.rs, BackupManager::verify_backup, BackupManager::list_backups, BackupManager::delete_backup). verify_backup flagging a torn backup is tested by restore_refuses_backup_missing_listed_sstable. Not verified: no test calls delete_backup.

Encrypted data directories

When at-rest encryption is on, files are copied as they are on disk, still encrypted, and the key-check file is copied with them. A restore therefore needs the same master key (or the key as a retired key) to open. The SST and manifest seals bind the file name, not the directory, so moved copies still open (source: src/storage/backup.rs, copy_key_check; src/storage/keyring.rs, file_id_for). See Encryption at rest internals.

Command line

The synergydb binary dispatches backup, restore and backup-verify to src/backup_cmd.rs (source: src/main.rs; src/backup_cmd.rs, run_backup, run_restore, run_verify).

Command What the code does
backup --to DIR [-c FILE] Requires an existing MANIFEST in the data directory and a DIR that does not exist. Opens the engine (the data-directory lock refuses if a server is running), runs create_backup with BackupOptions::full(), shuts down
restore --from PATH [--force] [--to-backup ID] [--to-sequence N] Refuses a non-empty data directory unless --force. Takes and releases the data-directory lock to make sure no server runs. If PATH is a single backup and no target is given, uses restore with verification; otherwise uses restore_pitr on the root, mapping --to-backup to that backup's end_sequence. Then opens the engine once, flushes and shuts down
backup-verify PATH / backup-verify --list ROOT verify_backup on one backup, or list_backups on a root

(source: src/backup_cmd.rs, run_backup, run_restore, run_verify) Not verified: no test runs these commands; the drill status entry above used them.

HTTP endpoints

The REST server exposes backup endpoints behind the same require_auth middleware as the other /api/v1 management routes (source: src/protocol/http/routes.rs, build_router, require_auth).

Method and path Handler What it does
GET /api/v1/backups list_backups Returns the in-memory list of backups taken since the server started
POST /api/v1/backups create_backup Runs a backup synchronously inside the request (see below)
GET /api/v1/backups/:id get_backup Looks up the in-memory list
DELETE /api/v1/backups/:id delete_backup Removes the entry from the in-memory list; files are not deleted
POST /api/v1/backups/:id/restore restore_backup Always answers 501 Not Implemented and points to the CLI
GET, POST /api/v1/backup-schedules, GET, PUT, DELETE /api/v1/backup-schedules/:id *_backup_schedule Stores schedules in memory only

(source: src/protocol/http/routes.rs, list_backups, create_backup, get_backup, delete_backup, restore_backup, BACKUPS, BACKUP_SCHEDULES)

POST /api/v1/backups takes a BackupRequest (source: src/protocol/http/models.rs, BackupRequest; src/protocol/http/routes.rs, create_backup):

Field Default Handling
database required Must be all or a valid identifier; otherwise ignored, the whole engine is backed up
backup_type full incremental chains to the newest backup in the catalog of the destination's parent directory and fails if there is none; any other value runs a full backup
compression lz4 Mapped to BackupOptions::compress, which is never read
destination <parent of data_dir>/backups/backup_<UTC timestamp>_<n> Treated as a local path. The model's comment mentions s3:// and gs://, but no code handles them
include_wal true Passed through

The response is the BackupInfo record with status completed (201) or failed (500). The list behind GET and DELETE is a process-global vector and is lost on restart (source: src/protocol/http/routes.rs, create_backup, BackupInfo).

Not wired: backup schedules are stored but never executed. src/storage/backup_scheduler.rs contains a cron parser and scheduler with its own unit tests (test_cron_parse_*, test_schedule_creation, test_next_run_calculation), but nothing outside the module calls it (source: src/storage/backup_scheduler.rs).

Not verified: no test calls the HTTP backup endpoints. The drill status entry reports an online backup over HTTP completing in under 0.1 s on its test data (source: docs/status/2026-11-24-backup-restore-drill.md).

Known gaps

Gap Source
No compression or parallel copy despite the options src/storage/backup.rs, BackupOptions
Copies and catalog not fsynced or atomically replaced src/storage/backup.rs, copy_file_with_checksum, BackupCatalog::save
Time-based PITR only reaches backup boundaries src/storage/backup.rs, BackupManager::restore_pitr
apply_retention misses full backups stored under other directory names src/storage/backup.rs, BackupManager::apply_retention
HTTP restore returns 501; HTTP list, delete and schedules are memory-only src/protocol/http/routes.rs
Remote destinations (s3://, gs://) not implemented src/protocol/http/routes.rs, create_backup
A comment in backup.rs still says WAL cleanup is disabled while the engine runs; the WAL backup hold replaced that src/storage/backup.rs; src/storage/wal.rs, cleanup_old_wal_files