Backing Up My WordPress Site

Bookmark and Share

Thought I would share a quick post on how I protect this site. For those who don’t know me I started my career working in the backup industry. As such I am a firm believer in the saying “if you don’t have 3 copies of it then it doesn’t exist.” As such I try to keep several copies of wondernerd.net floating around.  How many can vary at any given time but needless to say its a lot.

I’ll start with the first level of protection. Basic backups of my wordpress site. How do it do it? I use the UpdraftPlus (http://updraftplus.com/) to backup my wordpress site. One of my main reasons for choosing this is that UpdraftPlus also gets clean dumps of the sites databases. You can configure it to send a copy to a cloud provider if you want. There is a whole list of them on the updraftplus website. I’m a bit more paranoid than just sending them to a cloud site.

Once a backup completes, I then have a post process task running on my home computer that logs into my website and pulls down the backup to my home network and distributes copies to various locations. I make the connection and get copies of the files with PSCP (the same folks who make putty).

A typical backup methodology might be the following:

  • Keep the latest 3 backups for 3 days (or cycles)
  • Keep the end of week backups for 2 weeks (or cycles)
  • Keep end of month backups indefinitely

I’ve illustrated it in this picture.

Website Backup Diagram

How typical backups might work. 

  1. Backups are run on the website.
  2. Those backups are then pulled to the local system as daily backups.
  3. A copy of the daily backup from the start of the week is moved to the weekly backups location
  4. At the begining of the month a copy of the backup is moved to the First of Month Backup location.

 

 

Here is the basic script that I use to do this:


setlocal enabledelayedexpansion

C:
cd "C:\Website_Backup\"

set MyYear=%Date:~10,4%
set MyMonth=%Date:~4,2%
set MyDay=%Date:~7,2%
set DOW=%Date:~0,3%

REM move the oldest to a temp directory for deletion 
ren "E:\Wondernerd_netBackup\5" "temp"

REM Keep For x days
set KeepFor=3

Set /A PrimeRead=%KeepFor%-1
REM Day counter
set NextDay=%KeepFor%
set /A PrevDay=%KeepFor%-1

FOR /l %%x IN (1,1,%PrimeRead%) DO (
  ren "C:\Website_Backup\!PrevDay!" "!NextDay!"
  set /A PrevDay=!PrevDay!-1
  set /A NextDay=!NextDay!-1
)

md "E:\Wondernerd_netBackup\1"

md "C:\Website_Backup\1"

pscp.exe -v -r -C -batch -sftp -pw PASSWORD USERNAME@YOURWEBSITE 
      .COM:/home/WEBSITE/wp-content/updraft/* C:\Website_Backup\1 >> C: \Website_Backup\1\quicklog.txt

rmdir /Q /S "C:\Website_Backup\temp\"



REM Snag monthly backups and keep indefinitely, Grabs the first day of the month

if %MyDay%==01 (
  md "C:\Website_Backup\MonthEnd\%MyYear%-%MyMonth%-%MyDay%"
)

if %MyDay%==01 (
  xcopy /s "C:\Website_Backup\1" "C:\Website_Backup\MonthEnd\%MyYear%-%MyMonth%-%MyDay%"
)

Z:
set EOW=Sun
REM Sync a copy every Sunday to third storage area 
if %DOW%==%EOW% (
  rmdir /Q /S "Z:\Website_Backup\Temp"
)

if %DOW%==%EOW% (
  ren "Z:\Website_Backup\Second" "Temp"
)

if %DOW%==%EOW% (
  ren "Z:\Website_Backup\First" "Second"
)

if %DOW%==%EOW% (
  md "Z:\Website_Backup\First"
)
if %DOW%==%EOW% (
  xcopy /s "C:\Website_Backup\1" "Z:\Website_Backup\First"
)

if %DOW%==%EOW% (
  rmdir /Q /S "Z:\Website_Backup\Temp"
)

 

This is how the above bat script works.

  • First we make sure to change to the correct base directory on my local system.
  • Next we break the date out into a useable form we can use for keeping backups for a given time.
  • Next we shuffle our directories so we can age off the oldest backup at the end of the process. This script keeps 3 backups. You can change this by changing the KeepFor variable and creating the needed folders.
  • Now we create our latest directory where the backups will be placed.
  • Once we have that we are ready to go get the files. For this task I am using PSCP to securely download the files from my web server.
    • Note that in the above script it appears as three lines with a “↵” at the end of the first two lines. The pscp command is actually a single line.
    • The parameters should be fairly obvious in the PSCP command. At least the ones in capital letters should be.
    • I also output the download process to a file so I can have a quick list of files backed up along with any errors that occur during the backup
    • [warning] Your password is stored in clear text make sure to take appropriate precautions with this script.[/warning]
  • Once the files are downloaded I then get rid of the temp directory that contains the oldest backup in the set.
  • Now we get to the long term protection. If its the first of the month (or what ever day you want to set it for) we copy the latest backup to a date identified long term backup directory.
  • The last 6 IF statements copy a weekly copy of the backup to a designated folder if its a Sunday. I would shorten it down but if statements in bat scripts don’t do well with multiple lines of code.
  • I put the above bat script in a directory with PSCP and all of the other directories (except the temp directories) per-created.
  • Now all that needs done is to schedule the bat script to run.

And that’s it. I now have lots of copies of my website with relatively little effort and since its more than 3 copies… it really does exist!

If you’ve made it this far you are probably thinking this is great how do I implement this. That’s fairly simple.

  1. Install and configure UpdraftPlus for your wordpress site.
  2. Create your backup directory.
  3. In the backup directory place:
    1. A copy of PSCP (unless you’ve changed the path in the script)
    2. All of the backup sub directories (1-3, First, Second) the rest should be automatically created. NOTE: If you plan to place weekly backups on a different storage location you will need to create a separate directory for First & Second directories.
  4. Create a bat file
    1. Copy the above code into a text document.
    2. Make any changes you want such as directories that you are saving to and how many backup sets you want to keep on your local system.
    3. Save the file with a .bat extension into the backup directory.
  5. Create a Windows schedule to run the bat file created in step 2.

That’s all there is to it. You now have a fairly complex backup system for your website. And if you’re like me you can place copies across several different sets of storage so you are protected locally and remotely.

If you have any feedback to share please do. It would be great to continue to improve this script. Hopefully this has been helpful.

 

Permanent link to this article: https://www.wondernerd.net/backing-up-my-wordpress-site/