Backup and restore a Django site

Data can and will get lost. In this tutorial we will backup and restore the data in our Django / Wagtail site.

July 16, 2020, 3 p.m.
Themes: Deployment

Backing up should need no further justification: it is crucial to preserve not only the contents of your site but also user information. In this tutorial we will backup the contents of our site including all mediafiles with the package dbbackup and restore it in an empty environment.

In this tutorial we will backup on our production environment. You can follow the same steps to backup your development environment; it is a good idea to try the process out before executing it on a live server environment. On your development computer, activate your virtual environment and install dbbackup:

pip3 install django-dbbackup

Add it to your requirements.txt file and add the app to your INSTALLED_APPS:

DBBACKUP_STORAGE_OPTIONS = {'location': os.path.join(BASE_DIR, '../backups')}

The first parameter points to the class Django uses to implement basic file storage. The second parameter sets the location for backups. You can use any path you like; the setting above puts backups in a directory at the same level as the project. Create this directory /backups. There is no need to migrate the database. Push the changes to your repository, go to the server, activate your virtual environment there and pull the changes from the repository. Create the directory /backups on your server as well. Install the package, either with the same command as above, or with

pip3 install -r requirements.txt
python3 dbbackup
python3 mediabackup

You should now see two new files with the backups in the directory /backups.

With these files we can restore to the existing database (the one we backed up) or to another one, either on our development computer or on our production server. If you restore in a new environment, dbbackup should obviously be installed there as well, and the location for the backups should be correctly set. Copying the backup files from the remote server to the current directory can be done with the scp command

scp usr_pet@\* .

where usr_pet is the authorized user for the project with home directory /home/usr_pet and the IP-address is the address of your server. The backslash here serves as an escape character and is only necessary with zsh.

To restore in a new environment:

  • make sure the version of PostgreSQL is the same as on the server. You can check the version at a psql prompt with: SELECT version(); Otherwise install the right version (e.g. on Mac with Homebrew) and start it. You then have to create a database user first (see first tutorial).
  • create a backup database at a psql prompt: CREATE DATABASE db_pet_backup OWNER usr_pet;
  • create a directory: mkdir pet_backup && cd pet_backup
  • create a virtual environment in it: python3 -m venv env && source env/bin/activate
  • clone the repository: git clone
  • enter the project directory and install all requirements: pip3 install -r requirements.txt
  • create a settings file with all necessary passwords (or copy it from the server); make sure the database name is db_pet_backup
  • migrate the database: python3 migrate

You now have an empty site. Put the directory /backups with the backup files at the same level as the project directory. Restore the data and media with:

python3 dbrestore
python3 mediarestore

That completes the restore. There are many more things we can add to our site, leave a comment if you have suggestions!

Comment on this article (sign in first or confirm by name and email below)