Skip to content

Backup with Windows & WP-CLI

Introduction

This post is a bit of an extension to my first post on wp-cli only this time using batch and wp-cli to deploy and backup WordPress.

Before I get started I will assume you have wp-cli and php installed correctly if not I cover how to install wp-cli in my previous post, as for php there are plenty of tutorials all over the place just google for it.

Let’s dive straight in

I tend to do a lot of backups for my local installs of WordPress and have devised a neat way of packaging them using a combination of Batch and WP-cli.

I use the 7zip cli extension to zip my files and database (sql file) together. you will need to install the 7zip cli in order for the code below to work.

@echo off

:: Set the projects path
SET LOCALROOT="%cd%"

::ASCII ART

echo.
echo --------------------------------------------------------------------------------
echo  ____                     __                        
echo /\  _`\                  /\ \                       
echo \ \ \L\ \     __      ___\ \ \/'\   __  __  _____   
echo  \ \  _ '  /'__`\   /'___\ \ ,    /\ \/\ \/\ '__`\ 
echo   \ \ \L\ \/\ \L\.\_/\ \__/\ \ \\`\\ \ \_\ \ \ \L\ \
echo    \ \____/\ \__/.\_\ \____\\ \_\ \_\ \____/\ \ ,__/
echo     \/___/  \/__/\/_/\/____/ \/_/\/_/\/___/  \ \ \/ 
echo                                               \ \_\ 
echo                                                \/_/  
echo --------------------------------------------------------------------------------
echo Backup Development projects
echo.

:: Set project name to extract
SET PROJECT=%1

:: Set the projects root
SET PROJECTROOT=%LOCALROOT%\%PROJECT%

:: Test if project exists
IF EXIST %PROJECTROOT%\ GOTO ExtractMethod
GOTO FailedMethod

:ExtractMethod

:: navigate to the projects root
cd %PROJECTROOT%

:: export the database
call wp db export "original-database.sql" --path=%PROJECTROOT%

:: Enter the search and Replacement terms
SET /P SEARCH=Enter Search term: 
SET /P REPLACEMENT=Enter Replacement term (no http://): 

:: run the Replacement
call wp search-replace %SEARCH% %REPLACEMENT%

:: export the replaced database
call wp db export "database-backup.sql" --path=%PROJECTROOT%

:: import the original db back into the development server
call wp db import "original-database.sql"

:: remove the original DB
del original-database.sql

:: remove node modules and sass cache
cd wp-content/themes/lt-theme
call rimraf node_modules
rmdir /q /s .sass-cache

:: make a copy of the project into the new temp directory
robocopy %PROJECTROOT% %LOCALROOT%\%PROJECT%_temp /e /NJH /NJS /NDL /nc /ns

:: Clear the screen
cls

:: Set timestamps
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

:: Set backup path
set BACKUPDIR="%USERPROFILE%\Desktop\_backups"

:: Navigate to the Desktop
cd "%USERPROFILE%\Desktop"

:: if a folder doesn't exist for the backups create it
IF NOT EXIST %BACKUPDIR%\ (
	mkdir "_backups"
)

:: Set timestart
set TIMESTART=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

:: cd into the backup directory
cd %BACKUPDIR%

:: if a folder doesn't exist for the current project create it
IF NOT EXIST %BACKUPDIR%\%PROJECT% (
	mkdir %PROJECT%
)

:: set project backup directory
SET PROJECTBACKUPDIR=%BACKUPDIR%\%PROJECT%

:: go to project backup directory
cd %PROJECTBACKUPDIR%

:: zip up the current project with the SQL file
7za a backup-%TIMESTART%.zip %LOCALROOT%/%PROJECT%_temp/*

:: go back to the localroot
cd %LOCALROOT%

:: remove the temp directory
rmdir /q /s %PROJECT%_temp

:: go to the projects directory
cd %PROJECTROOT%

::delete the sql file
del database-backup.sql

:: install node modules
cd wp-content/themes/lt-theme
call npm install

:: return back to the localroot
cd %LOCALROOT%

cls

echo.
echo --------------------------------------------------------------------------------
echo %PROJECT% successfully backed up to 
echo %PROJECTBACKUPDIR%/backup-%TIMESTART%.zip
echo.
echo --------------------------------------------------------------------------------
echo.

:END

Once you have this code sitting in a bat file named backup and it’s referenced in the path you can navigate one folder up from your WordPress install and type backup {FOLDER/PROJECT NAME} tab comes in handy for auto completion here.

The script will ask a few questions such as the current domain in your database and the domain you want to replace it with if you prefer the database backup to be the same as your local version then simply hit enter leaving both questions blank.

Provided everything is correct this will begin the backup process, ziping up the files and database and placing them on your desktop. Don’t forget to move the backup to an off site location after this is complete!

Comments

There are currently no comments on this article. Why don't you add the first?

Have Your Say

Your email address will not be published. Required fields are marked *