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

 

Moodle and e-commerce shopping carts

Linking Moodle to a web based shopping cart is easy and requires very little coding to allow users to browse courses, add them to a cart, pay for them, and then automatically gain access to the paid courses on Moodle.  And best if all, only use one login for the entire process.

Moodle + Magneto = Amazing Moodle Shopping CartMy personal favourite Open Source carts are OS Commerce and the newer, slicker Magento.  But it really doesn’t matter which cart you use, as long as it is driven by a popular web database such as MySQL or MSQL. It can be home grown or a commercial platform.

This short blog posts explains the process in its simplest terms.

In your online shop you create courses as products. You can put them in categories, offer discounts and name then whatever you like. Just like you would if we’re selling DVDs or shoes.

The important bit is to use a unique field for your product that we can use as a course ID number in Moodle.

Once you have your courses entered into your shop and you’ve configured it to accept payments and create accounts you are ready to instruct Moodle to read the products and the users from the shop database.

For this step we will be using 2 important Moodle features.

  1. Plugins > Authentication
  2. Plugin > Enrolment

Under the authentication plugin in Moodle, choose “External Database” and complete the form to match the credentials of your shopping carts database.  You will need to know the user fields used in your shopping cart and basically match them up to the user fields in Moodle.

Now under the enrolment plugin we need to do the same thing, this time we are matching fields from your product table with fields in the Moodle course table.

Now make sure that your courses have the same IDNUMBER as your products in the shopping site.  Lastly, make sure both plugins are enabled and away you go.

Here’s  a quick recap of the process and what happens:

  1. Your user visits your shopping site
  2. They browse for courses and add them to the cart
  3. They go to the checkout and pay for the courses and enter their personal details
  4. Now they can visit your Moodle site and login with the same details they just entered for the shopping site
  5. Moodle will log in the user and enrol them on the correct courses (i.e the ones they have paid for).

You can take this further and theme the shopping site to match your Moodle install and customise the emails to provide direct links to the course in the emailed receipt.  And if you want to go even further, you could tweak the code to keep the same login session so it’s seamless between the 2 systems.

I hope you found this useful and if anyone is thinking about creating a shopping cart for Moodle.