Clone site bash script - eeclone

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
2 Likes

Thanks for this script. It has saved me bigtime after the Duplicator plugin made me waste 3 hours.

Perfect for moving a developed site from staging.example.com to example.com in the same DO server.

Great script thanks for sharing! Could it be modified for non-ee site with similar file structure in Ubuntu or Debian?

OK, this is my cleanest version yet. If I ever update this script, it will be in this gist:

#!/bin/bash

log ()
{
    STAMP=$(date +%Y-%m-%d_%H-%M-%S)
    echo "$STAMP - $1"
    echo "$STAMP - $1" >> $LOG_FILE
}

do_action ()
{
        log "$1"
        su www-data -c "$1"
}

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

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"

LOG_FILE="/var/www/$2/logs/eeclone.log"
su www-data -c "mkdir -p /var/www/$2/logs/"
su www-data -c "touch $LOG_FILE"

USER=`grep -Po '^user = \K(.*)' /etc/mysql/conf.d/my.cnf`
PASSWORD=`grep -Po '^password = \K(.*)' /etc/mysql/conf.d/my.cnf`

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 "---- START CLONE $1 TO $2 ----"

                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"
                su www-data -c "mkdir $BACKUPDIR"
                do_action "mysqldump -u $USER -p$PASSWORD $DESTWPDBNAME > $BACKUPDIR/$DESTWPDBNAME.sql"

                log "-- Finished backing up $2 --"
                do_action "zip -r $BACKUPDIR/htdocs.zip  /var/www/$2/htdocs/*"
                echo
                log "finished backing up -- cloning db"
                echo
                do_action "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
                do_action "rsync -av /var/www/$1/htdocs/ /var/www/$2/htdocs/"
                log "$1 wp-content cloned to $2"

                echo
                do_action "wp search-replace 'https?:\/\/(www\.)?$1' 'http$S2://$2' --regex --recurse-objects --skip-columns=guid --path='/var/www/$2/htdocs/'"

                echo
                do_action "wp cache flush  --path=/var/www/$2/htdocs/"

                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
1 Like

I updated eeclone.sh script, removed some bugs and I do not attempt to backup the destination site anymore, I created an eebackup.sh script to backup sites when I want to, that I simply run bafore eeclone.

Hope they are useful for someone, for me they are increadibly so!