Rsync / Sqlite database

Does anyone have ever tried rsync'ing sqlite database?

Is it possible to access the data while it gets syncronised?


Solution 1:

I would consider this dangerous.

The SQLite database also has journal files that need to be preserved.

If you

  • rsync the database without the journal files in the middle of a transaction
  • copy the database file
  • access the copy

It is highly likely that you will experience corruption.

Use the SQLite Online Backup API instead.

Solution 2:

Assuming you'd like to do this via shell commands, you could do something along the lines of:

ssh user@host 'sqlite3 "/path/to/db.sqlite" ".backup /path/to/dbbackup.sqlite"'
rsync user@host:"/path/to/dbbackup.sqlite" "db.sqlite"

Which first, safely creates a backup of the existing database, and then copies it to "db.sqlite" on your own machine.

Solution 3:

As Noah have pointed, it is dangerous. Your database will get corrupted.

You can use the litereplica library instead.

It does all the work behind the scenes respecting the normal SQLite transaction. And incrementally (only the changed pages are transferred).

Or the rqlite library.

This last one requires the installation of the Go language.