Skip to content

Instantly share code, notes, and snippets.

@kingkero
Created August 8, 2017 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingkero/2ce55515b10ab7b1d89ef0614003c7dd to your computer and use it in GitHub Desktop.
Save kingkero/2ce55515b10ab7b1d89ef0614003c7dd to your computer and use it in GitHub Desktop.
backup WP, then update all via WP-CLI
#!/bin/sh
# MNKY lab's custom backup script
# author: Martin Rehberger <martin@mnkylab.com>
# author uri: https://mnkylab.com
# license: GPL v3 or later
# license uri: https://www.gnu.org/licenses/gpl.html
### variables
WPCLI=/home/www/.../files/wp-cli.phar
EXPORTPATH=~/html/backups/
STARTTIME=`date +%Y-%m-%d.%H-%M`
DBEXPORT=export-$STARTTIME.sql
### helper functions
log() {
# $1 is the message to log
echo "[$(date +%H:%M:%S)] $1"
}
backup_wp_db() {
# $1 is path where export should be stored
# $2 is path to WP install
php_cli $WPCLI db export $1/$DBEXPORT --path=$2
}
clean_wp_db() {
# $1 is path where export is stored
rm $1/$DBEXPORT
}
copy_tar_root() {
# $1 abs path to dir
# $2 abs path to tar file
TMP=/tmp/$(basename $1)
cp -R $1 $TMP
tar czPf $2 $TMP
rm -rf $TMP
}
backup() {
# $1 abspath
# $2 localpath
# $3 tar
backup_wp_db $1 $2
copy_tar_root $1 $3
clean_wp_db $1
}
update() {
# $1 localpath
php_cli $WPCLI plugin update --all --path=$1
log "finished plugins"
php_cli $WPCLI theme update --all --path=$1
}
backup_update() {
# $1 is abspath
TIME=`date +%Y-%m-%d.%H-%M`
TAREND=-$TIME.tar.gz
ME=$EXPORTPATH$(basename $1)$TAREND
if [ -f $1/htdocs/index.php ]; then
LOCALPATH=$1/htdocs
elif [ -f $1/index.php ]; then
LOCALPATH=$1
fi
backup $1 $LOCALPATH $ME
log "finished backup"
update $LOCALPATH
log "finished update"
}
### start backup
for D in ~/html/wordpress-*; do
log $D
if [ -f $D/wp-config.php ]; then
log "start $(basename $D)"
backup_update $D
log "end $(basename $D)"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment