WordOps is a great way to manage WordPress or any site using NGINX. All of my client sites are managed using WordOps and it’s one of the best tools to manage servers.
Backups are a huge part of managing servers and it’s always good to have multiple ways of restoring a site/server.
In this guide, you will create a script that will run to backup a specified site using WordOps.
When you’re finished, you’ll have a compressed file of the root directory excluding the WordPress install files and a gz of the database.
Before you begin this guide you’ll need the following:
- WordOps running
- A non-root user with sudo privileges
Step 1 — Create the Backup Script
First create a directory to store the script such as
wo-backup.sh using nano or your preferred editor.
Paste the following:
#! /bin/bash # Example usage: /scripts/wo-backup.sh yourdomain.com SITE=$1 DATEFORM=$(date -d "today" +"%Y%m%d%H%M") BACKUPPATH="/tmp/backups/$DATEFORM" SITESBASE="/var/www/$SITE/htdocs" if test -z "$SITE"; then echo "ERROR: no domain informed!" exit 1 fi if [ ! -e "/var/www/$SITE/wp-config.php" ]; then echo "ERROR: $SITE does not appear to be a valid WordPress!" exit 1 fi # Make sure the backup folder exists mkdir -p "$BACKUPPATH" cd "$SITESBASE" || exit echo "Backing up files to $BACKUPPATH..." tar -czf "$BACKUPPATH/$DATEFORM-$SITE.tar.gz" --exclude-from="/scripts/exclusions.txt" -C "$SITESBASE" . # Backup WordPress config file cp "/var/www/$SITE/wp-config.php" "$BACKUPPATH" echo "Backing up WordPress database..." wp db export "$BACKUPPATH/$DATEFORM-$SITE".sql \ --single-transaction \ --quick \ --lock-tables=false \ --allow-root \ --skip-themes \ --skip-plugins gzip <"$BACKUPPATH/$DATEFORM-$SITE".sql >"$BACKUPPATH/$DATEFORM-$SITE".sql.gz rm "$BACKUPPATH/$DATEFORM-$SITE".sql exit 0
Ctrl+O Return then Ctrl+X to save.
Next, give it execution permission:
sudo chmod +x /scripts/wo-backup.sh
Finally, create a text file to exclude any files or directories that’s aren’t needed in the backup.
Paste the following:
wp-admin wp-includes wp-content/index.php wp-content/upgrade wp-content/cache wp-content/*.zip wp-content/*.gz wp-content/plugins/index.php wp-content/themes/index.php index.php license.txt readme.html wp-activate.php wp-blog-header.php wp-comments-post.php wp-config-sample.php wp-cron.php wp-links-opml.php wp-load.php wp-login.php wp-mail.php wp-settings.php wp-signup.php wp-trackback.php xmlrpc.php
Now everything should be ready to backup one of your sites.
Step 2 — Use the Backup Script
To use the backup script, run the following:
/scripts/wo-backup.sh yourdomain.com Backing up files to /tmp/backups/yourdomain.com... Backing up WordPress database... Success: Exported to '/tmp/backups/yourdomain.com/202205061919-yourdomain.com.sql'.
Here’s what’s happening when running this script:
- Checks if there’s a WordPress site installed
- Creates a backup path in /var/www/yourdomain.com/backup/
- Backs up files excluding files listed in
- Backs up wp-config.php
- Backs up the database
After the backup, you should be left with 3 files.
- Site files compressed as a tar.gz
- Database compressed as a gz
You can then copy these files using SFTP or rsync to keep as a backup.
Updated the script to backup files into your sites WO directory instead of a tmp directory (example:
I’ve pushed all this code up to this repository if it’s easier.