WordPress backups with restic

Backing up large sites with WordPress can get complicated. If this is your case, you may be interested in making a copy with restic.

This tutorial has been created on a Clouding.io VPS. You can create your own VPS from 3€/month.

In addition, you have the possibility to create your VPS with the WordPress image in one click.

PARTNERSHIP

Objectives and requirements

The objective of using this system is to create backups with a quick access, but from console (CLI). Keep in mind that this tool uses a snapshot system, so you will not be able to access each file in the place of data hosting.

In cases of having a large WordPress site, sometimes it is problematic to use a plugin, or the backups of a Plesk or cPanel type panel, so we will have to make the copies more manually.

In addition, this system can also server as a secondary copy to a system that is already established.

This tutorial will be tested on:

  • Ubuntu 22
  • WordPress
  • Wasabi (external storage)

Installing restic

The first thing we have to do is install the tool itself, restic.

apt-get -y install restic

We can also add the self-completing system.

restic generate --bash-completion /etc/bash_completion.d/restic

Setting up a storage

There are different types of external storage to restore. In this case we are going to do the test with Wasabi, a compatible storage system-S3.

As in any system of this type, we will need 4 data:

  • Access Key (example: 3Q7WB7Y98VHMWN4XEVEB)
  • Secret Key (example: ySXTLpQnqanfHLHM9RA6VXb3SjRaFG22ryuhRvGc)
  • Repository (example: eu-west-2)
  • Bucket name (example: vps-1-2-3-4)

In addition, each restic installation has a password with which it encrypts the data on the remote site, and that you must save if you want to restore to another machine.

With this, we can create a file in which to include all this data:

cat > /root/.restic-env << EOF
export AWS_ACCESS_KEY_ID=3Q7WB7Y98VHMWN4XEVEB
export AWS_SECRET_ACCESS_KEY=ySXTLpQnqanfHLHM9RA6VXb3SjRaFG22ryuhRvGc
export RESTIC_REPOSITORY="s3:s3.eu-west-2.wasabisys.com/vps-1-2-3-4"
export RESTIC_PASSWORD="A326AVTjhgdkmA2TukB2J3UcmbuJ2cT98YMp"
EOF

First test

Now that we have the tool and the configuration, we can do a first test of operation.

The first thing we will do is load the configuration.

source /root/.restic-env

Afterwards, we will check that it has been loaded. When you run it, you will need to display the storage path. If nothing appears, it is that the configuration has not been loaded correctly.

echo $RESTIC_REPOSITORY

We will run the tool. At this time it makes checks to connect to the remote storage.

restic init

And we can list the available snapshots. The first time it should show us an empty list.

restic snapshots

Backup script

Since the goal is to automate backups, let’s create a script in Bash that does all the work for us.

The script could be similar to the following (you have to adapt the routes, names and so on).

if [ ! $RESTIC_REPOSITORY ]
then
  source /root/.restic-env
  echo $RESTIC_REPOSITORY
  restic init
fi

restic backup /etc/

mkdir -p /backups-db/
mysqldump --quick --skip-lock-tables --single-transaction --quick --verbose example-com-db | zip > /backups-db/example-com-db-$(date +%F-%T).sql
restic backup /backups-db/
rm -rf /backups-db/

restic backup /webs/example.com/

Loading the configuration

The first of the blocks is the validation and loading of the configuration.

if [ ! $RESTIC_REPOSITORY ]
then
  source /root/.restic-env
  echo $RESTIC_REPOSITORY
  restic init
fi

Here, basically what we validate is whether the configuration is loaded. If it is, go ahead. If it is not, it charges it and connects to the remote storage system.

Copying configurations

The first copy we will make is that of the general configurations of the machine.

restic backup /etc/

Copy of the database

The second element to which we are going to make the copy is to the database. In this case we will first make a MySQLdump and that generated file will be the one we use.

mkdir -p /backups-db/
mysqldump --quick --skip-lock-tables --single-transaction --quick --verbose example-com-db | zip > /backups-db/example-com-db-$(date +%F-%T).sql
restic backup /backups-db/
rm -rf /backups-db/

Copy of the files

Finally we will copy the files of the WordPress itself.

restic backup /webs/example.com/

List snapshots

Now that we have several copies made, we can see the list of what could be recovered.

restic snapshots

Something that will give us back something like:

repository cgms3tqn opened successfully, password is correct
ID        Time                 Host                Tags        Paths
--------------------------------------------------------------------
af9qtv5m  2022-06-13 18:24:04  example.com                     /etc
26kkqczj  2022-06-13 18:24:18  example.com                     /backups-db
44z2cta5  2022-06-13 18:24:29  example.com                     /webs/example.com
4tdrn8zx  2022-06-13 18:53:42  example.com                     /etc
v3y8d9ee  2022-06-13 18:53:46  example.com                     /backups-db
4j9qs4m2  2022-06-13 18:53:50  example.com                     /webs/example.com

Recover a snapshot

If we want to recover one of the copies, we can do it on the existing files, or we can recover it in a temporary folder to look for the files we need.

restic restore 26kkqczj --target /tmp/restore

If we access the folder /tmp/restore, we can find the snapshot restoration.


About this document

This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.

WordPress System Administration Services

Do you have a high traffic WordPress website? Are you an Agency with servers with cPanel, Plesk or other panel on which you maintain WordPress for your clients?

If so, and you are interested in a professional WordPress infrastructure maintenance and performance improvement service for your or your clients’ websites, contact me.