Coding Microsoft

A Windows batch file to do a simple backup of a WordPress site

I have a production WordPress blog site that is hosted on IIS & Windows Server 2016. To do regular backups of the web folder and the MySQL database, I use the super-useful WordPress Plugin – UpdraftPlus. It creates backup files and uploads them automatically to Google Drive/One Drive/AWS/DropBox & more.

Once in a year or so, I do a manual backup, which involves me (manually) selecting the folders, then running 7-Zip to create the backup files. Every time I do this, I have to remember to exclude the UpdraftPlus temporary folder, otherwise, the resulting backup file will be ginormous as they will be including the huge UpdraftPlus backup files. Today, I set out to solve this by writing a Windows Batch File. Yes, not a fashionable BASH script, but a humble .bat file that was available from the MS-DOS days.

The first step was to find the list of command-line parameters for 7-Zip and understand their individual usage – for creating a new zip file ( a ), adding folders, excluding folders ( -x! ),  specifying the compression type (I wanted zip, so it was -tzip). After a few trial and error runs, I figured the few ones I needed to know. Remember, the folder you are specifying with the -x! parameter, has to be a relative path (just wp-content\updraft) to the added files and not be an absolute path (so no c:\inetpub\wwwroot\wp-content\updraft). The next step was figuring out a way to generate today’s date in a fixed format irrespective of the windows locale of the system, I found a handy script online, which I tweaked to create setcurrentdate.bat, given below:

#setcurrentdate.bat - generates today's date in a fixed YYDDMM format irrespective of the windows locale set#
echo off
FOR /f %%a in ('WMIC OS GET LocalDateTime ^| find "."') DO set DTS=%%a
set CUR_DATE=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%

Below is the actual batch file “www-backup.bat“, remember you need to use the CALL command to invoke the SetCurrentDate.BAT file – if you use just the filename without the CALL command, the control gets transferred to the called batch file, and it never comes back to the next line in this file.

echo off
call SetCurrentDate.bat
echo 1.Backup of all files in C:\inetpub\wwwroot\ 
echo 2.Excluding C:\inetpub\wwwroot\wp-content\updraft 
echo 3.Backup location: "C:\Backup\

rem 'C:\Program Files\7-Zip\7z.exe' a -tzip -mx0 $7ZipArchiveFile $SourceFolders -x!ExclusionsFolder-from-the-basefolder

"C:\Program Files\7-Zip\7z.exe" a -tzip -mx0 "C:\Backup\" "C:\inetpub\wwwroot\" "-x!wp-content\updraft"

Continuing, I wrote another batch file (MySQL-backup.BAT), to back up the data folder of MySQL, which involves stopping the service before the backup and starting it after. Yes, this is a crude method, but as I will be running this backup say once in a year, the downtime of a minute or so, was not a problem in my case.

echo off
call SetCurrentDate.bat
echo 1.Backup of all files in C:\MySQL\MySQL Server 5.7\
echo 2.Backup location: "C:\Backup\

net stop MySQL57

"C:\Program Files\7-Zip\7z.exe" a -tzip -mx0 "C:\Backup\" "C:\MySQL\MySQL Server 5.7\"

net start MySQL57 

If this picked your interest to learn Windows Batch files for doing handy tasks, here are some references to learn more, all are free!

  1. Windows Central’s introduction to the world of Batch files on Windows 10.
  2. Wikibooks’ book on Windows Batch Scripting.
  3. Microsoft’s official comprehensive documentation on the list of all commands.

P.S.: I have used 7-zip in this sample as that’s what I have been using for many years now. Windows 10 version 1803 and above have the inbuilt command-line tool ‘tar‘ for managing zip files, interesting there is ‘curl’ also available for downloading files.