Note: You are currently viewing documentation for Moodle 1.9. Up-to-date documentation for the latest stable version is available here: Hacking the Moodle 2.0 database transfer script to convert a Moodle 1.9 site.

Hacking the Moodle 2.0 database transfer script to convert a Moodle 1.9 site

From MoodleDocs

Moodle 2.0 has a database transfer script that will copy the whole of your Moodle database to another server, even doing conversions like MySQL to PostgreSQL.

Very nice, but what if you want to convert a Moodle 1.9 database without upgrading?

The following instructions tell you how. Warning, this is a very quick and dirty hack, no guarantees. Make sure you have a backup of everything first. Even better, take a copy of your real system, and do the transfer on the copy. However, it worked for us.

Starting point

Let us suppose you have a working Moodle 1.9 install as follows (vary the instructions as necessary for your particular setup):

  • Code in /var/www/moodle19, accessible as http://example.com/moodle19/
  • moodledata in /var/moodledata.
  • some database, as set up in /var/www/moodle19/config.php.

Step 1: Get the Moodle 2.0 code

  • Download a copy of the Moodle 2.0 code to somewhere, say /var/www/moodle20.
  • Make a new moodledata for this, say: /var/moodledata20.
  • copy /var/www/moodle19/config.php to /var/www/moodle20/config.php
  • edit /var/www/moodle20/config.php to make wwwroot, dirroot and dataroot point at the new locations.

At this point it is essential that you do not go to http://example.com/moodle20/ and let your Moodle upgrade itself.

Step 2: Hack the code to skip the login step

  • Open /var/www/moodle20/admin/dbtransfer/index.php in your editor.
  • Comment out the lines
require_login();
admin_externalpage_setup('dbtransfer');
  • There are 4 lines that call functions called
admin_externalpage_print_header();
admin_externalpage_print_footer();
Change these to call plain print_header/footer() instead.
  • Save index.php.
  • Open /var/www/moodle20/lib/sessionlib.php in your editor.
  • Find the function confirm_sesskey();
  • Add a line at the start of this function:
return true;
  • Save sessionlib.php.

Step 3: Create the new database you want to copy to

As if you were about to install Moodle. You need an empty database, and a 'moodle' user that can connect to it with full control.

Step 4: Replace all the database definition files in 2.0 with their 1.9 equivalents

What we need to do is delete all of the install.xml files that belong to Moodle 2.0, wherever they are inside the moodle20 directory, and instead copy in all the ones belonging to Moodle 1.9. This is best done with the following command-line magic:

cd /var/www/moodle20
find . -iname 'install.xml' -exec rm -f {} \;
mkdir -p mod/workshop/db mod/lams/db mod/journal/db mod/excercise/db
cd /var/www/moodle19
find . -iname 'install.xml' -exec cp {} ../moodle20/{} \;

Now there is a tricky issue to sort out. Moodle 1.9 had a blocks/db/install.xml and backup/db/install.xml file, which does not exist in Moodle 2.0, and will not be found, even if you copy the file the the corresponding place in Moodle 2.0. Therefore, we cheat, and copy that install.xml file somewhere where it will be found.

cd /var/www/moodle20/mod
mkdir -p blocks/db backup/db
cp /var/www/moodle19/blocks/db/install.xml blocks/db/
cp /var/www/moodle19/backup/db/install.xml backup/db/

Step 5: Do the copy

Hopefully, that works. Good luck!