How to Batch Zip Backup + Install WordPress Plugins with WP-CLI

During WordPress performance troubleshooting or malware investigation, you will often want to disable all of the plugins at some stage of your process. You may also want to back up all of the WordPress plugins as separate zip files for safekeeping just in case you lose track during the troubleshooting process.

Re-installing all of the plugins from their zip files manually would be a very tedious task so in this post I show you how to automate the process. First we will make a zip of each WordPress plugin and place them in a temporary folder. To complement this scipt I have also included a simple loop for batch installing all of the plugin backup zips with WP-CLI.

How to Automate Batch Zip Backups of WordPress plugins

For this stage, you will need the zip binary on your Linux distro, you can check if it’s installed  on your debian or Ubuntu system with dpkg-query and for CentOS the rpm -q command accomplishes the same thing.

Checking zip Binary is Installed

For debian-based distros like Ubuntu use this command

dpkg-query -l zip

On Debian/Ubuntu you should see something like this if zip is installed

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                                  Version                         Architecture                    Description
+++-=====================================================-===============================-===============================-===============================================================================================================
ii  zip                                                   3.0-8                           amd64                           Archiver for .zip files

If you do not see the above output then install zip on Ubuntu/debian with this command

sudo apt install zip

On CentOS use this command to find out if the zip binary is installed

rpm -q zip

If you do not see output that looks like this

zip-3.0-11.el7.x86_64

To install zip on CentOS

sudo yum install zip

Now that we’ve verified the zip binary is installed and available we can move on to the batch zipping script!

Batch Zip WordPress Plugins

Here is the basic zip directory loop that should be run inside of the wp-content/plugins folder to zip each plugin folder as its own separate zip file.

Don’t run this yet though unless you want a messy wp-content/plugins folder!
for DIR in */
do 
    zip -r "${DIR%/}.zip" "$DIR"
done

Ideally you want to put these plugin zip files in a temporary directory

# make the temporary directory
mkdir /tmp/plugins

Now you can enter the WordPress installation folder’s wp-content/plugins folder

cd /var/www/guides.wp-bullet.com/wp-content/plugins

Now we can loop through each folder in the plugins folder and place the zips in our temporary plugins folder

for DIR in */
do 
    echo "Zipping $DIR" | tr -d /
    zip -r --quiet "/tmp/plugins/${DIR%/}.zip" "$DIR"
done

Here is the entire script for batch zipping all of the WordPress plugins into separate zips and placing the in your temporary folder

WPDIR=/var/www/guides.wp-bullet.com/
TMPDIR="/tmp/plugins"

for DIR in ${WPDIR}/
do 
    echo "Zipping $DIR" | tr -d /
    zip -r --quiet "${TEMPDIR}${DIR%/}.zip" "$DIR"
done

Batch Install Plugin Zips for WordPress with WP-CLI

This snippet will help you automatically batch install multiple plugin zip files with WP-CLI. This script should be run from inside of the temporary folder where you have stored all of your plugin backup zip files.

Be sure to change the WPPATH variable so it points to your WordPress files. To get the full absolute path to your WordPress installation run pwd from an SSH terminal while inside of your WordPress directory.

WPPATH="/path/to/wordpress"

for PLUGIN in *.zip
do
    # jump out if it's not a zip
    [ -f "$PLUGIN" ] || break
    # install the plugin
    wp plugin install $PLUGIN --activate --path=${WPPATH}
done

No more tedious installing WordPress plugin zips manually!

Sources

Specify Destination Directory for zip Command
Command to zip into Multiple Files