Background Backup of a SQLite3 Database
By A.Bouchez on 2014, Friday August 15, 18:15 - mORMot Framework - Permalink
The primary purpose of any software Backup is to recover data after
its loss, be it by data deletion or corruption.
Data loss can be a common experience of computer users. A 2008 survey found
that 66% of respondents had lost files on their home PC, as Wikipedia quotes.
As a consequence, for any professional use of data, like in our mORMot server, a backup policy is mandatory.
We just introduced officially the SQLite3 Backup API to our low-level
SynSQLite3.pas
unit, and wrote dedicated methods to make
background backup of a running mORMot server easy and safe, without
any noticeable performance penalty.
In all cases, do not forget to perform backups of your SQlite3 database as often as possible (at least several times a day)!
Adding a backup feature on the server side is as simple as running:
Server.DB.BackupBackground('backup.db3',1024,10,nil);
The above line will perform a background live backup of the main
SQLite3 database, by steps of 1024 pages (i.e. it would process 1 MB
per step, since default page size is 1024 bytes), performing a little sleep of
10 milliseconds between each 1 MB copy step, allowing main CRUD / ORM
operations to continue uninterrupted during the backup.
You can even specify an OnProgress: TSQLDatabaseBackupEvent
callback event, to monitor the backup process.
Note that TSQLRestServerDB.Backup
or
TSQLRestServerDB.BackupGZ
methods are not recommended any more on
a running mORMot database, due to some potential issues with virtual
tables, especially on the Win64 platform. You should definitively use
TSQLDatabase.BackupBackground()
instead.
The same backup process can be used e.g. to save an in-memory SQLite3 database into a SQLite3 file, as such:
if aInMemoryDB.BackupBackground('backup.db3',-1,0,nil) then aInMemoryDB.BackupBackgroundWaitUntilFinished;
Above code will save the aInMemoryDB
database into the
'backup.db3
' file.
We wish you a safe and happy backup of your precious objects!