File: /home/httpd/html/ugtube.com/wpup.sh
#!/bin/bash
#Generate a random hash to use for backup filenames, makeing sure its unique
r=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c12`
while [ -e ../$r.sql ]
do
r=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c12`
done
#Logging this all
echo "=$r=Starting Upgrade in `pwd` at `date`==" >> /var/log/wordpress.script.log
#Read the wordpress config
echo -n Scanning wordpress config...
dname=`grep DB_NAME wp-config.php | awk -F \' '{print($4)}'`
echo -n .
dpass=`grep DB_PASSWORD wp-config.php | awk -F \' '{print($4)}'`
echo -n .
duser=`grep DB_USER wp-config.php | awk -F \' '{print($4)}'`
echo -n .
dhost=`grep DB_HOST wp-config.php | awk -F \' '{print($4)}'`
echo -n .
#Make sure we have the database info
if [ -z "$dname" ]; then echo Unable to find database name;exit 1;fi
echo -n .
if [ -z "$dpass" ] ;then echo Unable to find database password;exit 1;fi
echo -n .
if [ -z "$duser" ] ;then echo Unable to find database user;exit 1;fi
echo -n .
if [ -z "$dhost" ] ;then echo Unable to find database host;exit 1;fi
echo ... found $dname with user $duser on server $dhost
#Logging this all
echo ="$r"=Found MySQL info: "$duser"@"$dhost"/"$dname" >> /var/log/wordpress.script.log
echo Creating backup with hash $r
#Create MySQL backup
echo -n MySQL Backup.. ;
#Make a non-locking dump of the database (many users dont have LOCK priv)
if `mysqldump -f -Q -q -c --lock-tables=false -u $duser -p"$dpass" -h $dhost $dname > ../$r.sql `; then
echo ..MySQL completed
echo ="$r"=MySQL dump completed >> /var/log/wordpress.script.log
chmod 600 ../$r.sql
else
echo ..MySQL error
echo ="$r"=MySQL dump failed >> /var/log/wordpress.script.log
chmod 600 ../$r.sql
exit 1
fi
chown 0:0 ../$r.sql
chmod 700 ../$r.sql
#Using /var/tmp/installer/wordpress.latest.tar.gz for wordpress archive, make sure its new, if not delete it and get a new one.
[ -d /var/tmp/installer ] || mkdir -p /var/tmp/installer
find /var/tmp -mtime +1 -name wordpress.latest.tar.gz -exec rm '{}' \;
[ -e /var/tmp/installer/wordpress.latest.tar.gz ] && echo Already have recent wordpress archive || wget -O /var/tmp/installer/wordpress.latest.tar.gz http://wordpress.org/latest.tar.gz
[ -e /var/tmp/installer/wordpress.latest.tar.gz ] || exit
#Tar up the files we are going to replace
echo -n Wordpress Files Backup..;
#Get the files out of the new tarball
filesnew=`tar -z -f /var/tmp/installer/wordpress.latest.tar.gz -t | awk -F / '{print($2)}' | sort | uniq | grep -v ^$ | tr '\n' ' '`
for x in $filesnew;do
[ -e "$x" ] &&files="$files"" ""$x"
done
#Use those files to tar existing files
echo ="$r"=File Backup attempting >> /var/log/wordpress.script.log
if `tar -f ../$r.tar -c $files `
then
echo ..Complete;
echo ="$r"=File Backup complete >> /var/log/wordpress.script.log
chmod 600 ../$r.tar
else
echo ..Files failed
echo ="$r"=File Backup failed >> /var/log/wordpress.script.log
chmod 600 ../$r.tar
exit 1
fi
chown 0:0 ../$r.tar
chmod 700 ../$r.tar
#unpack the new wordpress
echo Extracting new WordPress...
(tar -z -f /var/tmp/installer/wordpress.latest.tar.gz -x --strip-components=1) || (echo Failed, trying with legacy tar options; tar -z -x --strip-path=1 -f /var/tmp/installer/wordpress.latest.tar.gz)
#chown it to the owner of the paren folder
echo -n Chowning... ; chown -R --reference=./ $filesnew && echo ...Complete || echo ...Failure
#chmod everything to safe perms
echo -n Chmoding... ; chmod -R u=rwX,g=rX,o=rX $filesnew && echo ...Complete || echo ...Failure
#chmod wp-content so wordpress can use it
echo -n Chmoding wp-content... ;chmod -R u=rwX,g=rwX,o=rX ./wp-content/ && echo ...Complete || echo ...Failure
echo ="$r"=Upgrade Complete >> /var/log/wordpress.script.log
#echo $filesnew