What's the proper way to do it? Do I just copy the .sq3 file?
What if there are users on the site and file is being written while it's being copied?
-
10SQLite has an API for thisColonel Thirty Two– Colonel Thirty Two2014年09月04日 23:47:37 +00:00Commented Sep 4, 2014 at 23:47
-
1What language and driver are you using to access the database?CL.– CL.2014年09月05日 07:28:52 +00:00Commented Sep 5, 2014 at 7:28
-
1i'm using PHP and the PDO extensionthelolcat– thelolcat2014年09月05日 21:46:21 +00:00Commented Sep 5, 2014 at 21:46
-
There is currently a feature request to expose the sqlite backup API in php: bugs.php.net/bug.php?id=70950Brian Minton– Brian Minton2017年11月30日 20:26:37 +00:00Commented Nov 30, 2017 at 20:26
-
2There is also a VACUUM INTO command as an alternative to the backup API.Rüdiger Herrmann– Rüdiger Herrmann2022年08月16日 08:19:50 +00:00Commented Aug 16, 2022 at 8:19
5 Answers 5
The sqlite3 command line tool features the .backup dot command.
You can connect to your database with:
sqlite3 my_database.sq3
and run the backup dot command with:
.backup backup_file.sq3
Instead of the interactive connection to the database, you can also do the backup and close the connection afterwards with
sqlite3 my_database.sq3 ".backup 'backup_file.sq3'"
Either way the result is a copy named backup_file.sq3 of the database my_database.sq3.
It's different from regularly file copying, because it takes care of any users currently working on the database. There are proper locks set on the database, so the backup is done exclusively.
14 Comments
sqlite3 m_database.sq3 ".backup m_database.sq3.bak"database is locked errors... sqlite3 source.db ".timeout 1000" ".backup backup.db".backup is the best way:
sqlite3 my_database ".backup my_database.back"
you can also try .dump command, it gives you the ability to dump the entire database or tables into a text file. If TABLE specified, only dump tables matching LIKE pattern TABLE.
sqlite3 my_database .dump > my_database.back
A good way to make an archival copy using dump and store, Reconstruct the database at a later time.
sqlite3 my_database .dump | gzip -c > my_database.dump.gz
zcat my_database.dump.gz | sqlite3 my_database
Also check this question Do the SQLite3 .backup and .dump commands lock the database?
6 Comments
.backup doesn't work as shown above ("missing FILENAME argument on .backup")Short and simple answer would be
sqlite3 m_database.sq3 ".backup m_database.sq3.bak"
Comments
For streaming replication of SQLite, check out Litestream.
Compared to using the sqlite3-backup command, this is automatic, and incremental.
If you need to restore from backup, the data will be a lot more up to date than if you did a regular backup every hour for example.
Comments
People who want to do the backup from within their app should check out the backup API at https://www.sqlite.org/backup.html