Hi everyone,
I am running a Django application using the default SQLite database. I am planning the backup and want to make sure I implement the correct, safe approach. Can I simply copy the .sqlite3 file? What is the best approach to automate this?
I would appreciate any insights on how to handle this safely. Thanks!
Yes - but to be safe, you want to stop your Django app if there’s any chance that someone might be updating data in it when you make your copy.
If you wanted to do this on a scheduled basis, I’d suggest setting up a cron job to:
- Stop the app.
- Copy the database
- Restart the app.
Couple of techniques here too if shutting down isn’t an option - SQLite Backup API and https://sqldocs.org/sqlite-backup/
Depending on how busy your site is, just copying the sqlite database can be fine, but the safest option is to use the VACUUM INTO command. I have a little backup script I drop into all my projects that backs up a sqlite database to an S3 bucket, but the key bit is:
sqlite3 db.sqlite3 "VACUUM INTO 'backup.sqlite3'"
db.sqlite3 is the name of your database file, and backup.sqlite3 is the name of the backup file.