INTEGRITY Cloudflare Docs

Time Travel and backups

Time Travel is D1's approach to backups and point-in-time-recovery, and allows you to restore a database to any minute within the last 30 days.

By not having to rely on scheduled backups and/or manually initiated backups, you can go back in time and restore a database prior to a failed migration or schema change, a DELETE or UPDATE statement without a specific WHERE clause, and in the future, fork/copy a production database directly.

Bookmarks

Time Travel leverages D1's concept of a bookmark to restore to a point in time.

Bookmarks are also leveraged by Sessions API to ensure sequential consistency within a Session.

Timestamps

Time Travel supports two timestamp formats:

Requirements

Retrieve a bookmark

You can retrieve a bookmark for the current timestamp by calling the d1 info command, which defaults to returning the current bookmark:

wrangler d1 time-travel info YOUR_DATABASE
🚧 Time Traveling...
⚠️ The current bookmark is '00000085-0000024c-00004c6d-8e61117bf38d7adb71b934ebbf891683'
⚡️ To restore to this specific bookmark, run:
 `wrangler d1 time-travel restore YOUR_DATABASE --bookmark=00000085-0000024c-00004c6d-8e61117bf38d7adb71b934ebbf891683`

To retrieve the bookmark for a timestamp in the past, pass the --timestamp flag with a valid Unix or RFC3339 timestamp:

Using an RFC3339 timestamp, including the timezone
wrangler d1 time-travel info YOUR_DATABASE --timestamp="2023-07-09T17:31:11+00:00"

Restore a database

To restore a database to a specific point-in-time:

wrangler d1 time-travel restore YOUR_DATABASE --timestamp=UNIX_TIMESTAMP
🚧 Restoring database YOUR_DATABASE from bookmark 00000080-ffffffff-00004c60-390376cb1c4dd679b74a19d19f5ca5be

⚠️ This will overwrite all data in database YOUR_DATABASE.
In-flight queries and transactions will be cancelled.

✔ OK to proceed (y/N) … yes
⚡️ Time travel in progress...
✅ Database YOUR_DATABASE restored back to bookmark 00000080-ffffffff-00004c60-390376cb1c4dd679b74a19d19f5ca5be

↩️ To undo this operation, you can restore to the previous bookmark: 00000085-ffffffff-00004c6d-2510c8b03a2eb2c48b2422bb3b33fad5

Note that:

Undo a restore

You can undo a restore by:

To fetch a bookmark from an earlier state:

wrangler d1 time-travel info YOUR_DATABASE
🚧 Time Traveling...
⚠️ The current bookmark is '00000085-0000024c-00004c6d-8e61117bf38d7adb71b934ebbf891683'
⚡️ To restore to this specific bookmark, run:
 `wrangler d1 time-travel restore YOUR_DATABASE --bookmark=00000085-0000024c-00004c6d-8e61117bf38d7adb71b934ebbf891683`

Export D1 into R2 using Workflows

You can automatically export your D1 database into R2 storage via REST API and Cloudflare Workflows. This may be useful if you wish to store a state of your D1 database for longer than 30 days.

Refer to the guide Export and save D1 database.

Notes