I have a Django application that uses a Postgres database. I need to be able to backup and restore the db, both to ensure no data is lost and to be able to copy data from the production server to the development server during testing.
There seem to be a few different ways to do this:
Just interact with the db directly. So, for Postgres I might write a script using pg_dumpall
and psql
.
Use the sqlclear
/sqlall
commands that come with Django.
Use the dumpdata
/loaddata
commands that come with Django. So create new fixtures from the db you want to backup and then load them into the db you want to restore.
Use a Django plugin like django-dbbackup.
I really don't understand the pros/cons of these different techniques.
Just off the top of my head: Option 1 is database-specific and option 3 seems more suited to setting up initial data. But I'm still not sure what advantages option 4 has over option 2.
Back up a database Open the database for which you want to create a backup copy and do the following: Click File, and then click Save As. Under File Types, click Save Database As. Under Advanced, click Back Up Database, and then click Save As.
Django officially supports the following databases: PostgreSQL. MariaDB. MySQL.
sqlite3'. The file is database file where all the data that you will be generating will be stored. It is a local file as Django is a server-side framework and it treats your computer as the host when you actually run the server in command line/terminal.
Step 1 — Creating the Database. Django supports a number of popular database management systems, but this guide focuses on connecting Django to a MySQL database. In order to do this, you need to create a database on your MySQL instance as well as a MySQL user profile that Django can use to connect to the database.
The problem with options 1-3 are that media files (anything uploaded through FileField
) are not included in the backup. It is possible to separately backup the directory containing the media files. However, because Django doesn't remove files when they are no longer referenced by a FileField
, you will inevitably end up with files in the backup that don't need to be there.
That's why I would go with option #4. In particular, I recommend django-archive*. Some of its features include:
Dumps the contents of all important models (by default ContentType
, Permission
, and Session
are excluded since they are populated by manage.py migrate
) and lets you choose additional models to exclude.
Includes media files referenced by FileField
and ImageField
fields. Note that only the files referenced by rows in the database are included; files left over by deleted rows are ignored.
Produces a single archive containing both the database backup and media files.
Provides options for customizing the location where archives should be stored, the filename format, and archive type (gz
and bz2
).
Installation is as simple as adding django_archive
to INSTALLED_APPS
and setting options in settings.py
if needed. Once installed, you can immediately create an archive of your entire database (including media files) by running:
./manage.py archive
* Disclaimer: I am the author of the package
For regular backups I'd go for option 1, using PostgreSQL's own native tool, as it is probably the most efficient.
I would argue that option 2 is primarily concerned with creating the tables and loading initial data so is not suitable for backups.
Option 3 can be used for backups and would be particularly useful if you needed to migrate to a different database platform since the data is dumped in a non-SQL form, i.e. JSON understood by Django.
Option 4 the plugin appears to be using db's own backup tools (as per option 1) but additionally provides help to push your backups into cloud storage in Amazon S3 or Dropbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With