Upgrading Moodle from 2.1 to 2.5 using command line and automated batch files

Upgrading Moodle from 2.1 to 2.5 using command line and automated batch files

This summer I am undertaking a huge Moodle upgrade.  We are actually moving up 4 versions in one go.  But as you are already aware, you can’t simply upgrade from 2.1 to 2.5 in one step.  And the processes involved to get that far are tedious and time consuming.  So I have a solution to make the upgrade as simple and as seamless as possible, and that’s to use command line and a clever batch file.

Batch Files Moodle
Batch files- it takes you back to the 90s, in a good way!

So let’s take a look at what’s involved without using command line upgrades and batch files.
To upgrade Moodle from 2.1 we would have to do the following:

  1. Backup the DB
  2. Delete the contents of the webfolder leaving the config.php
  3. Copy the 2.2 files across
  4. Login to the site and run the upgrade page and sit through the upgrade process
  5. Whilst crossing your fingers and praying the server doesn’t time out.

And then once this is done, you repeat the process for 2.3, then for 2.4, and then for 2.5.  This would take huge amounts of time, and most likely time out and requires you to be sat there watching..waiting..stressing!

Command Line
Upgrading Moodle from the command line is always recommended, it won’t time out and is much safer and faster.  But we’d still need to delete files, copy files and keep track of everything.  The solution to this is to create a batch file that triggers all the commands sequentially and completes the steps for us.

The Solution

Windows batch files are so easy to create using a text editor like notepad.  Each command is on its own line and the whole thing is relatively painless.  We begin by dumping the current database to an SQL backup file in case we break things.

We need to create a folder on our server with all the various Moodle versions in separate folders, along with our custom stuff.  So it looks something like this:

Windows Explorer showing custom files
Windows Explorer showing custom files

In this folder (called upgrade) I have the Moodle 2.2 files, the 2.3 files, 2.4 and 2.5 respectively.  I also have a copy of my Moodle config.php file in a folder called config (I’ll need to keep copying this across before each upgrade when I wipe the folder contents).  All my custom code and third party blocks and modules are in the custom folder and follow the same file structure as Moodle.

So we create a file called “moodleupgrade.bat” and with a text editor begin to write DOS commands. The first command is the most important – BACK IT UP!

ECHO Moodle Upgrade Batch script thingymysql --user=root --password=******* > moodle.sql

Now we need to clear the contents of the webroot and copy the Moodle 2.2 files across.
We use Robocopy for this because RoboCopy is awesome , it’s fast, it’s built into Windows and it can mirror folders and directories (thus wiping them).

robocopy c:\upgrade\moodle22 c:\sites\lcmvletest\public_html /MIR
robocopy c:\upgrade\moodle22 c:\sites\lcmvletest\public_html config.php

We have now copied all the files ready to upgrade, we now need to run the Moodle upgrade script from command line.  And because we don’t want to have to enter Yes when asked, we want it to be automatic we prefix the command with that key stroke

cd PHP
ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php
Moodle Batch file upgrade
Command line, your old new best friend

We then repeat the process for all the other versions.  Lastly we add our custom blocks and third party blocks by using Robocopy and the upgrade script from DOS.
And it’s as easy as that.  The hardest part is remebering where all your files are making sure you have all the files required for the upgrade.

The full script I use is found below.  It currently takes 45 minutes to run through and upgrade Moodle from 2.1 to 2.5 with all the modules and blocks in place.

And the best thing is, it is purely automated, no user input required and it can be set as a scheduled Windows tasks so you could tell it to trigger in the early hours of the morning for you to wake up to.  But I’m not that brave, I would prefer to watch it on screen as it happens.

I hope you find this useful.

ECHO Moodle Upgrade Automation
mysql --user=root --password=******** moodle > moodle.sql

robocopy c:\upgrade\moodle22 c:\sites\lcmvletest\public_html /MIR

ECHO Now putting config.php back in
robocopy c:\upgrade\config c:\sites\lcmvletest\public_html config.php
ECHO Files copied, now running upgrade script
cd PHP

ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php 
ECHO Upgrade to 2.2 complete, now copying files for 2.3

ECHO Copying 2.3 files...
robocopy c:\upgrade\moodle23 c:\sites\lcmvletest\public_html /MIR
ECHO Now putting config.php back in
robocopy c:\upgrade\config c:\sites\lcmvletest\public_html config.php

ECHO Now let's upgrade to 2.3...
ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php 
ECHO Upgrade to 2.3 complete, now copying files for 2.4

ECHO Copying 2.4 files...
robocopy c:\upgrade\moodle24 c:\sites\lcmvletest\public_html /MIR
ECHO Now putting config.php back in
robocopy c:\upgrade\config c:\sites\lcmvletest\public_html config.php

ECHO Now let's upgrade to 2.4...
ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php 
ECHO Upgrade to 2.4 complete, now copying files for 2.5

ECHO Copying 2.5 files...
robocopy c:\upgrade\moodle25 c:\sites\lcmvletest\public_html /MIR
ECHO Now putting config.php back in
robocopy c:\upgrade\config c:\sites\lcmvletest\public_html config.php

ECHO Now let's upgrade to 2.5...
ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php 

ECHO now copying database enrol tweaks
robocopy c:\upgrade\custom\database c:\sites\lcmvletest\public_html\enrol\database /MIR

ECHO now copying custom blocks
robocopy c:\upgrade\custom\blocks c:\sites\lcmvletest\public_html\blocks /s
ECHO Now run upgrade script to install blocks
ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php 

ECHO Blocks done, now copying custom mods
robocopy c:\upgrade\custom\mod c:\sites\lcmvletest\public_html\mod /s
ECHO Now run upgrade script to install mods...
ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php 

ECHO mods complete, now copying custom local plugins
robocopy c:\upgrade\custom\local c:\sites\lcmvletest\public_html\local /s
ECHO Now run upgrade script to install custom local plugins
ECHO y|php.exe c:\sites\lcmvletest\public_html\admin\cli\upgrade.php 

ECHO ALL done!!!UPGRADE COMPLETE
PAUSE

 

Previous post Moodle MySQL Replication using PHPMyAdmin
Piwik with Moodle Next post 5 Reasons to use Piwik with Moodle