Wordpress Tutorial: Easily Upgrade with SSH and Linux Shell Script
Overview
We often upgrade multiple Wordpress sites when they become available and using the normal methods available can consume time and produce complications. However writing a simple shell/bash script to keep on your server can upgrade your Wordpress install in less than 5 seconds.
If you have a general shared hosting account with just ftp access, and your hosting provider will not enable SSH or otherwise called “shell access” then this tutorial is not for you.
Backup
You never know what can happen and we don’t assume any liability for the use of anything that can potentially occur. The script below includes a file backup mechanism but you should add your own mysql dump calls or manually access phpmyadmin and download a backup of your Wordpress website database.
Step-by-Step
*Note: The $ symbol is to demonstrate a linux command to run in the window, only copy or type the text that follows it to run the command.
- First access your server with SSH and navigate to the directory that contains the folder that contains your Worpress install.
- Use vi or pico to create a new file. For this exercise we will use vi.
$ vi upgradewordpress.sh - Type “i” to enter insert mode and copy and paste the following code, comments have been added in the code as to what each line does. replace “public_html” with the directory or path to the directory that contains your Wordpress Installation.
#!/bin/bash #This is to backup your current install encase there are any problems that occur. First a variable is set to name the backup with the current date and then a tar file is created to compress all the files. OF="wwwbackup-$(date +%Y%m%d).tgz"; tar -cZvf $OF public_html/*; # Download latest wordpress zip and give the zip a new name to reference for unzipping since each wordpress download will be a different name with version number wget -O wordpress.zip "http://wordpress.org/latest.zip"; # Unzip the file unzip wordpress.zip; # Remove the zip file rm wordpress.zip; # Copy all the new files from the unzipped folder into your functional Wordpress folder and overwrite the old ones. cp -r wordpress/* public_html/; # Remove the unzipped folder rm -r wordpress/; # Output that all steps are complete. echo "Wordpress Successfully Installed"; #exit from script execution exit
- Press “esc” to exit the “insert” mode in vi.
- Now save the file and exit out of vi.
#this writes/saves the contents to the file. :w #this quits vi :q
- Now give the script execution permissions.
$ chmod u+x upgradewordpress.sh
- Run the script.
$ ./upgradewordpress.sh - Your Done!
Result
Now you have a script you can save to your hard drive and upload to any website that you need to upgrade and just simply give the script executable permissions and then run and your done.







