Posts Tagged ‘bash’

WordPress Auto-Update Script For A Linux Server

Monday, March 16th, 2009

I wrote a little update script for my server to auto update my wordpress installation and figured it might help some other people as well. To use it you simply supply as arguments to the script three things:

1. the directory where your wordpress install lives
2. the name of the backups you’d like to create for that directory
3. the version of the wordpress install you’d like to upgrade to

For example, on my server I use this command to update my wp version:

sudo ./updateWordpress.sh /path/to/wordpress/installation efnx 2.7.1
[enter password]
[watch output]

done!

Here is the code to my script:

#!/bin/bash
DIR2UPDATE=$1
NAME=$2
VERSION=$3

echo "Beginning update of $DIR2UPDATE to version $VERSION..."

if [ -d wordpress_svn ]
then
    day=`date | cut -d" " -f3`
    tme=`date | cut -d" " -f4`
    hour=`echo ${tme} | cut -d":" -f1`

    fileday=`ls -lh | grep wordpress_svn | cut -c 37-39`
    filehour=`ls -lh | grep wordpress_svn | cut -c 40-41`

    if [ ${fileday} != ${day} ]; then
            echo "SVN directory not up to date, [ file's date ${fileday} != today ${day} ] deleting and updating"
        rm -rf wordpress_svn/*
        cd wordpress_svn
            svn co http://svn.automattic.com/wordpress/tags/${VERSION} .
    else
            if [ "$filehour" != "$hour" ]; then
                    echo "SVN directory not up to date, [ file's hour ${filehour} != now ${hour} ] deleting and updating"
                rm -rf wordpress_svn/*
                    cd wordpress_svn
                    svn co http://svn.automattic.com/wordpress/tags/${VERSION} .
        else
                    echo "SVN directory is up to date, skipping update"
                cd wordpress_svn
        fi
    fi

else
    echo "Creating new svn directory and checking out version $VERSION...";
    mkdir wordpress_svn;
    cd wordpress_svn;
    svn co http://svn.automattic.com/wordpress/tags/${VERSION} .;
fi

cd ..

if [ -d wordpress_int ]
then
    echo "Removing old intermediate container...";
    rm -rf wordpress_int;
fi

echo "Creating new intermediate container..."
mkdir wordpress_int
echo "Moving version $VERSION files into intermediate container..."
cp -rpf wordpress_svn/* wordpress_int

echo "Moving config and custom files from $NAME into intermediate container..."

cp -p ${DIR2UPDATE}/wp-config.php wordpress_int
cp -rpf ${DIR2UPDATE}/wp-content/* wordpress_int/wp-content/
cp -p ${DIR2UPDATE}/.htaccess wordpress_int
echo 'Updating svn for wp-content'
svn update wordpress_int

echo "Backing up $DIR2UPDATE..."
mkdir ${NAME}_backup
#mv -f ${DIR2UPDATE}/* ${NAME}_backup   # if you'd like to mv instead of cp
cp -rpf ${DIR2UPDATE}/* ${NAME}_backup
tar -czvf ${NAME}_backup.tar.gz ${NAME}_backup
rm -rf ${NAME}_backup

echo 'Removing svn data from intermediate container...'
rm -rf `find wordpress_int/ -type d -name .svn`
echo "Moving intermediate container contents to $DIR2UPDATE..."
cp -rpf wordpress_int/* ${DIR2UPDATE}

Or you can just download the script here [rightclick + 'save as']->
updateWordpress.sh

Auto Comment Block In TextMate

Monday, January 21st, 2008

If you haven’t heard of TextMate go check it out it’s sweet. It allows a helluva lot of automation and is damn near the only thing you need for development of any language [minus compilers]. I’m about ready to throw away my Flash CS3 IDE I got for free from my day job and shell out the 39 pounds to buy it [those of you who know me personally know I don't ever buy software].

So, here’s a little command I wrote in the form of a bash script [I think all textmate commands are bash scripts or cocoadialog thangs]. The script takes selected input and pads it with “/* */” if it isn’t already, effectively commenting the text or it strips the comment block. I don’t write shell scripts often, so I like it when I get one that’s really useful.

#!/bin/bash

#-------------------------------------------------------------------------
# Make comment / Undo comment
# by Schell Scivally
# @ efnx.com
#-------------------------------------------------------------------------

opening=`echo "$TM_SELECTED_TEXT" | awk 'BEGIN {nlines=0} /\/\*/ {nlines++} END {print nlines}'`
if [[ "$opening" == 0 ]];
    then
        echo -n "/*$TM_SELECTED_TEXT*/"
    else
        output=`echo "$TM_SELECTED_TEXT" | sed 's/\*\///g' | sed 's/\/\*//g'`
        echo -n "$output"
fi

Follow me on GitHub
Follow me on Google+
Follow me on Twitter