Backing up WordPress can be a pain on the windows platform. Some plug-ins work, some don’t, maybe you need a mixture of this or that. There are commercial options, some a great, some not so much. After trying various options, I broke down and decided to create a process to backup WordPress on Windows. This process uses a batch file and can use the Windows Scheduler to automatically execute whenever you want.
At the end of the day, all you want is your WordPress content and database backed up.
Lets start with MySQL.
There are lots of plugins that will backup your database. If that’s what you want, check out this handy post: Careful Crash. This user used a service called Blog Vault. I checked it out and it seems extremely interesting. Pricing starts at ~$10 a month. It backed up my WordPress site and even let me do a test restore.
So if you want to shell out some cash, maybe that’s a good way to go.
I, however, already pay for backup. Crash Plan. As a side bar – if you aren’t backing up, or are only backing up locally or to a USB hard drive – your data is at risk. With Crash Plan, you can backup 10 PCs (Linux, Mac, or Windows) to the cloud with unlimited storage for $120 a year. Just sign up for the Crashplan+ Family Plan.
Anyway back to this post.
So I started searching for a way to make sure I have a full backup of MySQL AND my WordPress blog folder.
I found this very handy script by Red Olive Design, which inspired me to make this. This fine script does some nice work and will even FTP your files.
Since I have backup software already running, I chose to simply backup the folder where this archive lands.
For this script, you supply a few paths, the login, and how many days you wish to keep backups. Additionally, on the 1st of every month, this script creates a monthly archive.
It requires 7Zip, which you can get here.
Have any tips? Ideas? Found a problem? Suggestions? Wants? Just leave a comment.
In the pipeline to enhance this:
- A flag to FTP the file elsewhere
- A flag to use 7Z compression
- A flag to include IIS configuration
- Parse the results and email on error only
- Send a txt if there’s a major issue
- A restore script
- A VBScript version of both the backup and restore script
- And an EXE version of these
The biggest risk with this script is passwords are inside a batch file. So anyone with access to the script can see the password.
Lastly, of course – use at your own risk. I made this for me. Just copy and save this as a CMD file.
:: Set Parameters used by script
:: MySQl DB user
:: MySQl DB users password
:: Max days for archive
:: Working Folders
:: Must Include Trailing Slash excpt for thebackupfolder
:: Where is the backup folder
:: Path to folder where mysqldump.exe is
set mySqlBinFolder=C:\Program Files\MySQL\MySQL Server 5.1\bin\
:: Path to where the MySQL data files are
set mySqlDataFolder=C:\ProgramData\MySQL\MySQL Server 5.1\data\
:: Path to your wordpress site
:: Path to the folder where 7Zip is installed. This script uses 7z.exe
set Pathto7zaexe=C:\Program Files\7-Zip\
:: End User Parameters
:: BEGIN VARIABLE SETUP
:: Convert to 8.3
FOR /F "delims=" %%I in ('echo %backupFolder%') do set backupFolder83=%backupFolder83%%%~sI
FOR /F "delims=" %%I in ('echo %mySqlBinFolder%') do set mySqlBinFolder83=%mySqlBinFolder83%%%~sI
FOR /F "delims=" %%I in ('echo %mySqlDataFolder%') do set mySqlDataFolder83=%mySqlDataFolder83%%%~sI
FOR /F "delims=" %%I in ('echo %PathToWordpress%') do set PathToWordpress83=%PathToWordpress83%%%~sI
FOR /F "delims=" %%I in ('echo %Pathto7zaexe%') do set Pathto7zaexe83=%Pathto7zaexe83%%%~sI
:: Working Variables
IF %TIME:~0,2% LSS 10 (
) ELSE (
IF %TIME:~3,2% LSS 10 (
) ELSE (
@echo Verifying if %archiveFolder83% exists and if not create it
if NOT EXIST %archiveFolder83%\nul (
@echo archive folder not found, creating %archiveFolder83%
:: Begin Backup
@echo Starting ...
:: Switch to MYSQL Data folder
@echo Pass each folder name in %mySqlDataFolder83% to mysqldump.exe and output an individual .sql file for each database
FOR /D %%F IN (*) DO (
%MySqlDump% --user=%MYSQLUSER% --password=%MYSQLPASS% --databases --log-error="%backupFolder83%MySQLDumpError.txt" %%F > "%backupFolder83%%%F.%backupdate%.sql"
@echo Delete the file %DBBackupZip% if it exists
if exist %DBBackupZip% (del %DBBackupZip%)
@echo Zip all files ending in .sql in the folder %backupFolder83% to the file %DBBackupZip%
%SevenZ% a -tzip %DBBackupZip% %backupFolder83%*.sql
@echo Delete all the files ending in .sql only
@echo Deleting zip files older than %MaxDays% days
forfiles /p %backupFolder83:~0,-1% /M *.zip /D -%MaxDays% /C "cmd /c del @Path /Q" 2>&1 | find /v /i "ERROR: No files found"
@echo Create a new archive of the wordpress folder
%SevenZ% a -tzip "%WPBackupZip%" "%PathToWordpress83%"
@echo Ensure there is no complete backup for this day in the folder:
if EXIST %CompleteBackupZip% (
@echo merge DB and WP archives into single archive with zero compression
%SevenZ% a -tzip -mx0 "%CompleteBackupZip%" "%WPBackupZip%" "%DBBackupZip%"
@echo create a monthly archive
if %day% EQU 09 (
@echo First of the month, creating archive
copy %CompleteBackupZip% %ArchiveBackupZip%
@echo Removing temp archives
forfiles /p %backupFolder83:~0,-1% /M Full*.bk.zip /C "cmd /c del @Path /Q" 2>&1 | find /v /i "ERROR: No files found"
@echo returning to starting folder