I’m new to Moodle Mayhem and happy to be posting.  Thanks in advance for any help you can provide on my question below. 

Does anybody know of a way to restore all courses from one instance of moodle into another all at once?  I know I can backup and restore each course individually but I am hoping there is a way to do this all in one swoop to save time.  Are there any folders I can copy over to make this easier?

One Response:

Find out more about Moodle Mayhem, a Moodle group for educators, online.

Great question! Duplicating a Moodle instance can be a real time-saver, eliminating the need to recreate LDAP user authentication, create admin users all over, etc. 

To repeat your question back at you, how do you restore all courses from one Moodle instance (http://website/moodle1instance) to another (http://website/moodle2instance)?

You have to consider that there are 3 components to any Moodle:
1) The MySQL database
2) The PHP files that interact with the database (located in the moodle folder at http://website/moodle1instance)
3) The MoodleData folder, which is typically located one level up from wherever the web pages are served on your web server.
Here’s how I have done this:
1) Duplicate the PHP files folder (the moodle folder) and then rename it to what you want (e.g. moodle2instance). This leaves your original intact
2) Modify the config.php (very important) file in the moodle2instance to point to a different database, moodle folder, and moodledata folder (essentially, the 3 components I mentioned above). 
This is critical because if you leave the config file intact, when you start making changes, it will save data in the moodle1instance database and moodledata rather than where it’s supposed to.

For example, the original Config.php would have these three lines (not necessarily next to each other like I’ve placed them for demonstration purposes here):

$CFG->dbname    = ‘moodle1instance‘;      // database name, eg moodle

$CFG->wwwroot   = ‘http://yourmoodleserver/moodle1instance‘;

$CFG->dirroot   = ‘/localvolume/moodle1instance‘;

$CFG->dataroot  = ‘/localvolume/moodledata/moodle1instance‘;

The items in bold would be changed to look like this:

$CFG->dbname    = ‘moodle2instance‘;      // database name, eg moodle

$CFG->wwwroot   = ‘http://yourmoodleserver/moodle2instance‘;

$CFG->dirroot   = ‘/localvolume/moodle2instance‘;

$CFG->dataroot  = ‘/localvolume/moodledata/moodle2instance‘;

3) Duplicate the MoodleData folder for Moodle1instance and rename it to Moodle2Instance_MoodleData (to keep things clean, I actually create a MoodleData folder then put folders with the same name as the Moodle.
4) Let’s review. You’ve duplicated two folders, including a) the Moodle folder with the PHP files and b) the MoodleData folder.
5) Make a backup of your Moodle1instance MySQL database. There are a variety of tools you can use to do this. I prefer phpMyAdmin or SQLYog ( – get the Community edition). You should use the SQL Dump or Export Database option. This will create a text file that, when you import it, will re-create the database as is.

Screenshots below are taken with phpMyAdmin

6) Modify the SQL Dump file. After making the backup of your Moodle1instance MySQL database, which we’ll name in this way “Moodle1Instance_moodle” (the naming convention in my neck of the woods), open it up in a text editor (on Mac, TextWrangler is a good choice. On Windows, use PSPad –; on GNU/Linux, Gedit/Nano/Kedit).
Do a find and replace on the OLD name of the database and change it to the NEW name of the database. For example:
Old database name: Moodle1instance_moodle is renamed to:
New database name: Moodle2instance_moodle

Once that is done, save the file with the name “moodle2instance” with whatever extension it had before (sql or txt).

7) Restore/import the “moodle2instance.sql” file. With PHPMyAdmin, this process will create the database for you. With other tools, you may have to create the database yourself and then import into it. Be consistent with what tool you use in exporting/backing up and importing/restoring.

so it looks like this:

Wait patiently….
Note the new database imports and creates itself…

All the original content that was in “moodle1instance” database appear in “moodle2instance”

If you mess up, drop the database and try again.

8) Before going online to the new address, verify your Moodle config.php file is correct and accurate, pointing to all the pieces. At that point, you’ll be able to go to your new Moodle and everything works fine.
That’s pretty much the process! 

Subscribe to Around the

Everything posted on Miguel Guhlin’s blogs/wikis are his personal opinion and do not necessarily represent the views of his employer(s) or its clients. Read Full Disclosure