Using WordPress CLI to update multiple sites

By June 3, 2015 Uncategorized No Comments


#!/bin/bash
# Script to update every WordPress Install in a given directory
serverdir="var/www"
for d in /$serverdir/*/ ; do (cd "$d" &&
name=$(basename "$d")
printf "Currently updating $name! \n"
printf "Should we do this one? ($name) (y/n) \n"
read answer
if echo "$answer" | grep -iq "^y" ;then
wp db export --allow-root
cd /$serverdir
tar -zcf $name.tar.gz $d
cd $d
wp core update --allow-root
wp core update-db --allow-root
wp plugin update --all --allow-root
wp theme update --all --allow-root
printf "Please check the site for errors... \n"
printf "Everything OK? (y/n) \n"
read answer
if echo "$answer" | grep -iq "^y" ;then
continue
else
printf "D'oh! Restore, Restore! \n"
cd /$serverdir
printf "Rolling back $name"
printf "Wait! This will override the current site with the backup! \n"
printf "Press enter to continue, or exit with Ctrl-C \n"
read
rm -rf $name
read
tar xzf $name.tar.gz
cd /$serverdir/$serverdir
mv $name /$serverdir/
cd $d
wp db import --allow-root
printf "Please update manually \n"
fi
printf "Press Enter to Continue to the next site...\n"
read answer
else
printf "On to the next \n"
fi
);

cd ~
rm -rf /$serverdir/$serverdir
done

Simple, but significant Let's get started.