Upgrade WordPress automatically

July 7th, 2013

WordPress has a one-click upgrade feature, but it requires you to give it your FTP/SFTP credentials. If you log into SSH with keys instead of passwords, you can’t use it. I wrote a script that runs on the server (thus it needs no password or special permissions) to back up your current files, then upgrade.

Lines 6 through 10 features variables that you’ll probably want to alter before you run this script.

A few notes:

  • It only backs up the files, not the database, so if WordPress has a database update that breaks your database, hopefully you’ve got some other backup in place.
  • Sometimes after an update, WordPress will prompt you to please update the database. This script will not do that step – you still need to log in and click the button. This script will send you a reminder that an upgrade has been performed and which will allow you to both confirm that nothing was broken, and click the aforementioned button. I don’t trust myself to act on that reminder promptly, so I haven’t set this script to automatically run. Instead, I have a reminder on my phone to go run it manually.
  • A future version might delete all the old backups (both WordPress versions and websites).


#!/bin/bash
# Created Sept 1, 2012
# Last updated July 7, 2013
# Automatically update WordPress.
 
# Alter these to reflect your environment
website='~/public_html' # website and htdocs might be the same. If unsure, keep them identical.
htdocs='~/public_html/htdocs'
email='you@example.com'
sitename='My web site'
 
date=`date +%y-%m-%d`
log=wp_upgrade_${date}.txt
 
if [ -f latest.zip ]; then
rm latest.zip
echo Removed old latest.zip >> $log
fi
 
wget http://wordpress.org/latest.zip
echo Got latest version of WordPress >> $log
 
unzip latest.zip
echo Unzipped latest version of WordPress >> $log
 
# Create old/ if DNE
if [ ! -d old ]; then
mkdir old
echo Created directory for old stuff >> $log
fi
 
# Create a backup of your website before making changes
htdocs_dir=`basename $htdocs`
new_htdocs=${htdocs_dir}_{$date}
cp -Rv $htdocs old/$new_htdocs
 
echo Copied htdocs to $new_htdocs >> $log
 
# Put the files for the new WordPress version into your website.
cp wordpress/* $htdocs -Rv
 
echo Dumped new WordPress into htdocs >> $log
 
# Archive this version of WordPress. You could probably delete it, but I like to have a clean copy around in case I muck up the code.
mv wordpress/ old/wordpress_${date}
 
echo Copied pristine WordPress code into old/ >> $log
 
# Sometimes it has database upgrades and a human needs to visit the site
# to tell it to go ahead, so this e-mail serves as your reminder.
mail $email -s "Wordpress updated on $sitename - go check it out" < $log

Assuming you store this in ~/bin/upgrade_wordpress.sh, you can run this script monthly automatically with the following steps:

  1. At a prompt, type 'crontab -e'
  2. On most systems, this will open vi. Type "i" to insert text.
  3. Paste:
    0 0 1 * * ~/bin/upgrade_wordpress.sh
  4. Push the ESC key to exit insert mode.
  5. Type ":wq" to write changes then quit vi
    1. Tags: , ,

Comments are closed.