Scripting A Wordpress Upgrade

Created: 11 January 2015  Modified:

Wordpress seems to require ftp or ftps access to automatically upgrade itself.  This bothered me as I saw it as a potential security risk and work I would have to put in to setting up a FTPS server I didn’t want.  My solution was to upgrade Wordpress manually.

Following the steps in the link above Wordpress was successfully upgraded.  The process was not quick and not easy to remember if they are only used periodically.  The decision was then easy to build some scripts and directions for upgrading.

The steps need for this process is as follows

  1. Login into WordPress and disable your plugins.
  2. Login to your server using ssh.
  3. Using wget retrieve the latest copy of wordpress. “wget https://downloads.wordpress.org/release/wordpress-4.1.zip”
  4. unzip the files “unzip wordpress-4.1.zip”
  5. Backup Wordpress on the server
  6. Upgrade Wordpress on the server
  7. Go to your wordpress wp-admin page.
  8. You may be prompted to upgrade your databae.  Go ahead and do so.
  9. Enable your plugins.

For steps 4 and 5 we need some helpful scripts.  The first script dumps the mysql database and tars up the wordpress files.

wordpressbackup.sh

NOW=$(date +"%Y%m%d")

mysqldump -u root -p$1 mysite > ~/mysitebackup/mysite$NOW.sql

tar -cvzf ~/mysitebackup/mysite$NOW.tar.gz /var/www/html/mysite

The second file needs to be run in the directory containing the new Wordpress application files.  These would be the files you retrieved and extracted in steps 3 and 4.

upgradewordpress.sh

rm -rf /var/www/html/mysite/wp-includes
rm -rf /var/www/html/mysite/wp-admin

cp -rf wp-includes /var/www/html/mysite
cp -rf wp-admin /var/www/html/mysite
cp -rf wp-content /var/www/html/mysite

chown -R apache /var/www/html/mysite/wp-includes
chown -R apache /var/www/html/mysite/wp-admin
chown -R apache /var/www/html/mysite/wp-content

I placed these two files in the PATH so that I could run them from wherever.  Voila!  We now have mostly automated the manual upgrade of WordPress.

Below is a terminal capture of what the process would look like for steps 3 through 6 using the scripts.  Steps 4 and 5 will produce verbose output which i didn’t capture below.

Terminal window Steps 3-6

[user@server ~]# wget https://downloads.wordpress.org/release/wordpress-4.1.zip
--2015-01-11 17:33:43--  https://downloads.wordpress.org/release/wordpress-4.1.zip
Resolving downloads.wordpress.org (downloads.wordpress.org)... 66.155.40.203, 66.155.40.202
Connecting to downloads.wordpress.org (downloads.wordpress.org)|66.155.40.203|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6718759 (6.4M) [application/zip]
Saving to: ‘wordpress-4.1.zip’

100%[========================================================================================================================================================================================>] 6,718,759   9.46MB/s   in 0.7s   

2015-01-11 17:33:44 (9.46 MB/s) - ‘wordpress-4.1.zip’ saved [6718759/6718759]
[user@server ~]# unzip wordpress-4.1.zip
[user@server ~]# cd wordpress
[user@server wordpress]# wordpressbackup.sh 
Enter password:
[user@server wordpress]# upgradewordpress.sh
tags: bash - script - WordPress
   Less Is More