Upgrade WordPress using Python Fabric
by Stii
I’m simply loving Python Fabric. When I heard WordPress 2.7 was out, I thought “I wonder if I could automate this upgrading process?” since it takes a little time to do it manually. Specially if you have to upgarde more than one blog, this would be handy. Here is my fab recipe.
Before you get stuck in, note that you’ll need SSH access and you have to have it set up with automatic login. Here is a guide to do that SSH Automatic Login. Also, I’ve created the wpupgrade and wpupgrade/bac folders beforehand.
I’ve created my fabfile and added the following:
def setup():
set(fab_user = 'stii')
set(fab_hosts = ['stii.co.za'])
def apache_restart():
"Reboot Apache."
setup()
sudo('/etc/init.d/apache2 restart')
def wp_upgrade():
"Upgrade wordpress"
setup()
run('mkdir ~/wpupgrade/$(fab_timestamp)')
run('mkdir ~/wpupgrade/bac/$(fab_timestamp)')
run('wget http://wordpress.org/latest.tar.gz \
-O ~/wpupgrade/$(fab_timestamp)/latest.tar.gz')
run('tar -C /home/wpupgrade/$(fab_timestamp)/ \
-xvzf ~/wpupgrade/$(fab_timestamp)/latest.tar.gz')
run('cp -R /path/to/wordpress \
~/wpupgrade/bac/$(fab_timestamp)/')
sudo('cp -R ~/wpupgrade/$(fab_timestamp)/wordpress/* \
/path/to/wordpress/')
sudo('chown -R /path/to/wordpress')
You’ll see that I’ve created a setup() function that basically sets the host and user for both the apache_restart and wp_upgrade functions. I’ve added an apache_restart function for illustrative purpose. If you do need to restart Apache often, there. Thats how. Then I added the wp_upgrade() magic. Here are the steps taken:
- I first create directories in my wpupgrade folder using the fab_timestamp variable. The fab_timestamp is when the instance of Fabric started. This is just so I don’t overwrite things.
- Next I use wget to fetch the lastest upgrade. We know wordpress upgrades are always in the same place at http://wordpress.org/latest.tar.gz which makes this easy. I download it to the newly created timestamp directory.
- Then I simply untar it to that same directory.
- I make a backup of my current installation to the ~/wpupgrade/bac/timestamp directory, just to be on the safe side! If something goes horribly wrong, I can always recover.
- Copy the new WordPress files to your web directory and change the ownership and Dave’s your uncle. Its that simple!
When WordPress 2.7.1 and future upgrades are released, all I would have to do is to fire up the Terminal and type:
> fab wp_upgrade
Nice! I’ll adapt this a bit and add database backup and rollback if something horrible went wrong when I have a little more time. Next time!
wow – nice little snippet dude
Thanx Steven. :)
Cool! Great! Thanks for that!
Though I believe, after 2.7, upgrades will be one click, from within WP, so you’ll only need to use this script one last time :)
I’m not sure magitam… I don’t really trust that! I’ll continue using my trusty Fabric setup. At least I’ll know that permissions will safe.
[...] then on your dashboard already included in the 2.7 is all of your stats from all of your blogs Upgrade WordPress using Python Fabric – stii.co.za 12/12/2008 wplogo-notext-rgb I’m simply loving Python Fabric. When I heard WordPress [...]
[...] petit bout de code, inspiré par ici , a permis d'automatiser les [...]