3 Ways to Backup an SQLite Database

When it comes to backing up your databases in SQLite, you have a few options as to how to go about it.

In particular, you can use one of the following methods:

  • Use the .backup command to back up a specified database
  • Use the .dump command to export the database to a .sql file
  • Use the .clone command to clone the database

The .backup Command

The .backup command accepts two arguments; the name of the database to back up, and the name of the database you want to back it up to.

Here’s an example:

.backup pets pets_backup.db

This example backs up a database called pets to a file called pets_backup.db.

This example puts the file into the current directory. To place it into a different directory, specify the path.

The .dump Command

The .dump command converts a whole database to a single ASCII text file. It renders the entire database as SQL (creates all objects, inserts data, etc), so you can reconstruct the database by simply running the SQL against a new database.

This command accepts an optional argument that specifies a table to script, but if you omit this argument, all tables are scripted.

Here’s an example of usage:

.once pets_backup.sql
.dump

Here I use .dump in conjunction with the .once command. The .once command specifies that the next command will be written to the specified file.

You can alternatively use .output, which specifies that all future commands will be written to the specified file.

The .clone Command

The .clone command simply clones the current database. The only argument it accepts is for the destination file. You can’t specify a different database to clone. If you want to do that, you’ll need to switch over to that database first.

Here’s how .clone works:

.clone pets_backup.db

Again, you can provide the path to the file if it needs to be saved to a different directory.