# Back up a group of KV keys to R2

Export a selected prefix with binary values, metadata and absolute expiry times. Store a dated JSON file in a private R2 bucket.

[Use template](https://dashboard.fasthook.io/workflow/new?templateKey=cloudflare-kv-prefix-backups-to-r2&templateName=Back+up+a+group+of+KV+keys+to+R2)

**Required accounts**

- Cloudflare Workers KV
- Cloudflare R2

## Back up a group of KV keys to R2

### 1. Trigger · Daily schedule

**Schedule**. Run automatically on the configured schedule.

### 2. Action · Read keys and preserve their expiry

**Cloudflare Workers KV**. Export matching keys, binary values, metadata and absolute expiry times to a JSON file for restoration.

### 3. Action · Save the dated backup

**Cloudflare R2**. Run the R2 S3-compatible PutObject operation.

## Set up your workflow

1. **Choose the keys to include** Connect Workers KV, choose the source namespace and enter a prefix such as routing:. Use a separate prefix for configuration that must be restored together.
2. **Select the private archive bucket** Connect R2 and select the backup bucket. Each schedule occurrence produces a distinct JSON object under kv-backups/. Choose a quiet time because a prefix export reads keys over several requests.
3. **Check the archive** Open a test archive with your R2 connection. Entries include the key and a base64 value, plus metadata and absolute expiration when present. A narrower prefix is needed if the export exceeds 5 MiB or 10,000 keys.
4. **Practice a restore** Choose a separate empty KV namespace. Skip entries that have expired, write the remaining rows with Write a batch of keys, and compare restored bytes and metadata with the originals.

## Related integrations

- [Schedule trigger](https://www.fasthook.io/docs/triggers/schedule)
- [Cloudflare Workers KV integrations](https://www.fasthook.io/apps/cloudflare-workers-kv/integrations)
- [Cloudflare R2 integrations](https://www.fasthook.io/apps/cloudflare-r2/integrations)

## Related workflow templates

- [Archive DNS records to R2 on a schedule](https://www.fasthook.io/templates/cloudflare-dns-zone-exports-to-r2.md): Save a dated BIND zone file in a private R2 bucket.
- [Publish website redirects from Google Sheets](https://www.fasthook.io/templates/google-sheets-redirects-to-cloudflare-kv.md): Publish a checked table of same-site redirects in one KV write.
- [Archive a daily D1 SQL export in R2](https://www.fasthook.io/templates/cloudflare-d1-backups-to-r2.md): Export database schema and data as SQL and save a dated copy in the selected R2 bucket.
- [Report R2 bucket storage to Discord](https://www.fasthook.io/templates/cloudflare-r2-storage-report-to-discord.md): Post a daily report of object counts and bytes in one selected R2 bucket, grouped by storage class.
