Hi Wilhelm!
Well, copying the SQLite database file is not a good backup approach. The database copy can be corrupted.
We have 2 alternatives:
Use the SQLite backup interface
Use LiteReplica
In this last option you can open a connection to the master node and check for the replication completion. This can be done in 3 ways:
Using the PRAGMA replica_status
command, that returns a JSON object.
Using the sqlite3_replica_status()
function.
Registering a callback function via the sqlite3_replica_hook()
function and checking the local_db_updated parameter.
The callback function must have this format (in C):
void replica_status_event(void *userdata, char *address, int conn_status, int replica_db_state, int local_db_updated);
Note that this function is called by the worker thread (not the main thread!) so it must do simple and fast things, like just sending a message to the main thread of your application.
Something like this:
void replica_status_event(void *userdata, char *address, int conn_status, int replica_db_state, int local_db_updated){
if(local_db_updated){
notify_main_thread(...);
}
}
Here is an example of how to use the sqlite3_replica_status()
function:
replica_status status={0};
if( sqlite3_replica_status(db, name, &status)!=SQLITE_OK ){
puts("could not retrieve the replica status");
return;
}
if( status.is_slave && status.db_state==DB_STATE_UPDATED ){
do_domething();
}