Bash scripts: WordPress Backup

WordPress backup is one of the most important tasks for a WordPress site administrator for obvious reasons. After many years of struggling with a good concise backup method that was guaranteed to work every time, I decided to design my own method using a Bash script. The script presented in this post guarantees a way to reproducibility backup a WordPress site and it’s accompanying database.

This post is part of the Bash scripts for fun and leisure series.

The method described here assumes the following is true:

  • You have sudo on the server.
  • You have the proper access and permissions to read the document root ( /var/www/html ) of the WordPress installation.
  • You can execute tar and gzip.
  • You can create a file in the working backup directory
    ( ~/backup ) and write to it.
  • You can change file permissions on the file you just created.
  • You have appropriate permission to dump the appropriate MySQL WordPress tables to an sql file in the working backup directory.

Finally it assumes that you have the correct database account password.

The Script

The script for performing a full WordPress backup is as follows:

#! /bin/bash
# DO NOT LEAK THIS SCRIPT
# IT HAS CRITICAL DATABASE DATA!

# Configure variables.
_sn=$(date +"%Y%m%dT%H%M%S")
_default="\e[39m"
_red="\e[31m"
_green="\e[32m"
_magenta="\e[35m"
_backup_owner=<the account that owns the backups>
_db_user=<the database username>
_db_pwd=<the database password, enclosed in double quotes>
_db_name=<the name of your database>
_sitename=<your site name or whatever you want as prefix>

# DO IT FOR SITE!
echo -e $_magenta$_sitename" Website Code Backup"
echo -e "Version: 20221128" $_default
echo -e $_red"Starting website code backup..."$_default
pushd /var/www
sudo tar -cpzf ~/backups/$_sitename'-'$_sn'.tgz' html
cd ~/backups
ls -al $_sitename-$_sn.tgz
echo `sha256sum $_sitename-$_sn.tgz` > $_sitename'-'$_sn'.tgz.asc'
cat $_sitename-$_sn.tgz.asc
echo -e $_red"Protecting code backup files..."$_default
sudo chown $_backup_owner:$_backup_owner $_sitename-$_sn.*
chmod 400 $_sitename-$_sn.*
echo -e $_green"Done backing up "$_sitename" code!"$_default

# DO IT FOR DATABASE!
echo -e $_magenta$_sitename" Website Database Backup"
echo -e "Version: 20221128"$_default
echo -e $_red"Starting website database backup..."$_default
mysqldump -u $_db_user -p$_db_pwd $_db_name > ~/backups/$_sitename'_db-'$_sn'.sql'
gzip $_sitename'_db-'$_sn'.sql'
ls -al $_sitename'_db-'$_sn'.sql.gz'
sha256sum $_sitename'_db-'$_sn'.sql.gz' > $_sitename'_db-'$_sn'.sql.gz.asc'
cat $_sitename'_db-'$_sn'.sql.gz.asc'
echo -e $_red"Protecting database backup files..."$_default
sudo chown $_backup_owner:$_backup_owner $_sitename'_db-'$_sn'.sql.'*
chmod 400 $_sitename'_db-'$_sn'.sql.'*
ls -al *$_sn*
popd
echo -e $_green"Done backing up "$_sitename" database!"$_default

The WordPress backup script outputs four files. There is a site code backup file and a site database backup file. Additionally, there are sha256 checksum files for both with the extension “.asc”.

After exporting from your server, you can verify the accuracy of the backup files by executing the following terminal command.

sha256sum -c <[path to files/]backup file name>.asc

The script has been verified on Ubuntu 22.04 LTS running on a T2 small AWS EC2 instance.

Caveats

In conjunction with Bash Scripts: WordPress Restore, you have a tool set that can restore your site from a server crash where there is data loss on the server. However, these scripts only work if you do your due diligence as a site administrator. You must perform WordPress backup of your site regularly in addition to exporting that backup off of the server. How often is regularly enough? That depends on how much data you can afford to loose… 😉