Backup and Restore a WagtailCMS website

May 26, 2023

Essential Guide: Backing up and Restoring Your Wagtail Site for Data Security and Peace of Mind.

Introduction

Backups are an essential part of maintaining any website, including those built with Wagtail. Whether it's due to a server crash, a hacking incident, or accidental deletion, losing your website data can be devastating. In this blog post, I will walk you through the steps of manually creating a backup and restoring a Wagtail site. By following these steps, you can safeguard your website data and quickly restore it in the event of a disaster.

Creating a Backup

There are two main types of backups that you need to create for your Wagtail site: a file system backup and a database backup. The file system backup will contain all the files that make up your website, while the database backup will contain all the data stored in your database.

Database Backup

To create a database backup, follow these steps:

  1. SSH into your server as a user with sudo privileges.
  2. (Optional) Navigate to your websites root directory. This is the directory where your Wagtail projects manage.py file is stored. For example, if you followed my guide to Deploying Wagtail your root directory would be at /opt/wagtail/<projectname> This is where I like to store the database backup so that it will be included in the file system backup.
  3. My server uses MariaDB so that's what I'll be documenting here. For other databases it's easy enough to do a search for creating a database dump. Run the following commands for switching to the root user, creating an SQL dump, then exiting back to your normal user.

    $ sudo su [sudo] password for user: $ mysqldump -u root -p wagtail > db_backup.sql Enter password: $ exit

  4. Move the file to a secure location. You can use commands like scp or rsync to copy the file to your local machine or to a remote backup server.

File System Backup

  1. SSH into your server as a user with sudo privileges.
  2. Use the tar command to compress and archive all the files in the root directory into a single file. For example, to create a backup file called 'example.com.tar.gz' and store it in your users home directory use the following command

    $ tar -czvf ~/example.com.tar.gz .

  3. Move the file to a secure location. You can use commands like scp or rsync to copy the file to your local machine or to a remote backup server.

At this point you should now have both a file system backup and a database backup stored in a secured location in case of emergencies. In the next section, I will walk you through the steps of restoring a backup.

Restoring a Backup

What good is a backup if you don't know how to restore? Whether you're restoring after an emergency or you're migrating your site to a new host, these steps will walk you through getting your site back up and running.

I won't be going over setting up your web server, proxy software, or firewall. If you're not sure how to setup your host server have a look at my guide for Deploying Wagtail on CentOS 8. Instead of cloning a git repository follow the instructions on the next section to restore the file system. Instead of setting up the DB, follow the instructions in the Database Restore section.

File System Restore

  1. SSH into your new server as a user with sudo privileges.
  2. Copy the backup file from your backup location. If you're using a attachable volume or an S3 equivalent, go ahead and mount that device to your new server, if you haven't already, and change into that directory using the "cd" command.
  3. If you followed my deployment guide, linked above, change into your wagtail user. sudo su - wagtail
  4. Create the folder where your project files will live. Then exit back to your user with sudo privileges (or switch to the tmux terminal with that user). mkdir /opt/wagtail/<projectname> exit
  5. Extract the files from the backup tar.gz file. `sudo tar -xzvf example.com.tar.gz -C /opt/wagtail//'

Now that the files are in place, we need to get the database setup before we continue.

Database Restore

  1. As a user with sudo privileges switch to the root user. sudo su
  2. Change into the directory with your SQL backup file. If you're following this guide it should be in your projects root folder. cd /opt/wagtail/<projectname>
  3. To restore the database we need to first create it. And we might as well create the user and grant it all privileges while we're at it.

    $ mysql -u root -p Enter password: <enter> > CREATE DATABASE wagtail CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; > CREATE USER 'wagtaildb'@'localhost' IDENTIFIED BY '<db user password>'; > GRANT ALL PRIVILEGES ON wagtail.* to wagtaildb@localhost; > FLUSH PRIVILEGES; > exit;

  4. Now, restore the contents of the SQL file to your newly created wagtail database. mysql -u root -p wagtail < db_backup.sql

Finishing Up

Best Practices for Backups and Restorations

To ensure the effectiveness and reliability of your backups and restorations for a Wagtail site, consider the following best practices:

By following these best practices, you can enhance the reliability, security, and effectiveness of your backup and restoration procedures for your Wagtail site.

Conclusion

In conclusion, implementing regular backups, testing them, and storing them off-site are crucial for ensuring the safety and integrity of your Wagtail site. By following best practices such as encryption, documentation, and monitoring, you can enhance the reliability and security of your backup and restoration processes. Additionally, keeping software and components up to date is essential for maintaining compatibility and optimal performance.

Remember, preparedness and practice are key to successfully safeguarding your website data. With these practices in place, you can confidently navigate the backup and restoration process and protect your valuable Wagtail site.

Additional Resources

For more information and resources on Wagtail, backups, and website deployment, please visit the following:

Stay informed, explore, and keep your Wagtail website up and running smoothly!

Return to blog index