Skip to content

Instantly share code, notes, and snippets.

@daddykotex
Created October 17, 2016 18:27
Show Gist options
  • Save daddykotex/e3557ff92c3d5da9379c74c92492a228 to your computer and use it in GitHub Desktop.
Save daddykotex/e3557ff92c3d5da9379c74c92492a228 to your computer and use it in GitHub Desktop.
Update wordpress via shell access (3.1+)
#!/bin/bash
set -ex
# Update wordpress to a specific version. This script should work with versions above 3.1
# If you provide a LANG_CODE. It will be used to the appropriate Wordpress package.
# LANG_CODE examples: fr_CA, pt_BR, es_ES
# See WP supported languages here: https://codex.wordpress.org/Installing_WordPress_in_Your_Language#Manually_Installing_Language_Files
# See extended WP procedures here: https://codex.wordpress.org/Upgrading_WordPress_Extended
# $0 WORDPRESS_VERSION WORDPRESS_DIR [LANG_CODE] [BACKUP_DIR]
if [ "$#" -ne 2 ] && [ "$#" -ne 3 ] && [ "$#" -ne 4 ]; then
echo "Illegal number of parameters: only 2, 3 or 4 expected."
echo "Usage: $0 <WORDPRESS_VERSION> <WORDPRESS_DIR> [LANG_CODE] [BACKUP_DIR]"
exit 1
fi
WORDPRESS_VERSION=$1
WORDPRESS_DIR=$2
LANG_CODE=$3
USER_BACKUP_DIR=$4
if [ ! -z "$USER_BACKUP_DIR" ] && [ ! -d "$USER_BACKUP_DIR" ]; then
echo "USER_BACKUP_DIR: $USER_BACKUP_DIR does not exist."
exit 1
fi
if [ -z "$USER_BACKUP_DIR" ]; then
BACKUP_DIR=/tmp
else
BACKUP_DIR=$USER_BACKUP_DIR
fi
if [ ! -d "$WORDPRESS_DIR" ]; then
echo "WORDPRESS_DIR: $WORDPRESS_DIR does not exist."
exit 1
fi
# Preparation
TS=$(date +%s)
TMP_WP_FOLDER=/tmp/WORDPESS-$WORDPRESS_VERSION-$TS
BACKUP_WP_FOLDER=$BACKUP_DIR/WORDPESS-$TS-BACKUP
## backup
mkdir $BACKUP_WP_FOLDER
cp -r $WORDPRESS_DIR $BACKUP_WP_FOLDER
echo "To restore backup: \"rsync -rt --delete $BACKUP_WP_FOLDER/ $WORDPRESS_DIR/\"."
## download
echo "Downloading..."
if [ -z "$LANG_CODE" ]; then
WORDPRESS_FILENAME=wordpress-$WORDPRESS_VERSION.zip
WORDPRESS_URL=https://wordpress.org/$WORDPRESS_FILENAME
else
URL_LANG_CODE=$(echo $LANG_CODE | tr '[:upper:]' '[:lower:]' | tr "_" "-" )
WORDPRESS_FILENAME=wordpress-$WORDPRESS_VERSION-$LANG_CODE.zip
WORDPRESS_URL=https://$URL_LANG_CODE.wordpress.org/$WORDPRESS_FILENAME
fi
mkdir $TMP_WP_FOLDER
wget -q -O $TMP_WP_FOLDER/$WORDPRESS_FILENAME $WORDPRESS_URL
unzip -q -d $TMP_WP_FOLDER $TMP_WP_FOLDER/$WORDPRESS_FILENAME
echo "New version of wordpress in: $TMP_WP_FOLDER."
# install
echo "Installing."
rsync -rt --delete --exclude 'wp-content' --exclude 'wp-config.php' $TMP_WP_FOLDER/wordpress/ $WORDPRESS_DIR/
rsync -rt $TMP_WP_FOLDER/wordpress/wp-content/ $WORDPRESS_DIR/wp-content/
echo "Done."
# cleanup
rm -rf $TMP_WP_FOLDER
echo "Leaving backup in $BACKUP_WP_FOLDER"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment