Skip to content

Instantly share code, notes, and snippets.

@santiazpi
Last active September 20, 2018 21:52
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save santiazpi/2f83abd7527fa7299059d231f1c35fc2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Bash script to back up an easyengine webiste installation
# To debug, use set -xe
# I personally prefer to create an alias, after I placed this file at ~/scripts/
# echo 'alias eebackup=". ~/scripts/eebackup.sh"' >> ~/.bash_aliases
# . ~/.bash_rc to load the new alias
# Note that the script will also create a log at the /var/www/backupdomain.com/logs/ folder
set -e
LOG_FILE="/var/www/$1/logs/eebackup.log"
su www-data -c "mkdir -p /var/www/$1/logs/"
su www-data -c "touch $LOG_FILE"
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"
}
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
log "$1 not found in ee website list. Exiting..."
exit
}
is_ee_web $1
if [ $? -ne 0 ]; then log "Backup target installation $1 is not an ee install, exiting..."
rm $LOG_FILE
rmdir "/var/www/$1/logs/"
rmdir "/var/www/$1"
exit 0
fi
echo "This script will back up Easy Engine wp installations in $1"
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"
log "---- START BACKUP $1 ----"
log "-- Backing up $1 --"
if [ ! -d "/var/www/$1/backup" ]; then su www-data -c "mkdir -p /var/www/$2/backup"; fi
current_time=$(date "+%Y%m%d-%H%M%S")
BACKUPDIR="/var/www/$1/backup/$current_time"
do_action "mkdir -p $BACKUPDIR"
do_action "mysqldump -u $USER -p$PASSWORD $ORIGWPDBNAME > $BACKUPDIR/$ORIGWPDBNAME.sql"
log "-- databased backed up --"
log ""
do_action "zip -r $BACKUPDIR/htdocs.zip /var/www/$1/htdocs/*"
log "-- htdocs folder backed up --"
log "---- FINISHED BACKING UP $1 ----"
else
log "BACK UP $1 >> $1 - $ORIGWPDBNAME DB NOT FOUND. Exiting..."
exit 2
fi
echo >> $LOG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment