Data can and will get lost. In this tutorial we will backup and restore the data in our Django / Wagtail site.
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',
DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'
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
Backing up the database of your site as well as all media files is now as easy as:
python3 manage.py dbbackup
python3 manage.py 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@165.22.199.4:/home/usr_pet/backups/\* .
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:
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).psql
prompt: CREATE DATABASE db_pet_backup OWNER usr_pet;
mkdir pet_backup && cd pet_backup
python3 -m venv env && source env/bin/activate
git clone https://github.com/prijme/pet.git
pip3 install -r requirements.txt
local.py
settings file with all necessary passwords (or copy it from the server); make sure the database name is db_pet_backup
python3 manage.py 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 manage.py dbrestore
python3 manage.py 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)