Here is the bash script that I use to clone installations, while we wait for EE4…
The scripts uses the root Mysql userename / password at /etc/mysql/conf.d/my.cnf in default EE installs, and needs the destination site to already exist; it backups up destination site.
It runs like (if you name the file eeclone)
. eeclone originaldomain.com destinationdomain.com
Hope it is helpful for some!
#!/bin/bash
TEMP_DIRECTORY=$PWD"/temp"
LOG_FILE="/var/www/$2/logs/eeclone.log"
mkdir -p /var/www/$2/logs/
touch $LOG_FILE
log ()
{
STAMP=$(date +%Y-%m-%d_%H-%M-%S)
echo "$STAMP - $1"
echo "$STAMP - $1" >> $LOG_FILE
}
USER=`grep -Po '^user = \K(.*)' /etc/mysql/conf.d/my.cnf`
PASSWORD=`grep -Po '^password = \K(.*)' /etc/mysql/conf.d/my.cnf`
is_ee_web() {
for i in $( ee site list ); do
WEBNAME=$(echo $i | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g")
if [ -n "$1" ]; then
if [ "$1" == "$WEBNAME" ]; then
return 0
fi
fi
done
return 1
}
is_ee_web $1
if [ $? -ne 0 ]; then log "Orinating website: $1 is not an ee install, exiting..." && exit 0; fi
is_ee_web $2
if [ $? -ne 0 ]; then log "Destination website: $2 is not an ee install, exiting..." && exit 0; fi
log "---- START CLONE $1 TO $2 ----"
echo "This script works when Easy Engine wp installations in $1 and $2 exist and are compatible"
echo "It will OVERWRITE $2 with all contents (wp-content folder and the database) from $1"
echo "Also make sure that NO CHACHING PLUGIN IS ACTIVE AT $1 AND THE CACHE IS FLUSHED"
echo
echo "ARE YOU SURE?"
echo ""
read -p "Press enter to continue, ctl-C to cancel"
if [ -a "/var/www/$1/wp-config.php" ]; then
ORIGWPDBNAME=`cat /var/www/$1/wp-config.php | grep DB_NAME | cut -d \' -f 4`
log "FOUND $1 DB: $ORIGWPDBNAME"
if [ -a "/var/www/$2/wp-config.php" ]; then
DESTWPDBNAME=`cat /var/www/$2/wp-config.php | grep DB_NAME | cut -d \' -f 4`
log "FOUND $2 DB: $DESTWPDBNAME"
log "Backing up $2"
if [ ! -d "/var/www/$2/backup" ]; then mkdir "/var/www/$2/backup"; fi
current_time=$(date "+%Y%m%d-%H%M%S")
BACKUPDIR="/var/www/$2/backup/$current_time"
mkdir "$BACKUPDIR"
mysqldump -u $USER -p$PASSWORD $DESTWPNAM > $BACKUPDIR/$DESTWPDBNAME.sql
zip -r $BACKUPDIR/htdocs.zip /var/www/$2/htdocs/*
log "finished backing up -- cloning db"
mysqldump -u$USER -p$PASSWORD $ORIGWPDBNAME | mysql -u$USER -p$PASSWORD $DESTWPDBNAME
#checking if any of the sites have ssl enabled
if ( ee site info $1 | grep 'SSL.*enabled' ); then S1="s"; else S1=""; fi
if ( ee site info $2 | grep 'SSL.*enabled' ); then S2="s"; else S2=""; fi
echo
ACTION="rsync -av /var/www/$1/htdocs/ /var/www/$2/htdocs/"
log "$ACTION"
su www-data -c "$ACTION"
log "$1 wp-content cloned to $2"
echo
ACTION="wp search-replace 'https?:\/\/(www\.)?$1' 'http$S2://$2' --regex --recurse-objects --skip-columns=guid --path='/var/www/$2/htdocs/'"
log "$ACTION"
su www-data -c "$ACTION"
echo
ACTION="wp cache flush --path=/var/www/$2/htdocs/"
log "$ACTION"
su www-data -c "$ACTION"
log "$ORIGWPDBNAME cloned to $DESTWPDBNAME including http: url change and cache flush"
log "---- END CLONE $1 TO $2 ----"
else
log "CLONE $1 TO $2 >> $2 DB NOT FOUND. Exiting..."
exit 4
fi
else
log "CLONE $1 TO $2 >> $1 - $ORIGWPDBNAME DB NOT FOUND. Exiting..."
exit 3
fi