SQLite Forum

how to backup a locked DB
Login

how to backup a locked DB

(1) By anonymous on 2020-04-27 16:53:34 [link]

Hey.

Is there any way to backup a locked DB from the command line utility?

.backup doesn't seem to work, as it complains:
Error: database is locked


Thanks,
Chris.

(2) By David Raymond (dvdraymond) on 2020-04-27 17:29:27 in reply to 1 [link]

Not that I'm aware of, no. If it's locked to the point where you can't read it with the library, then odds are decent that another process may be writing to the file at that moment. And trying to backup a file that's actively being written to is probably a bad call.

(3) By anonymous on 2020-04-27 17:56:53 in reply to 2 [link]

Okay that's pretty strange... any other DBMS I can think of allows to do online backups.

(4) By anonymous on 2020-04-28 03:31:37 in reply to 3 [link]

You would probably be able to backup while a write is ongoing if you are in the WAL journaling mode. As it allows readers to continue even when a writer is in progress, unlike the default journaling mode.

(5) By David Raymond (dvdraymond) on 2020-04-28 12:31:20 in reply to 3 [link]

With an online DBMS there's a master server process, and your backup request goes to the server process. That server process is the only thing that touches files, and it can coordinate with all the other requests going on, because it has complete overlord power and doesn't have to worry about rogue processes jumping in and trying to do stuff with the files. A server DBMS might use row locking, or table locking, etc. along with various other methods of concurrency control that let it do a consistent backup while others reading and writing requests are going on at the same time.

With SQLite there is no overlord server process to coordinate things. Everyone is accessing the database files directly themselves, not requesting someone else to do it and send them the results. The only lock on a SQLite database is for the entire database. If you're writing, then nobody else is.

WAL mode means that readers and writer (still singular) can coexist, and as mentioned should probably be your first option. WAL mode has some restrictions, notably that all the processes accessing a database have to be on the same computer. If that's ok, then the library should be able to open a read connection at any time to make a backup from.

(Barring of course anyone explicitly opening the database in exclusive mode, like I think Firefox does with its SQLite files when it's running)

(8) By anonymous on 2020-04-29 21:05:12 in reply to 5 [link]

Without having a system right now to test it on, I believe that last point is key. If it's got an exclusive lock you'll get that error. If not, the backup system will restart if anyone writes to it but it should proceed.

Donald Shepherd

(6) By Simon Slavin (slavin) on 2020-04-29 13:17:23 in reply to 3 [link]

Sure.  And if you do it while the data is being changed you get a corrupt backup.

(7) By Keith Medcalf (kmedcalf) on 2020-04-29 15:41:31 in reply to 6

The technical term is "crash consistent".

(9) By anonymous on 2020-04-29 21:06:23 in reply to 6 [link]

If you're using the backup API, it will restart it's attempt to generate a backup rather than result in corrupt data when there's a write.

Donald Shepherd