156

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?

5

6 Answers 6

224

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.

15
  • 84
    You can do it all in one line... sqlite3 m_database.sq3 ".backup m_database.sq3.bak" Sep 7, 2014 at 18:02
  • 4
    @mOna: This is just a mechanism for making backups. Replication means propagating changes on the fly (kind of a distributed database), which this won't do for you.
    – Googie
    Oct 19, 2016 at 8:22
  • 2
    @RonJohn It actually makes a copy of a file, but it also ansures, that write access to the database is restricted with proper locks, so it's an atomic operation, no intermediate modifications.
    – Googie
    Dec 24, 2018 at 0:58
  • 1
    @RadioControlled As I said earlier - this method locks database to avoid data corruption when there are concurrent users. If other user was first to lock the database (like in your case), then of course backup request will fail, cause it cannot secure a lock.
    – Googie
    May 17, 2019 at 9:16
  • 5
    You should also specify a timeout to minimize database is locked errors... sqlite3 source.db ".timeout 1000" ".backup backup.db"
    – Melvyn
    Jun 7, 2020 at 19:03
25

.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
  • 8
    On SQLite 3.8.2, .backup doesn't work as shown above ("missing FILENAME argument on .backup") Oct 18, 2017 at 9:11
  • 5
    This is the best answer. If you are using .backup on a working database used by many it may not work because at some point the database is locked. So if you are using this in a CRON it won't work and won't tell you there is an error... better use .dump (allways works) or the API given by SQLite.
    – Memristor
    Apr 7, 2018 at 11:51
  • @Memristor But doesn't .dump lock the DB for others? FWIW, I would prefer a failed backup (with mail to the admin) to a disrupted service. Sep 9, 2018 at 5:34
  • 1
    Please fix .backup syntax in your answer. It doesn't need '>' operator
    – Nashev
    Dec 6, 2018 at 14:59
  • @Memristor but locks exist for a reason, i.e. to not dump a corrupted database in the middle of a write. Problem is better solved by first issuing a .timeout [ms]; command before .backup; That way .backup will wait for lock to be released up to [ms] milliseconds before failing.
    – JohnyTex
    Feb 23, 2022 at 12:40
5

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.

3

Short and simple answer would be

sqlite3 m_database.sq3 ".backup m_database.sq3.bak"

0

People who want to do the backup from within their app should check out the backup API at https://www.sqlite.org/backup.html

1
  • note: some example usage could improve the long-term value of this post.
    – starball
    May 6 at 0:37
0

In the simplest form you can run from project root a command of the following form (default format is JSON, other options include XML, YAML, see the links below):

./manage.py dumpdata --output mydata.json

You might want instead to dump only specific django apps in your project, e.g. assuming you created your app with manage.py startapp dinsdaleapp:

./manage.py dumpdata dinsdaleapp --output dinsdaleapp-data.json

It's also possible to only dump specific models:

./manage.py dumpdata dinsdaleapp.institution -o institutions-data.json

To restore the previously created dump

./manage.py loaddata mydata.json

To dump flatpages:

./manage.py dumpdata flatpages -o flatpages.json

In case you prefer a timestamped backup:

./manage.py dumpdata flatpages -o dumps/flatpages-$(date +%Y-%m-%d_%H_%M_%S).json

For details and options see:

./manage.py dumpdata --help
./manage.py loaddata --help

Also see:

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.