Note: You are currently viewing documentation for Moodle 3.8. Up-to-date documentation for the latest stable version of Moodle may be available here: Moodle migration.

Moodle migration: Difference between revisions

From MoodleDocs
(→‎See also: video linnk thanks to Garrett Reynolds)
 
(44 intermediate revisions by 21 users not shown)
Line 1: Line 1:
There may be times when you need to move your Moodle site from one server to another, for example, moving from shared hosting to a dedicated server - this is known as migrating your Moodle site. One common mistake to watch out for is to remember to update the relevant details in your Moodle ''config.php'' file.
{{Installing Moodle}}
There may be times when you need to move your Moodle site from one server to another. For example, moving a Moodle site from shared hosting service's server to a dedicated server.


==Migrating a complete Moodle site - method 1==
==Recommended method==


This involves moving a whole site from one server to another. If you are changing the domain/IP address to the new server you need to do these steps:
This involves moving a whole site from one server to another. If you are changing the domain/IP address to the new server you need to do these steps:
* '''Maintenance mode'''. Place your current Moodle site in maintenance mode to prevent any further additions to the Moodle database. Don't let administrators login during the migration as they are not affected by the maintenance mode setting.
* '''Backup your current Moodle database'''. Do this by following the instructions in the [[Upgrading | upgrading Moodle]] page. This will give you a text file containing the mysql dump.
* '''Copy the Moodle software'''. You will need to copy the Moodle code itself to the new server - upgrade the code to the latest version if you can.
* '''Change <code>$CFG->wwwroot</code>'''. In your (possibly new) Moodle directory, change the <code>$CFG->wwwroot</code> variable in the ''config.php'' file for the new server.
* '''Copy data directory contents (moodledata)'''. Copy the contents of your data directory (check for the value in <code>$CFG->dataroot</code>) to the new server.
* '''Review moodledata permissions'''. Check also that permissions remain the same on the new dataroot folder and change the value if you have changed its location on the new server.
* '''Change your Moodle URL'''. If you have a new URL, you'll need to change this in the Moodle database to the new server. This is needed as links to pictures, files, etc are stored as absolute links and will reference the old <code>$CFG->wwwroot</code> value. So when loading a mysql backup dump of the Moodle server into mysql on another server the absolute referenced links will be broken. There are two methods of doing this:
:(a) The first method changes the Moodle URL using the Moodle script ''replace.php'' while your site is currently running just before you backup the Moodle database. Point your browser to <nowiki>http://yourserver.com/admin/replace.php</nowiki>


:Enter the url for your old server (<nowiki>http://oldserver.com/</nowiki>) and new server (<nowiki>http://newserver.com/</nowiki>) and it will fix the mysql tables. You will also need to clear out any cached links by restarting your webserver. Now, take another backup of the Moodle database - this one will have the correct URLs.
===Turn on maintenance mode===


:(b) The second method is to backup the Moodle database first, then use the search and replace feature of your text editor (or use a unix tool like sed) to replace the old URL with the new one in the mysql backup file. Here is an example sed command:
Place your current Moodle site into [[Maintenance mode]] via ''Site Administration > Server > Maintenance Mode'' to prevent any further additions to the Moodle database. Don't let administrators login during the migration as they are not affected by the maintenance mode setting.


: <code>#sed -e 's/oldserver.com/newserver.com/g' oldmysqldump.sql > newmysqldump.sql</code>
===Backup the Moodle database on the old server===


:''TIP:'' You may want to check the mysqldump file to see how the old server was referenced.
The right way to back up your database depends on which database system you are using. The instructions below are one way to back up a MySQL database. Another option would be to use a tool like phpMyAdmin to manually make a backup. The documentation for your database will give more options. There are many ways to do such backups. Here is an outline of a little script you can run from command line on Unix to backup the database:
:After changing the URL, restore the mysql database
* '''Test the migration'''. To test the new install, access Moodle using your browser and the new server's URL. When you have tested that a number of links in the courses work, take the new Moodle site out of maintenance mode.
'''See also''': [http://moodle.org/mod/forum/discuss.php?d=85812 Forum discussion] on migrating Moodle's data directory on a Windows system.


==Migrating a complete Moodle site - method 2==
<pre>
cd /my/backup/directory
mv moodle-database.sql.gz moodle-database-old.sql.gz
mysqldump -h example.com -u myusername -p'mypassword' -C -Q -e --create-options mydatabasename > moodle-database.sql
</pre>
If you only write "-p" without your password, you will be prompted for it.


Do you have shell access on both servers? If so, the following method is a very quick and efficient method to migrate a Unix based site.
=== Restore the database backup to the new server ===
 
Copy the database back up files to the new server and restore into the new database server.
 
Once you have created the new database on the new server:
 
<pre>mysql -p new_database < moodle-database.sql</pre>
 
For other databases, follow their instructions for restoring a backup.
 
===Copy moodledata from the old server to the new server===
 
Copy the contents of your data directory (check for the value in <code>$CFG->dataroot</code>) to the new server. This can be a lot of data, so consider using a good data copying tool like [https://rsync.samba.org/ rsync]. If using an FTP client, the transfer of the <code>filedir</code> folder must be in '''BINARY''' mode or the files will get corrupted in the process.
 
NB: It is not necessary to copy the contents of these directories:
* /moodledata/cache
* /moodledata/localcache
* /moodledata/sessions
* /moodledata/temp
* /moodledata/trashdir
Omitting these files will reduce transfer time substantially.
 
Check the file permissions of the copied files. The web server needs read and write access.
 
===Copy the Moodle code from the old server to the new server===
 
You will need to copy the Moodle code itself to the new server (this is the Moodle folder found in your webroot folder e.g. /var/www or public_html).
 
Check the file permissions of the copied files. The web server needs read access.
 
===Update config.php with the URL of the new server===
 
If the migration will move Moodle to a new URL, then update $CFG->wwwroot in config.php to point to the new location.
 
Also check the other properties there. Is the path $CFG->moodledata still correct? Do the database connection settings need to be changed?
 
===Test the copied site===
 
You should now be able to log into the new site as admin, and verify that most things are working.
 
===Update links containing wwwroot in the database===
 
The one thing we have not fixed is any internal links stored in the database. To fix these use the [[Search and replace tool]] by going to {wwwroot}/admin/tool/replace/index.php.
 
Enter the url for your old server (<nowiki>http://oldserver.com/</nowiki>) and new server (<nowiki>http://newserver.com/</nowiki>) and it will fix any links stored in the database.
 
===Take the site out of maintenance mode===
 
Test the migration some more, then when you are satisfied, remember to take the site out of maintenance mode.
 
==Quick and hacky method==
 
If you have shell access on both servers, here is a quick command-line based method.


It is also useful for creating snapshots or test sites.
*Set up a new empty database on the '''new''' server.
*Set up a new empty database on the '''new''' server.
*Place your Moodle site into maintenance mode.
*Place your existing Moodle site into maintenance mode.
*Login to shell on the '''old''' server.
*Login to shell on the '''old''' existing server.
*Use rsync to copy '''moodledata''' and '''public_html''' (or whatever directory your Moodle install is in) to the new server - execute (replacing caps with your details; SOURCE = the directory you want to copy) for each directory:
*Use rsync to copy '''moodledata''' and '''public_html''' or '''moodle''' folders (or whatever directory your Moodle install is in) to the new server - execute (replacing caps with your details; SOURCE = the directory you want to copy) for each directory:
::<code>rsync -av -e ssh SOURCE/ USERNAME@NEW_SERVER.COM:/PATH/TO/DESTINATION/</code>
::<code>rsync -av -e ssh SOURCE/ USERNAME@NEW_SERVER.COM:/PATH/TO/DESTINATION/</code>
*Dump existing database and move and import into database on new server by executing:
*Dump existing database and move and import into database on new server by executing:
::<code>mysqldump --allow-keywords --opt -uMySQL_USERNAME -pPASSWORD DATABASE | ssh USER@DOMAIN "mysql -uMySQL_USERNAME -pPASSWORD DATABASE"</code>
::<code>mysqldump --allow-keywords --opt -uMySQL_USERNAME -pPASSWORD DATABASE | ssh USER@DOMAIN "mysql -uMySQL_USERNAME -pPASSWORD DATABASE"</code>
*On the '''new server''', update '''config.php''' with relevant details.
*Replace any links in the database that contin the full site URL:
*To fix any internal Moodle links, login to your "new" Moodle install on your new server and use '''admin/replace.php''' to search and replace the old uri for the new.
::<code>#sed -e 's/oldserver.com/newserver.com/g' oldmysqldump.sql > newmysqldump.sql</code>
*On the '''new server''', update '''config.php''' with relevant details where applicable (e.g. database name and user details, the wwwroot and the dataroot).
*Check ownership and permissions are correct on both moodle code and moodledata directories.
*Make sure everything is working.
*Make sure everything is working.


Takes about 15 minutes.
Takes about 15 minutes for a small site. However, transferring several Gigabytes of data for a larger site can take hours depending on your network connection and hard drive read/write speed.
 
When you are happy all has gone well, set up redirects/make DNS changes if required, take new site out of maintenance mode and "switch off" old site.
 
==Other points to consider==


===Upgrade Moodle===
When you are happy all has gone well, set up redirects/make DNS changes if required, take new site out of maintenance mode and "switch off" old site.


When migrating Moodle it is often a good idea to take the opportunity to upgrade Moodle to the latest version. If you manage your own server, follow the instructions in [[Upgrading | upgrading moodle]], otherwise check if your host can upgrade for you.
*If you are switching the ip address from the old server to the new one, you will need to turn off the old server before firing up the new one to avoid ip addressing conflicts and confusion!


===Restoring a single course across servers===
==Other considerations==


You may need to restore a single course from an old site to a new one, especially if you are testing the migration. When restoring a Moodle backup file to Moodle on a different server than the one used to create the backup, the absolute referenced links to files maybe broken. To fix this problem open the ''backup-coursename.zip'' file and edit the ''moodle.xml'' file replacing links with <code>$@FILEPHP@$</code>.
===Upgrade Moodle at the same time?===


For example, replace <nowiki>http://yourserver.com/file.php/243/</nowiki> with <code>$@FILEPHP@$</code>
While doing the work of migrating Moodle, you might want to [[Upgrading | upgrade Moodle]] to the latest version at the same time. On the other hand, if you do that, and something breaks, you won't be sure which change caused the problem, so the more cautious approach is to change one thing at a time, and test in between to verify that all is well.
When the file is restored it will use the correct file path for the new course.


===DNS & Masquerading changes===
===DNS & masquerading changes===


You may have had to change the DNS entries for the new Moodle site. If you have done so, it will take some time for the changes to replicate, so be patient. If your server is located behind a firewall, you may also have to change your firewall rules to allow access to the new server. See the [[Masquerading | masquerading docs]].
You may have had to change the DNS entries for the new Moodle site. If you have done so, it will take some time for the changes to replicate, so be patient. If your server is located behind a firewall, you may also have to change your firewall rules to allow access to the new server. See the [[Masquerading | masquerading docs]].
Line 64: Line 106:
===Internal and external access===
===Internal and external access===


If you have a set up where your Moodle site can be accessed via a network and via the internet, ensure you check that the new site can be accessed internally and externally.   
If you have a set up where your Moodle site can be accessed via a network and via the internet, ensure you check that the new site can be accessed internally and externally.
 
===reCAPTCHA===
If you migrate to a new domain and have setup [[Email-based_self-registration]], you need to create new API-Keys at google. You will find the explanation and links to google in [[Email-based_self-registration]].


==See also==
==See also==


Moodle Docs:
* [[Site backup]]
* [[Site backup]]
* [[Site restore]]
* [[Backup and restore FAQ]]
* [[Backup and restore FAQ]]
* [http://www.youtube.com/watch?v=sFCXvRx20Bs Moving Moodle to a new server video]


Using Moodle forum discussions:
==Any questions?==
* [http://moodle.org/mod/forum/discuss.php?d=62959 Changing Moodle URL]
* [http://moodle.org/mod/forum/discuss.php?d=57477 Changing site address]
* [http://moodle.org/mod/forum/discuss.php?d=76704 Upgrading whilst migrating]
* [http://moodle.org/mod/forum/discuss.php?d=65450 Internal and external access]
* [http://moodle.org/mod/forum/discuss.php?d=111807 Migrated Moodle to New Server But Can't Login]


[[Category:Installation]]
Please post in the [http://moodle.org/mod/forum/view.php?id=28 Installing and upgrading help forum] on moodle.org


[[es:Migración de Moodle]]
[[fr:Migration de Moodle]]
[[fr:Migration de Moodle]]
[[nl:Moodle verhuizen]]
[[ja:Moodle移行]]
[[ja:Moodle移行]]
[[de:Moodle-Migration]]

Latest revision as of 17:15, 12 January 2020

There may be times when you need to move your Moodle site from one server to another. For example, moving a Moodle site from shared hosting service's server to a dedicated server.

Recommended method

This involves moving a whole site from one server to another. If you are changing the domain/IP address to the new server you need to do these steps:

Turn on maintenance mode

Place your current Moodle site into Maintenance mode via Site Administration > Server > Maintenance Mode to prevent any further additions to the Moodle database. Don't let administrators login during the migration as they are not affected by the maintenance mode setting.

Backup the Moodle database on the old server

The right way to back up your database depends on which database system you are using. The instructions below are one way to back up a MySQL database. Another option would be to use a tool like phpMyAdmin to manually make a backup. The documentation for your database will give more options. There are many ways to do such backups. Here is an outline of a little script you can run from command line on Unix to backup the database:

cd /my/backup/directory
mv moodle-database.sql.gz moodle-database-old.sql.gz
mysqldump -h example.com -u myusername -p'mypassword' -C -Q -e --create-options mydatabasename > moodle-database.sql

If you only write "-p" without your password, you will be prompted for it.

Restore the database backup to the new server

Copy the database back up files to the new server and restore into the new database server.

Once you have created the new database on the new server:

mysql -p new_database < moodle-database.sql

For other databases, follow their instructions for restoring a backup.

Copy moodledata from the old server to the new server

Copy the contents of your data directory (check for the value in $CFG->dataroot) to the new server. This can be a lot of data, so consider using a good data copying tool like rsync. If using an FTP client, the transfer of the filedir folder must be in BINARY mode or the files will get corrupted in the process.

NB: It is not necessary to copy the contents of these directories:

  • /moodledata/cache
  • /moodledata/localcache
  • /moodledata/sessions
  • /moodledata/temp
  • /moodledata/trashdir

Omitting these files will reduce transfer time substantially.

Check the file permissions of the copied files. The web server needs read and write access.

Copy the Moodle code from the old server to the new server

You will need to copy the Moodle code itself to the new server (this is the Moodle folder found in your webroot folder e.g. /var/www or public_html).

Check the file permissions of the copied files. The web server needs read access.

Update config.php with the URL of the new server

If the migration will move Moodle to a new URL, then update $CFG->wwwroot in config.php to point to the new location.

Also check the other properties there. Is the path $CFG->moodledata still correct? Do the database connection settings need to be changed?

Test the copied site

You should now be able to log into the new site as admin, and verify that most things are working.

Update links containing wwwroot in the database

The one thing we have not fixed is any internal links stored in the database. To fix these use the Search and replace tool by going to {wwwroot}/admin/tool/replace/index.php.

Enter the url for your old server (http://oldserver.com/) and new server (http://newserver.com/) and it will fix any links stored in the database.

Take the site out of maintenance mode

Test the migration some more, then when you are satisfied, remember to take the site out of maintenance mode.

Quick and hacky method

If you have shell access on both servers, here is a quick command-line based method.

  • Set up a new empty database on the new server.
  • Place your existing Moodle site into maintenance mode.
  • Login to shell on the old existing server.
  • Use rsync to copy moodledata and public_html or moodle folders (or whatever directory your Moodle install is in) to the new server - execute (replacing caps with your details; SOURCE = the directory you want to copy) for each directory:
rsync -av -e ssh SOURCE/ USERNAME@NEW_SERVER.COM:/PATH/TO/DESTINATION/
  • Dump existing database and move and import into database on new server by executing:
mysqldump --allow-keywords --opt -uMySQL_USERNAME -pPASSWORD DATABASE | ssh USER@DOMAIN "mysql -uMySQL_USERNAME -pPASSWORD DATABASE"
  • Replace any links in the database that contin the full site URL:
#sed -e 's/oldserver.com/newserver.com/g' oldmysqldump.sql > newmysqldump.sql
  • On the new server, update config.php with relevant details where applicable (e.g. database name and user details, the wwwroot and the dataroot).
  • Check ownership and permissions are correct on both moodle code and moodledata directories.
  • Make sure everything is working.

Takes about 15 minutes for a small site. However, transferring several Gigabytes of data for a larger site can take hours depending on your network connection and hard drive read/write speed.

When you are happy all has gone well, set up redirects/make DNS changes if required, take new site out of maintenance mode and "switch off" old site.

  • If you are switching the ip address from the old server to the new one, you will need to turn off the old server before firing up the new one to avoid ip addressing conflicts and confusion!

Other considerations

Upgrade Moodle at the same time?

While doing the work of migrating Moodle, you might want to upgrade Moodle to the latest version at the same time. On the other hand, if you do that, and something breaks, you won't be sure which change caused the problem, so the more cautious approach is to change one thing at a time, and test in between to verify that all is well.

DNS & masquerading changes

You may have had to change the DNS entries for the new Moodle site. If you have done so, it will take some time for the changes to replicate, so be patient. If your server is located behind a firewall, you may also have to change your firewall rules to allow access to the new server. See the masquerading docs.

Internal and external access

If you have a set up where your Moodle site can be accessed via a network and via the internet, ensure you check that the new site can be accessed internally and externally.

reCAPTCHA

If you migrate to a new domain and have setup Email-based_self-registration, you need to create new API-Keys at google. You will find the explanation and links to google in Email-based_self-registration.

See also

Any questions?

Please post in the Installing and upgrading help forum on moodle.org