Working with wordpress is a charm but it does get to a point where keeping your blog primmed and updated becomes more of a chore. Problem is, if you don’t update your blog often then you’re likely to be vulnerable to attacks… but what if you need to update 50 or more blogs? Hassle? Not really.
Here’s where the beauty of linux comes in. Shell scripting (or shell programming) is an indespensible tool to automating tasks mundane tasks and it’s the perfect medium to keep your wordpress installations update.
What’s the motivation for this? Well, wordpress is a popular platform which is no wonder that the bad guys are targeting it. Since I’m in charge of running and maintaining several wordpress installations, I thought I’d do something to lessen the load off my back so I decided to clean the dust off my linux shell scripting experience and get down and dirty.
What do I need?
There are several requirements for this method to work so it won’t be for everyone. Hopefully though, I’ve managed to make the code flexible enough to fit the needs of anyone who can use it.
Ok here’s the requirements:
- SSH root access (or equivalent… you can have a non-root account but access to sudo)
- Your wordpress blogs are hosted in a single server (or multiple servers)
- Familiarity with the directory structure of your wordpress installation – you need this because the default structure that the script is based on may not apply to your server.
- Courage to tinker with the terminal.
Where’s the code?
It’s right here. You can copy paste it to your favorite text editor or download the attached file. If you opted to copypasta, be sure that you convert it to unix format (on notepad++ it’s Format > Convert to Unix format) or you’ll end up with ^M errors when executing.
A few important notes however:
- This code assumes that your server configuration hosts your wordpress blogs in the following structure:
- Take note that the script relies on account name = home directory. This shouldn’t be a problem for people using cPanel but for everyone else, take time to explore your server.
- The script doesn’t copy the wp-content folder since (depending on your server config) it may overwrite your current one (and you don’t want that)
- The script cleans after itself. If you’ve configured it right, you don’t have to worry about orphan files floating around.
Now the code:
blog[1]=domainOne blog[2]=domainTwo blog[3]=domainThree blog[4]=domainFour #blog[5]=blog5 #add another blog by uncommenting the line above wget http://wordpress.org/latest.tar.gz -O latest.tar.gz tar xzf latest.tar.gz echo "Wordpress has been extracted" #for loop for your blog[] array for a in 1 2 3 4 do echo "Giving ${blog[$a]} an Upgrade" cp wordpress/* ${blog[$a]}/public_html/ #copy files to wp-admin echo "Manually copying wp-admin" cp -Rf wordpress/wp-admin/* ${blog[$a]}/public_html/wp-admin #copy files to wp-includes echo "Manually copying wp-includes" cp -Rf wordpress/wp-includes/* ${blog[$a]}/public_html/wp-includes #change ownership of files you've just copied chown ${blog[$a]}:${blog[$a]} ${blog[$a]}/public_html/* chown -Rf ${blog[$a]}:${blog[$a]} ${blog[$a]}/public_html/wp-admin/ chown -Rf ${blog[$a]}:${blog[$a]} ${blog[$a]}/public_html/wp-includes/ echo "Done upgrading ${blog[$a]}" done rm -Rf wordpress rm -Rf latest.tar.gz echo "Done Cleaning up" exit 0
Download wordpress_update
What do I do with it?
Make sure the script is configured to your server’s configuration (accounts, directories of hosted domains, etc) before proceeding otherwise you may end up cleaning up instead of updating anything.
Important disclaimer: Proceed at your own risk. I will not be held responsible for any damage done to your server since I’m just sharing what I’ve done on my side so that you can learn from it then apply to your own situation. If you own a VPS (or higher) make sure you know what you are doing. If you’re on shared hosting, consult with your host first.
- SSH to your server with root access (login via root account or equivalent)
- Upload the wordpress_update file in to the /home directory (you can rename it as you like)
- change file permission to 744
chmod 744 wordpress_update
./wordpress_update
Click for the full size.
And you’re done. I hope you pardon me for brushing out 2 lines. Security purposes 😀
Yes, the output is verbose (I can’t for one, remove the message of omitting directories with the cp command) and untidy but it works. And yes, I’m not an expert in linux shell scripting so if anyone can suggest something, please post a comment. I’ll be glad to update the listing with the new version :D.
If you’re going to re-release a version of the code on your blog, please link back here. Thanks.
You would not believe how long ive been googling for something like this. Through 6 pages of Google results without finding anything. First page of bing. There was this…. Gotta start using that more often
Thanks. I’ve been working on several options so that people will easily find this guide but sadly I’m at a loss at which keyword to target. In any case let me know if it works for you 🙂
Thank you, a lot! I can finally update 50~ blogs without having to spend more than 5 minutes :))