Script to make a WordPress Backup to s3 Compatible Storage

How to backup a WordPress website and store it offsite on S3-compatible storage using s3cmd on a Linux machine or Cloudpanel server.

Many providers offer S3 Compatible Storage, and DigitalOcean offers Spaces. In this example, I used Linode or Akamai Object Storage. See a list of providers or WordPress Hosting I recommend.

Prerequisites

  • Linode Object Storage or other s3-compatible storage
  • s3cmd
  • Linux server (I used a Cloudpanel Server)

Backup WordPress to S3

The script will dump the database and create 5 compressed files, to separate the backups into smaller files. The uploads directory is mostly the largest folder and for some older WordPress installations or WooCommerce websites, this folder increased over time.

  • database-name.sql.gz
  • wp_files.tar.bz2 (WordPress directory without the wp-content directory)
  • others.tar.bz2 (the wp-content directory without, the plugins, themes, uploads, and cache directories)
  • uploads.tar.bz2
  • themes.tar.bz2
  • plugins.tar.bz2

This method also allows you to retrieve and upload individual files.

The backup files will be stored in a folder named after the year-month-day (YYYY-MM-DD) the backup was made.

The backup will be sent to your S3 Bucket and we will use a lifecycle_policy.xml file to delete files older than a certain number of days. In the script, you can (un)comment the commands to activate or deactivate this feature.

Crontab

You can use a cron to schedule the script to run daily at 02:00 a.m.:

0 2 * * * /home/[website-path]/scripts/backup-s3.sh 2>&1

Install s3cmd

If s3cmd is not installed on the server, we install and configure s3cmd, the Open Source software that will be required to upload files to the cloud via the following command(s):

$ sudo apt update
$ sudo apt install python3-pip -y
$ sudo pip install s3cmd

To start the s3cmd wizard after installing it run the command:

$ s3cmd --configure

To check the s3cmd configuration you can run the following command:

$ s3cmd --dump-config

The script to backup WordPress to S3 Compatible Storage

Lifecycle Policy

The lifecycle policy is an XML file with instructions to delete files older than a certain number of days from the S3 Bucket. You need to upload this file using s3cmd and I already included the commands to upload the file or to deactivate the lifecycle policy.

Here is an example of the XML lifecycle policy file: lifecycle_policy.xml

Conclusion

Make WordPress backups and store them offsite on inexpensive S3 Compatible Storage using a Bash script. Most providers offer S3-compatible storage for $5 per month with around 250GB of storage, they will charge you a small fee for each additional GB of storage or data traffic.

It is a good practice to store your backups outside your server. It is still recommended to make server backups and or snapshots.

I recommend DigitalOcean Spaces or Linode Akamai, you do not have to use the S3-compatible storage of your provider, you can choose to store your backups with another provider.

Leave a Comment