Backup and Restore a WagtailCMS website
May 26, 2023
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:
- SSH into your server as a user with sudo privileges.
- (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. -
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
-
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
- SSH into your server as a user with sudo privileges.
-
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 .
-
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
- SSH into your new server as a user with sudo privileges.
- 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.
- If you followed my deployment guide, linked above, change into your wagtail user.
sudo su - wagtail
- 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
- 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
- As a user with sudo privileges switch to the root user.
sudo su
- 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>
-
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;
-
Now, restore the contents of the SQL file to your newly created wagtail database.
mysql -u root -p wagtail < db_backup.sql
Finishing Up
-
Now that your file system and database have been restored, I like to run migrations and collect static to make sure that everything is in place for your site. Switch to your wagtail user and make sure your have the virtual environment activated.
$ sudo su - wagtail
$ source .venv/bin/activate
$ cd <project name>
$ ./manage.py makemigrations
$ ./manage.py migrate
$ ./manage.py collectstatic --no-input
-
Restore or configure your web server (Gunicorn, uWSGI, etc...) and your reverse proxy (Nginx, Apache) and you should be all set.
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:
- Regularly Schedule Backups: Set up a regular backup schedule to ensure that your data is consistently backed up. Depending on the frequency of updates and changes to your website, consider daily, weekly, or monthly backups.
- Test Your Backups: Periodically test your backups by restoring them to a test environment. This step helps verify that your backups are complete and functional, allowing you to identify and address any issues before an actual restoration is needed.
- Store Backups Off-Site: Store your backups in an off-site location, separate from your production server. This practice provides protection against physical damage or server-related issues. Consider using cloud storage, external hard drives, or remote backup services.
- Implement Multiple Backup Storage: Avoid relying on a single backup location or storage device. Use multiple storage options to ensure redundancy and minimize the risk of data loss.
- Encrypt Your Backups: Protect the confidentiality and integrity of your backup data by encrypting it. This step ensures that even if your backups fall into the wrong hands, they will be unreadable without the encryption key.
- Document the Backup and Restoration Process: Maintain clear documentation outlining the step-by-step procedures for creating backups and restoring them. This documentation serves as a reference guide during emergency situations and helps ensure consistency in the process.
- Monitor Backup Success and Errors: Regularly check your backup logs and monitoring tools to verify the success of backups and identify any errors or failures. Promptly address any issues to maintain the reliability of your backup system.
- Keep Software and Components Up to Date: Regularly update and patch the software components of your Wagtail project, including the operating system, database, web server, and backup tools. This practice ensures compatibility, security, and optimal performance during backup and restoration processes.
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:
- Official Wagtail Documentation
- Deploying Wagtail on CentOS8 with MariaDB/Nginx/Gunicorn
Stay informed, explore, and keep your Wagtail website up and running smoothly!