Skip to content

Instantly share code, notes, and snippets.

@santiazpi
Last active July 16, 2018 17:14
Show Gist options
  • Save santiazpi/c150af1db22291bb0965ba5680c9ee3f to your computer and use it in GitHub Desktop.
Save santiazpi/c150af1db22291bb0965ba5680c9ee3f to your computer and use it in GitHub Desktop.
#!/bin/bash
# Bash script to clone an easyengine webiste installation onto another (the destination site must exist already)
# To debug, use set -xe
# I personally prefer to create an alias, after I placed this file at ~/scripts/
# echo 'alias eeclone=". ~/scripts/eeclone.sh"' >> ~/.bash_aliases
# . ~/.bash_rc to load the new alias
# Note that the script will also create a log at the /var/www/destinationdomain.com/logs/ folder
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 ""
echo "Be sure to backup the destination site before if there is any information there that needs safeguarding"
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 ----"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment