# Back Up a Cluster

Source: https://docs.formance.com/v4.0/modules/ledger/operations/backup

Stack: v4.0

A backup is useful only when you can identify its recovery point, restore it with the intended Ledger release, and verify the restored state. Keep the backup manifest and its sequence boundaries with every recovery record.

Ledger provides two backup operations:

- a **full backup** captures a point-in-time checkpoint of the cluster's primary hot data;
- an **incremental backup** exports log and audit entries committed after the existing backup boundary.

Create a full backup before the first incremental backup. A later full backup establishes a new base and replaces the previous incremental chain.

## Before you start

Confirm that the active `ledgerctl` profile targets the intended cluster and that the cluster has a leader. When authentication is enabled, both `ledgerctl store backup` and `ledgerctl store incremental-backup` require the granular `ledger:ClusterWrite` scope. The virtual `ledger:admin` scope includes this permission; `ledger:write` does not.

The active Ledger leader, not the `ledgerctl` host, opens the object-storage connection. Ensure every node that may become leader can reach the destination and has the required read, write, list, and delete permissions through the provider's default credential chain. When you pass static credentials as CLI flags, `ledgerctl` serializes them into the backup RPC instead; protect that invocation and do not expose the values in process logs or scheduler configuration. Verify the complete server-side path against a non-production destination before scheduling a production backup.

Ledger's local usage projection is not part of a full or incremental backup. The store at `<data-dir>/usage/usagedb` contains derived ledger statistics and Numscript template usage; it is reconstructed from the restored audit chain after Ledger starts. A successful backup therefore does not preserve or certify those figures.

## Choose a destination

Backend support depends on how you run the backup:

| Surface                                                           | Supported destinations                                                 |
| ----------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `ledgerctl store backup` and `ledgerctl store incremental-backup` | Amazon S3, S3-compatible storage such as MinIO, and Azure Blob Storage |
| The operator's scheduled `Backup` resource                        | Amazon S3 and S3-compatible storage only                               |

Use workload identity or the provider's default credential chain on every possible Ledger leader where possible. If you use static credentials, store and rotate them through your deployment's secret-management process rather than committing them with the backup configuration or scheduler.

## Create a full backup

The following command writes a full backup to S3:

```bash
ledgerctl store backup \
  --driver s3 \
  --s3-bucket ledger-production-backups \
  --s3-region eu-west-1 \
  --bucket-id ledger-production
```

For an S3-compatible service, add its endpoint:

```bash
ledgerctl store backup \
  --driver s3 \
  --s3-bucket ledger-backups \
  --s3-endpoint https://minio.example.com \
  --bucket-id ledger-production
```

For Azure Blob Storage:

```bash
ledgerctl store backup \
  --driver azure \
  --azure-account-name financearchive \
  --azure-container ledger-backups \
  --bucket-id ledger-production
```

When you omit `--azure-account-key`, Ledger uses `DefaultAzureCredential`. When you omit static S3 credentials, Ledger uses the default AWS credential chain.

The cluster continues to accept writes while it creates the checkpoint. The reported log, audit, and applied-index boundaries identify the state included in that backup.

At a given `bucketId`, a full backup publishes a new manifest with an empty incremental-export list. Only after the new manifest is durable does Ledger remove the checkpoint objects and incremental exports that the previous manifest no longer references. This ordering protects the new backup from an interrupted cleanup, but it also means that one `bucketId` represents one current backup chain rather than a built-in history of full-backup generations.

Object-store versioning and retention are separate from Ledger backup retention. If you need multiple independent recovery generations, use protected object versions, replication, or distinct `bucketId` values and test how you select each generation during restore.

## Add incremental backups

Run incremental backups more frequently than full backups to reduce the recovery point objective:

```bash
ledgerctl store incremental-backup \
  --driver s3 \
  --s3-bucket ledger-production-backups \
  --s3-region eu-west-1 \
  --bucket-id ledger-production
```

Ledger appends new log and audit segments to the backup manifest. It serializes backup work for the same destination. If leadership changes or a run fails, inspect the result and retry the command; do not infer completion from the presence of a partially uploaded object.

That serialization is cluster-wide rather than per node, and full and incremental runs share one slot per destination. Whichever run reaches the destination second is rejected with `backup already in progress for this destination` instead of racing the first. Overlapping schedules therefore produce failed runs rather than damaged chains — but which run fails is not something you can predict. Nothing gives a full backup precedence over an incremental one, so a weekly full at `0 2 * * 0` and an hourly incremental at `0 * * * *` can lose either member of the Sunday 02:00 pair, and losing the full is the more expensive outcome: the next one is a week away.

Offset the incremental schedule away from the full-backup slot by more than a full backup usually takes. That is a mitigation, not a guarantee — an overrunning run will still occasionally collide with the next slot — so treat this specific rejection as an expected event rather than paging on it, and alert instead on a destination that has gone too long without a successful run of either kind.

## Schedule backups with the Ledger operator

The Ledger Kubernetes operator ships a `Backup` resource that runs both commands on cron schedules. Each firing creates a `BackupRun`, which executes the matching `ledgerctl` invocation in a one-shot Job and reports its outcome back on the parent `Backup`:

```yaml
apiVersion: ledger.formance.com/v1alpha1
kind: Backup
metadata:
  name: production
spec:
  clusterRef: customer-ledger
  destination:
    driver: s3
    bucketId: customer-production
    s3:
      bucket: customer-ledger-backups
      region: eu-west-1
  schedule:
    full: "0 2 * * 0"
    incremental: "30 * * * *"
  successfulRunsHistoryLimit: 3
  failedRunsHistoryLimit: 1
```

The incremental schedule above is offset to the half hour so the two never fire in the same minute, which is the collision described under [Add incremental backups](#add-incremental-backups). An offset narrows that window but does not close it: each field takes a single five-field cron expression, so neither schedule can carve out a gap around the other, and a full backup running longer than thirty minutes still meets the following incremental. Size the offset against the full-backup duration you actually observe, and expect an occasional rejection when a run overruns it.

Both schedule entries are optional, and the operator holds the incremental schedule back until at least one full run has succeeded. A `Backup` with neither entry is a valid destination template for `BackupRun` resources you create yourself.

The resource status reports the same boundaries the CLI does. `status.lastFullBackup` and `status.lastIncrementalBackup` each carry the completion time, duration, and the last log and audit sequences, and the full-backup status adds the last applied index and the file counts uploaded, deleted, and pruned.

Plan around four constraints:

- the operator's destination driver accepts `s3` only. Azure Blob Storage is available through `ledgerctl` but not through a scheduled `Backup`;
- `successfulRunsHistoryLimit` and `failedRunsHistoryLimit` retain Kubernetes `BackupRun` resources per run type. They do not retain backup artifacts or restore points in object storage;
- one `Backup` carries one fixed `bucketId`, so rotating immutable retention points requires orchestration outside the operator;
- `spec.timeout`, one hour by default, caps how long each `ledgerctl` invocation may run before the client aborts. Size it against the ledger and the throughput of the destination bucket.

Whether a given Formance Stack release installs this operator is a separate question from whether the operator supports scheduled backups. Check which custom resources your Stack version provides before relying on either. Where the operator is unavailable, schedule the `ledgerctl` commands with your existing workload scheduler and reproduce the same properties: a stable `--bucket-id`, distinct full and incremental schedules, and a recorded terminal status carrying the sequence boundaries.

Treat the `--bucket-id` and `spec.destination.bucketId` value as a stable operational identifier. Change it deliberately when starting a separate recovery chain, and record which chain each restore exercise used.

## Record proof of completion

For every successful run, retain:

- the destination and `bucketId`;
- the backup manifest and its creation time;
- the last log sequence, last audit sequence, and last applied index reported by the run;
- the number of checkpoint files or incremental segments written;
- the Ledger release used to create the backup;
- the command result or scheduler status.

When you schedule backups through the operator, require both the terminal success state and the sequence boundaries recorded on the `Backup` status. A completed Kubernetes Job alone is not sufficient evidence that the manifest was published.

A successful upload proves that the backup operation completed. It does not prove that the data can be restored or that the recovered application state meets your requirements. Rehearse the procedure in [Restore a Cluster](https://docs.formance.com/v4.0/modules/ledger/operations/restore) and record the result.

## Define the recovery policy

Document these decisions before production:

1. Set the maximum acceptable data loss and derive the incremental schedule from it.
2. Set the recovery-time objective and test it with production-scale data.
3. Retain enough independent recovery generations to survive a damaged latest chain; a new full backup at the same `bucketId` replaces the manifest and purges objects from the previous chain.
4. Apply object lock, versioning, encryption, and access controls according to your risk model.
5. Assign an owner to review failures and expired recovery exercises.

The recovery set for a cluster is the latest usable backup chain on its own. Ledger 3.0 keeps every log and audit entry in the primary store for the life of the bucket, so a backup captures the whole of it — there is no separate archive tier to retain alongside it.

After a restore, the usage projection folds the audit entries reachable from the recovered primary store. A backup chain that covers the whole store therefore reproduces the full counters; a partial or truncated recovery is the only way they settle below their historical totals, and `formance.ledger.usage.builder.lag` reaching zero says nothing about whether the recovery was complete. Record which backup chain a restore came from whenever these figures feed reporting, billing, or limits.
