WordPress linux backup script to offsite NAS via Rsync

29 Jul 2017 | WordPress linux backup script to offsite NAS via Rsync |

Here’s a simple straightforward bash | shell script that I use for this SITE to easily and routinely backup my Entire  Wordpress Site , all its folders and plugins , and the corresponding database.   Of course there are dozens of WordPress Backup  plugins (such as Backup Buddy;  Updraft Plus; BackWpUp ) that do this sort of thing and much more , but I wanted a simple, single file that I could setup on a CRON to do the basics.  So I customized an existing script into the example below. Here are the main STEPS.

  1. After configuring the folders , and database to backup settings and supplying an Rsync destination the following happens
  2. The scrip uses standard zip command to archive the entire WordPress File, using an UPDATE command if the file exists this speeds up the updating of existing backup files
  3. Then it dumps the entire MYSQL database and combines that with the WordPress zip files. I choose Zip as its easier to work with especially on non-Linux systems.
    1. I kept the filename the same as $DOMAIN.WORDPRESS_LATEST.zip so you always have 1 copy of the most recent version. If you want to deal with multiple versions you can simply alter the name and ADD a $DAY or $WEEK variable that will provide different named versions.
  4. Finally is uses Rsync to send the complete backup ZIP file  to my Qnap or  Synlogy NAS drives. Rsync is the defacto method to backup large files over a network effectively and quickly. For more details on benefits and how  to use Rsync go here.  Setting up Rsync is a bit of work as i did not detail that here, most of it involves generating .SSH keys to automatically login and copy files to the server.

Requirements

  • Shell access with ability to run bash scripts
  • zip installed on your server (most debian servers have this by default otherwise use apt-get install zip)
  • ability to create cron entries (optional but needed for automatically backups)
  • Rsync (optional, but needed to transmit file offsite)

WordPress Backup Script wit Rsync

Here’s the code to the script, you’ll need access to your server’s command line and the ability to create cron entries. BE SURE to change the configuration sections variables to match your server settings.  Also CAUTION with the mySQL security settings, I put the mysqldump login info into the script but its safer to use the standard .my.cnf file with proper security user permissions, that’s how I do it on my site.

#!/bin/bash


# zipup_wp.sh This script creates a compressed backup 
# ZIP archive of the given directory WORDPRESS folder (option to excluding cache folder) 
# and given MySQL database.  Using standard zip (unix utility) to  archive folders and database
# Finally script will  RSYNC backup to an offsite location
#

# Feel free to use this script wherever you want, however you want. 
# Author: Antonio Brandao http://www.abrandao.com July 2017 
# based off script from Konstantin Kovshenin exclusively for Theme.fm in June, 2011
# Set the date format, filename and the directories where your backup files will be placed and which directory will be archived.
SECONDS=0  #  simply to track how long script runs  ,not essential

NOW=$(date +"%Y-%m-%d-%H%M")
TODAY=$(date +"%Y-%m-%d")   #just the data no time

#Domain / Host name , just  a easy way to prefix your files
DOMAIN="your_domain_name.com"   

#FILE="$DOMAIN.$NOW.zip"  # allows you to data stamp the filename 
FILE="$DOMAIN.WORDPRESS_LATEST.zip"   #keeps the updated file the same name

# Destination TARGET folder where  zip archive will be placed
BACKUP_DIR="/home/backup"

#Source folder (WORDPRESS  top directory) , typically your html root but check server settings
WWW_DIR="/var/www/public_html/"

# Folders to  exclude from acrhive typically CAChe files or other temporary files.
EXCLUDE="/var/www/public_html/wp-content/cache/*"

#Rsync information and login
RSYNC_HOST="www.your_rsync_server.com"   # website or server WHERE THE RSYNC host (RECEIVING) server lives
RSYNC_USER="admin"  # name of the RSync user needed to login using SSH keys
RSYNC_PORT=22  # Default port through which RSYNC is setup  using SSH keys port 22 is most common

# MySQL database credentials 
#  ** SECURITY ISSUES ** Placing your credentials here is a BIG security vulnerabiliity
#  consider using  .my.cnf  files  with proper permisssions 
# Replace below to match your 

DB_USER="user_name"
DB_PASS="pwd"
DB_NAME="database_name"
DB_FILE="$DOMAIN.$TODAY.sql"

# Create the archive and the MySQL dump
if [ -f  $BACKUP_DIR/$FILE ]; then
   echo "File $FILE exists. UPDATING ONLY [$SECONDS s].."
   zip -9 -u  $BACKUP_DIR/$FILE   $WWW_DIR -x "$EXCLUDE"
else
   echo "INITIAL backup up WORDPRESS folder [ $WWW_DIR] STARTED .... (be patient) ...  "
  zip -9  -rq  $BACKUP_DIR/$FILE  $WWW_DIR  -x "$EXCLUDE"
   echo "INITIAL backup up WORDPRESS folder [$WWW_DIR]  COMPLETED in [$SECONDS s] "
fi


# Create the MYSQL dump of your database
echo "CREATING  MYSQL dump of database  [$DB_FILE]  [$SECONDS s]"
mysqldump -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE

# Append the dump to the archive, remove the dump and compress the whole archive.
echo "Combining DATABASE + WORDPRESS Archive into final file  [$FILE]  [elpased $SECONDS s]"
zip -rv $BACKUP_DIR/$FILE $BACKUP_DIR/$DB_FILE

echo "REMOVING  database backup $BACKUP_DIR/$DB_FILE"
rm $BACKUP_DIR/$DB_FILE

# RSYNC to offsite , requires that  SSH keys for authentication  have been setup ahead of time.
# Refer to https://www.debian.org/devel/passwordlessssh on how to do this.
echo "STARTING OFFSITE RSyNC BACKUP  ..  [elapsed $SECONDS s]"
rsync -avz -e "ssh -p $RSYNC_PORT " --progress $BACKUP_DIR/$FILE  $RSYNC_USER@$RSYNC_HOST:/share/Download/

#echo "REMOVING   $BACKUP_DIR/$FILE compressed zip file"
# rm   $BACKUP_DIR/$FILE   #uncomment this if you want to re-create the zip file each time instead o fjust updating it

echo "BACKUP FINISHED in  $SECONDS seconds..."

Running it as a Cron/Scheduled Task

To make this WordPress backup automatic , simply create a CRON entry similar to the one below. If your not sure how to use Cron, check out this tutorial.

# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
0 5,20 * * *  /home/backup/zipup_wp.sh   #backups up at 5am and 8pm daily.

That’s it if all goes well, then you should have a daily backup on your Remote offsite server every day.

5 (100%) 1 vote

3 thoughts on “WordPress linux backup script to offsite NAS via Rsync

  1. Reply Jessica Noth Jul 29,2017 9:56 am

    Thanks , exactly what I was looking for, simple and easy to understand.

  2. Reply Ashish Patel Jul 29,2017 10:12 am

    Awesome script, thanks , but how can I get this to backup to run only 1x daily.

  3. Reply Clive Dhal Jul 30,2017 9:21 pm

    How can I change it so I get multiple versions , I didn;t understand why was ment by DAY or Week?

Leave a Reply