+++
title = "wp_patcher – a method for updating WordPress."
author = "Danilo M."
type = "tech"
date = "2009-07-22T13:37:40+00:00"
excerpt = "Here is a very simple method for updating your WordPress installation: wp_patcher!"
categories = [ "Code", "DIY"]
tags = ["bash", "svn", "update", "WordPress"]
+++

Hello everyone,

unfortunately, as we all know, summer is very bad for the content of my blog. During this season, it becomes difficult to stay at home and write on the computer. The laptop keyboard turns into a hot plate on which you could easily roast meat for a picnic, and being near it is like lighting a fireplace with 40 degrees Celsius ambient temperature – practically torture…

<!--more-->

However, the WordPress developers continue to diligently release updates, and it is well known that you should always update your WP installation to avoid unpleasant issues such as data loss or hacking of your website. To help bloggers using the latest versions of WP, it is possible to automatically update your installation using an option in the control panel. This option, at least for me, has not worked in the last two revisions of version 2.8, so I decided to try another approach :)

While chatting on [slacky.eu][1], I was suggested to use svn to retrieve the various versions of WP and then take only the differences so as to replace only the files that have actually been modified. At that point, I thought, why not write a bash script that asks me only for my current version and the version I want to update to, and then creates an archive containing only the files I need??

So I got to work and played around a bit with Vim and came up with **wp_patcher.sh**, a bash script that downloads the two requested revisions of WordPress, checks the differences, and creates a tar.bz2 archive ready to be installed ;)

This is the script:

```bash
#! /bin/bash
# WP-patcher - script che si occupa di recuperare le differenze
# tra 2 versioni differenti di WordPress, scaricando le revisioni
# tramite svn.

# Autore: Danilo 'danix' Macrì - http://danixland.net
# Licensed under the statements of the GPL v3 - http://www.gnu.org/licenses/gpl-3.0.html

# exit status
E_NOARGS=171

# variabili
workdir="wp_patch"
old_vrs=$1
new_vrs=$2
sane_old_vrs=`echo $1 |sed s/[.]//g`
sane_new_vrs=`echo $2 |sed s/[.]//g`
patchdir="${sane_old_vrs}_to_${sane_new_vrs}_patch"

#funzione di aiuto (viene mostrata in caso di errore nell'avvio del programma)
showhelp () {
  echo "USO: `basename $0` vecchia_versione nuova_versione"
  echo "dove 'vecchia_versione' è la tua attuale versione di WordPress"
  echo "e 'nuova_versione' è la versione verso cui vuoi upgradare"
  exit $E_NOARGS
}

# controllo che siano presenti gli argomenti in modo da avere
# le versioni di WP da scaricare
if [ -z $1 ];then
  showhelp
elif [ -z $2 ];then
  showhelp
fi

# inizio l'esecuzione dei comandi
mkdir $workdir
cd $workdir
echo "sto recuperando le 2 versioni svn"
svn co http://core.svn.wordpress.org/tags/$old_vrs
echo "ho recuperato la versione $old_vrs"
svn co http://core.svn.wordpress.org/tags/$new_vrs
echo "ho recuperato la versione $new_vrs, adesso saranno esportate."
svn export $old_vrs wp_$sane_old_vrs
svn export $new_vrs wp_$sane_new_vrs
rm -rf $old_vrs $new_vrs
echo "ho esportato correttamente le 2 versioni di wordpress"
echo "adesso procedo alla creazione della patch"
diff -qr wp_$sane_old_vrs wp_$sane_new_vrs |cut -d" " -f4 > patch_file.txt
mkdir $patchdir
mods=`cat patch_file.txt`
for i in $mods;do
  cp --parents $i $patchdir
done
echo "compressione in corso..."
cd $patchdir
mv wp_$sane_new_vrs wp_$new_vrs
tar -jcvf ../wp_${patchdir}.tar.bz2 wp_$new_vrs
cd ../
mv wp_${patchdir}.tar.bz2 ../
echo "patch creata"
echo "ripulisco la directory di lavoro"
cd ../
rm -rf $workdir
echo "l'archivio contenente i files da sostituire"
echo "è il file 'wp_${patchdir}.tar.bz2"
# fine :)
exit 0
```

It is very simple and should not be difficult to modify according to your needs…

The script should be saved with a name of your choice (I called it wp_patcher.sh) and then launched in this way:

`wp_patcher.sh vecchia_versione nuova_versione`

In any directory in your home directory, of course, it is advisable to launch it in an empty directory to avoid conflicts with the files and directories that the program creates during its operation…

In the example I gave you, ‘old_version’ will be your current version of WordPress, and ‘new_version’ will be the version you want to update to.

In order for the script to work, it is necessary that svn, tar, and all basic commands such as mv, cp, rm, etc. are installed on the system… but I doubt you are using any Linux distribution and don’t have these programs installed :wink: .

Once the archive with the files to be updated has been created, all you have to do is unzip it and upload the files to your web space, overwriting the existing ones. If an update of the WordPress database is necessary, just launch the page from your browser: **http://urldelblog/wp-admin/upgrade.php**.

> **_As always, the advice applies: before updating WordPress, make a backup of the files and the database, so that you can restore a working situation in case of problems._**

This script, as you can see, is released under the GPL v3, so it can be freely used and modified within the terms of this license.

If it turns out to be useful to you, or even if you just want to tell me that you have tried it or that it didn’t work, or praise me or tell me to go to hell :smile: don’t hesitate and leave me a comment…

See you soon

[1]: http://www.slacky.eu "The Italian community of Slackware users"