Database / SQLite Interview questions
How do you back up a SQLite database?
Because a SQLite database is just an ordinary file, the simplest backup is a straightforward file copy — but that's only safe when you're certain no write is in progress at the same moment, since copying a file mid-write can capture an inconsistent snapshot.
# simple file copy (only safe if no writes are happening concurrently) cp mydata.db mydata_backup.db # safer: SQLite's built-in backup command, safe even with concurrent activity sqlite3 mydata.db ".backup mydata_backup.db"
The .backup command (or the equivalent sqlite3_backup_* C API) uses SQLite's
dedicated online backup mechanism, which correctly handles a database that's actively being read from or
written to during the backup, producing a consistent snapshot without requiring you to lock out other
activity first — the recommended approach over a raw file copy for any database that might be in active
use.
More Related questions...