Backups (Legacy)
D1 has built-in support for creating and restoring backups of your databases, including support for scheduled automatic backups and manual backup management.
D1 automatically backs up your databases every hour on your behalf, and retains backups for 24 hours. Backups will block access to the DB while they are running. In most cases this should only be a second or two, and any requests that arrive during the backup will be queued.
To view and manage these backups, including any manual backups you have made, you can use the d1 backup list <DATABASE_NAME>
command to list each backup.
For example, to list all of the backups of a D1 database named existing-db
:
The id
of each backup allows you to download or restore a specific backup.
Creating a manual backup of your database before making large schema changes, manually inserting or deleting data, or otherwise modifying a database you are actively using is a good practice to get into. D1 allows you to make a backup of a database at any time, and stores the backup on your behalf. You should also consider using migrations to simplify changes to an existing database.
To back up a D1 database, you must have:
- The Cloudflare Wrangler CLI installed
- An existing D1 database you want to back up.
For example, to create a manual backup of a D1 database named example-db
, call d1 backup create
.
Larger databases, especially those that are several megabytes (MB) in size with many tables, may take a few seconds to backup. The state
column in the output will let you know when the backup is done.
To download a backup locally, call wrangler d1 backup download <DATABASE_NAME> <BACKUP_ID>
. Use wrangler d1 backup list <DATABASE_NAME>
to list the available backups, including their IDs, for a given D1 database.
For example, to download a specific backup for a database named example-db
:
The database backup will be download to the current working directory in native SQLite3 format. To import a local database, read the documentation on importing data to D1.
Restoring a backup will overwrite the current running version of a database with the backup. Database tables (and their data) that do not exist in the backup will no longer exist in the current version of the database, and queries that rely on them will fail.
To restore a previous backup of a D1 database named existing-db
, pass the ID of that backup to d1 backup restore
:
Any queries against the database will immediately query the current (restored) version once the restore has completed.