Image of the glider from the Game of Life by John Conway
Skip to content

WordPress Upgrade Script

For those of you running WordPress on your own server, or a server that you maintain, I wrote a little script that may make upgrading your WordPress a bit easier. For me, I am running 4 installations of WordPress, so, when the upgrades are released, it's a bit of a pain to get them all into shape. So, thanks to Christer for the idea, I wrote a script that takes care of the job 10 times faster than if I were to do it by hand.

First, I should mention that this script comes with no warranty what-so-ever. Use it at your own risk. If you foul up your database or WordPress install, don't come crying to me. Although this upgraded my 4 installations without hitch, this does not necessarily mean that it will work for you. Please use caution when upgrading the files and the database. If you do run into problems, I will try to provide necessary support as possible. However, I am very busy, so it may be faster for you to figure out what happened, and to fix it on your own. You'll learn better that way anyway.

Next, this script follows the detailed instructions as closely as possible. Namely:

  • Your existing WordPress install is backed up, just in case.
  • All databases are backed up.
  • All necessary and important files (.htaccess, wp-config, etc) and directories are backed up.
  • The latest release is downloaded.
  • All files are upgraded.

Of course, it is your job to navigate your browser to the appropriate PHP upgrade page for every WordPress installation that is upgraded, and verify that all plugins, permalinks and themes work.

At any rate, here is the code: a simple Bash script. The version of the script is 0.1.2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash

# This program upgrades your existing Wordpress installations that you are running on your server.
#
# You need to make the necessary adjustments to this script as needed for your situation.
#
# Make this script executable: 'chmod 777 wp_upgrade.sh'
# Run the script: './wp_upgrade.sh'
#
# Author: Aaron Toponce
# License: GPL v2
# Version: 0.1.2

# =================== Start of Script =================== #

# Provide the necessary directories to what Wordpress installations need to be backed up space delimited
# Change as necessary and uncomment
# For example, if you had 3 sites in /var/www/site1, /var/www/site2 and /var/www/site3
# then it would look like below (do not add the trailing slash):
# directories=(/var/www/site1 /var/www/site2 /var/www/site3)
number=${#directories[@]}
# Testing that all directories specified above are valid before beginning
for (( i = 0 ; i < number ; i++ )); do
    if [[ ! -d ${directories[$i]} ]]; then
        echo "Directory ${$directories[$i]} does not exist."
        return 1
    fi
done

# First, we need to get the necessary file
cd ~

if [[ -f wordpress.tar.gz ]]; then
    echo "wordpress.tar.gz exists.  Please take notice to this upgrade before continuing."
    return 1
fi

wget -O latest.tar.gz http://wordpress.org/latest.tar.gz

echo "First disable all plugins on all installations before continuing."
echo "Press ENTER to continue..."
read blah

for (( i = 0 ; i < number ; i++ )); do
    clear
    cd ${directories[$i]}

    echo "We are backing up the full directory, in case anything goes wrong. Press ENTER..."
    read blah
    cd ../
    tar -cvvf ${directories[$i]}.tar ${directories[$i]}
    gzip  ${directories[$i]}.tar
    cd ${directories[$i]}
    mv ${directories[$i]}.tar.gz .
   
    # Timestamp in unix epoch format to create unique backup directories
    back_ts=$(date +%s)
    mkdir backup_${back_ts}
       
    # Backing up the necessary Wordpress database
    echo "Please provide the wordpress database name (case sensitive) for ${directories[$i]}:"
    read wp_db
    echo "And please provide the username to the database:"
    read wp_user
    mysqldump --add-drop-table -u ${wp_user} -p ${wp_db} > backup_${back_ts}/${wp_db}.sql
       
    # Make the necessary changes for what to backup.  This is the default as provided by Wordpress.
    echo "Backing up the important files. Press ENTER..."
    read blah
    cp .htaccess wp-config.php backup_${back_ts}
    cp -r wp-content wp-images wp-includes/languages backup_${back_ts}
   
    # Time to copy the latest wordpress that we downloaded and overwrite all files
    echo "Getting the latest cp of wordpress. Press ENTER..."
    read blah
    cp ~/latest.tar.gz ./wordpress.tar.gz
    tar -zxvf wordpress.tar.gz

    # Overwrite all files
    echo "Overwriting all old Wordpress files with the new. Press ENTER..."
    read blah
    cd wordpress
    cp -rf * ../

    # Copy the files that we backed up back
    echo "Coping the important backed up files back in. Press ENTER..."
    read blah
    cd ../backup_${back_ts}
    cp -rf * ../

    echo "Point your browser to the necessary site and run the upgrade script."
    echo "EG: http://example.com/wp-admin/upgrade.php"
    echo "Update your permalinks and .htaccess."
    echo "Install updated plugins and themes"
    echo "Reactivate plugins"
    echo "Press ENTER to continue..."
    read blah
done

clear
echo "Congratulations! You have successfully upgraded your Wordpress."
echo "Please review that your browser resolves your site."
echo "Enjoy!"

{ 9 } Comments

  1. Christer Edwards using Firefox 2.0.0.2 on Ubuntu | March 2, 2007 at 11:05 pm | Permalink

    I got one word for you...

    w00t!

  2. Martine to Albisetti using Firefox 2.0.0.2 on Ubuntu | March 3, 2007 at 10:47 am | Permalink

    Simple and effective. My favorite.
    Will start using it with the 10+ WP installations I have to keep up to date, and get you some good old feedback.

    Great job!

  3. Gergely Nagy using Firefox 1.5.0.10 on Ubuntu | March 19, 2007 at 7:05 am | Permalink

    I tested the script, really useful.
    Some extra i added to it first:
    echo the line with DB name, USER + PASS
    it's easier to copy+paste.

    The second thing i'll extend: if there's no wp_images and so on, there souldn't be any scary error messages from cp.

    The biggest problem i had in upgrading, the "latest" 2.1.2 latest.tar.gz was a plain TAR, shame on wordpress.org :)

    Greg

  4. Greg Harris using Firefox 2.0.0.4 on Mac OS | June 13, 2007 at 6:49 pm | Permalink

    Awesome script. With over 30 WP installs, this makes the upgrade task look a lot more doable without Fantastico. Speaking of, it doesn't seem that Fantastico disables plugins. If one blew up, wouldn't it just be a case of restoring backups? Also, could you not use the wp-config.php file for input for database connection info? Just trying to make it that much easier! Thanks for sharing.

  5. jose garrido using Epiphany 2.20 on Debian GNU/Linux | April 11, 2008 at 10:20 am | Permalink

    excelente script.
    muchas gracias por tu aporte (:

  6. Jerry Bell using Internet Explorer 7.0 on Windows XP | April 29, 2008 at 4:22 pm | Permalink

    Aaron,

    Thank you for the script. I found some additional ways to automate the upgrade. Also, the script creates some security issues, as it keeps a tarball with a somewhat predicable name in the document root directory, so I moved the storage location for the backups outside of the document root.

    The updated script can be found here: http://www.jerry-bell.com/2008/04/29/wordpress-backup-script/

    I'm sure my code can be improved on - I'm not the worlds best coder, but I am pretty lazy and motivated to find ways to automate things.

    Jerry

  7. Patrik using Firefox 3.0.7 on Windows XP | March 15, 2009 at 5:15 pm | Permalink

    Hi!
    Awesome script!

    A few words of caution though to anyone that tries it. As previous user commented keeping backups in the wordpress directory poses quite a security risk as anyone can just download the tarball. Keep the backups in a directory that's not accessible through the website.

    A little modification to make life yet a bit easier:

    Add a line below the directories array to handle the database names.
    directories=(/home/dir1 /home/dir2 /home/dir3)
    databasenames=(db1 db2 db3)

    Then replace wp_db in the mysqldump connection strin g with ${databasenames[$i]}

    Make sure the database names are in the same order as the directory. This can be done for the database users as well. I wouldn't recommend keeping the passwords there though... :)

    Good luck!

    /Patrik

  8. cgreality using Internet Explorer 5.01 on Windows Unknown | May 24, 2009 at 5:25 pm | Permalink

    Для тех, кто мечтает о покупке недвижимости одним из лучших предложении будет недвижимость в Черногории. Это, во-первых, прекрасное вложение денежных средств, а, во-вторых, отличное место для сезонного отдыха. Для российского гражданина, желающего приобрести недвижимость в Черногории, власти препятствий не создают. При покупке недвижимости в Черногории вид на жительство предоставляется автоматически. Купить квартиру в Словении также очень легко. Одним из способов приобретения недвижимости в Словении является создание фирмы, с начальным капиталом около 10000 евро. Цены на недвижимость в Черногории и квартиры в Словении намного меньше, чем на побережье Черного моря - в Сочи или в Туапсе.

  9. СловаьИстории using Internet Explorer 6.0 on Windows NT | July 23, 2009 at 4:37 pm | Permalink

    Галеас - ратный корабль, состоявший для вооружении многих стран Европы в XVI—XVII вв. Усовершенствованная крупная галера.
    Вот беспричинно вот! =) Интересно? - Заходи на мой сайт :)

{ 9 } Trackbacks

  1. [...] konusunda garanti vermiyorum. Database yedeinizi alp deneyebilirsiniz. Orjinal adres : Aaron Toponce Blog Archive WordPress Upgrade Script Kod [...]

  2. [...] to automagically upgrade your blog with minimal fuss, check out the script on Aaron’s blog: WordPress Upgrade Script. Worked like a charm for both the blogs I used it on, and I’ll definitely continue using it [...]

  3. [...] just supports MySQL. Also, I would like to see some way to update my WordPress installation without running a separate script to handle the upgrade. From what I understand, Movable Type supports this natively with CGI scripts [...]

  4. [...] just supports MySQL. Also, I would like to see some way to update my WordPress installation without running a separate script to handle the upgrade. From what I understand, Movable Type supports this natively with CGI scripts [...]

  5. [...] just supports MySQL. Also, I would like to see some way to update my WordPress installation without running a separate script to handle the upgrade. From what I understand, Movable Type supports this natively with CGI scripts [...]

  6. [...] just supports MySQL. Also, I would like to see some way to update my WordPress installation without running a separate script to handle the upgrade. From what I understand, Movable Type supports this natively with CGI scripts [...]

  7. Automatic wordpress updates | theInfluence | August 21, 2009 at 10:13 pm | Permalink

    [...] Toponce wrote a wonderful script to do the upgrade, and as many of the comments on the site point out it has some serious flaws, but [...]

  8. [...] Upgrade wordpress quickly in 3 easy steps from UNIX shell prompt wordpress update script WordPress Auto-Update Script For A Linux Server WordPress Upgrade Script [...]

  9. [...] and make backups of all files and database at the same time. It is a modified version of this one. By Kaspars on July 2nd, 2008 WordPress [...]

Post a Comment

Your email is never published nor shared.

Switch to our mobile site