# Ledger export and import

`ledgers.export` and `ledgers.import` use the same operation contract through
CLI, SDK, MCP and hosted HTTP. Select a connection as usual. These operations
move one ledger's retained records and files; they do not copy credentials,
account membership, local pending contributions or an entire application state root.

An import creates a **read-only replica**, not another authority. Original record
IDs, contract references, acceptance receipts and attachment IDs remain unchanged.
Every imported record and its read observation carries
`provenance: "imported-unverified"`: archive checksums establish byte integrity,
not proof that a source service authenticated the historical receipt. Authority
transfer and ordinary replica synchronization cannot activate an archive import.

## Export

Export requires `ledger.export`, `ledger.read`, `records.read` and
`contracts.read`. `files.read` includes file bytes and removal facts;
`access.read` includes the source policy. The `omitted` field declares missing
permission scopes. Missing or partial file bytes remain missing or partial.

```sh
op-sys --output json ledgers export --ledger-id <ledger-id> --limit 100
# Supply the returned next value to continue the same export:
op-sys --output json ledgers export --ledger-id <ledger-id> --continuation '<next>'
```

The CLI output envelope contains the archive page in `payload`. Save that page
rather than the CLI envelope when using it as import input. Each page has an
ordered archive commitment, page digest, previous digest, offset and total count.
Continuation is bound to the caller, current permissions, ledger state and source
connection. Any intervening source mutation invalidates continuation; restart
export rather than combine different snapshots. Tokens expire after 15 minutes.
An export is finished only when `complete` is true and `next` is null. Its
observation separately describes the source's coverage; finishing an archive
does not turn partial source coverage into complete history.

## Import

Import into an empty destination ledger identity, using a stable operation ID
for **all pages of that archive**. Source grants are retained as source information;
they never grant destination access. The authenticated importer receives replica
read/import/export permissions. Hosted import additionally requires the selected
context's creation, administrative and raw-data permissions.

```sh
op-sys --output json ledgers import --bundle @page.json --operation-id restore-1
```

```python
from op_sys import OpSys

source = OpSys(state_root=source_root, connection="source")
destination = OpSys(state_root=destination_root, connection="destination")
continuation = None
while True:
    page = source.ledgers.export(ledger_id=ledger_id, continuation=continuation)
    destination.ledgers.import_(bundle=page, operation_id="restore-1")
    continuation = page["next"]
    if continuation is None:
        break
```

MCP exposes `op_sys_ledgers_export` and `op_sys_ledgers_import` with the same
arguments. HTTP uses `GET /v1/ledgers/{ledger_id}/export` and
`POST /v1/ledger-imports`; the POST body is `{ "bundle": <page>,
"operation_id": "restore-1" }`. File paths are CLI conveniences on the caller's
machine. HTTP and MCP receive archive JSON, not server-side filesystem paths.

Pages are staged durably. Retrying an identical page after a restart or lost
response returns its saved result. Retrying it with changed content fails.
If a source change invalidates an unfinished export, begin a new export and use
`restart: true` (CLI `--restart true`) with its first page to explicitly replace
that incomplete import. A completed import cannot be restarted or overwritten.
Records become readable only after the final transaction verifies the complete
ordered manifest, contracts and file bytes. A failed final page leaves earlier
staged pages intact; resend the correct page with the same operation ID.

An import never replaces an existing authority, ordinary replica or a different
archive operation. This also prevents stale archives from overwriting known
removals. Use a new empty destination for an unrelated import. An archive in a
new store cannot know about erasures made at the source after export; it is a
historical, unverified copy, not a claim of current source authorization.

## Bounds and separate recovery scope

Pages contain at most 100 items within an 8 MiB response ceiling. Files use
chunks up to 1 MiB, retain the core 1 GiB per-file and 128 GiB per-ledger limits,
and preserve partial-upload status. One import is limited to one million items
and 192 GiB of encoded pages. Oversized archives fail explicitly; they are never silently
truncated. The first export page scans retained history to establish its archive commitment.
Signed continuations retain that commitment and skip earlier file ranges; later
pages do not reread the file bytes of earlier pages. Bulk backup throughput
remains unqualified.

State-root backup, approval-governed recovery, in-place database migration and
fenced authority transfer remain separate operations with their existing
protections. Archive import does not convert a superseded store format or make
imported history authoritative. Use the supported state migration/recovery path
for existing stores and the authority-transfer protocol to move write authority.
