Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 701b52cbe9
Fetching contributors…

Cannot retrieve contributors at this time

executable file 84 lines (73 sloc) 2.133 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#!/bin/bash
#
# Download latest version of Wordpress and install to site directory

# Ensure running as root
if [ "$(id -u)" -ne 0 ]; then
    echo 'This script is designed to run as root'
    exit
fi

# Check arguments
if [ $# -ne 1 ]; then
    echo 'Syntax: wordpress-update.sh <site-name>'
    exit
fi

# Determine site directory and user
SITENAME=$1
DESTDIR=/srv/www/$SITENAME
SITE_USER=$SUDO_USER
if [ ! -d $DESTDIR ]; then
    DESTDIR=(/srv/www/*/$SITENAME)
    if [ ! -d $DESTDIR ]; then
        echo "Site $SITENAME is not found"
        exit 1
    fi
    SITE_USER=$(echo $DESTDIR | awk -F'/' '{print $4}')
fi
ROOTDIR=$DESTDIR/public
SITE_GROUP='www-data'

# Ensure destination directory exists
if [ ! -d $ROOTDIR ]; then
    echo "Site root directory $ROOTDIR does not exist"
    exit 1
fi

# Check prerequisites
echo 'Install required packages'
apt-get -qy install zip > tmp/logfile

# Download Wordpress
echo 'Download and unpack WordPress'
if [ -d tmp/wordpress ]; then
    rm -rf tmp/wordpress
fi
wget -qO tmp/wordpress.tar.gz http://wordpress.org/latest.tar.gz
if [ $? -ne 0 ]; then
    echo 'Failed to download latest version of WordPress'
    exit $?
fi
cd tmp
tar xf wordpress.tar.gz > logfile
cd ..

# Create backup of current files in directory
echo 'Create backup of current files in site'
BACKUP_DIR='/srv/www/.backups'
if [ ! -d $BACKUP_DIR ]; then
    mkdir $BACKUP_DIR
    chown root:adm $BACKUP_DIR
    chmod 770 $BACKUP_DIR
fi
TODAY=`date +%F`
zip -qr $BACKUP_DIR/$SITENAME-$TODAY.zip $DESTDIR/*

# Copy all files
echo 'Copy WordPress to site'
cp -r tmp/wordpress/* $ROOTDIR/
rm -rf tmp/wordpress
rm tmp/wordpress.tar.gz

# Set permissions (in case new files added/changed)
chown -R $SITE_USER:$SITE_GROUP $ROOTDIR
chmod -R 0750 $ROOTDIR
find $ROOTDIR -type d -exec chmod 2750 {} \;
chmod -R 0770 $ROOTDIR/wp-content/plugins
find $ROOTDIR/wp-content/plugins -type d -exec chmod 2770 {} \;

# Apply patches to Wordpress
# This one removes a replacement of -- when saving post content
sed -i 's/'\''--'\''/'\''-_'\''/' $ROOTDIR/wp-includes/formatting.php

echo "Updated Wordpress for site $1 successfully"
Something went wrong with that request. Please try again.