Moodle migration from one server to another while upgrading to a new moodle version
Enable maintenance mode before doing anything. This will make sure that there is no data lose and also inform users that the site will not be available. Plan the migration steps before starting the migration which will speed up the process and also reduce the down time. For future reference these are the steps that have to be followed.
Change DNS Nameservers
If you are hosting in a vm and changed the IP update the ip pointing to the server. Later on when configuring certbot it is necessary to have the new IP for the domain.
Preparing the old System
We have to backup these folders and database. Change the paths according to where you have installed moodle. In this case moodle in installed as a default portal for the system in /var/www/html, moodledata can anywhere this is just an example.
- /var/www/html
- /var/www/moodledata
- database.sql Create a folder to copy all of them to one location. In this example we will create a folder called backup and copy all the files and folders to it. ‘~/’ is for home directory of the user in this case it is root.
mkdir ~/backup/
rsync -avz --progress --numeric-ids /var/www/html ~/backup
rsync -avz --progress --numeric-ids /var/www/moodledata ~/backup
mysqldump -u <dbuser> -p <dbname> > database.sql
for mysqldump it will ask for a password give the password of the database used in config.php file
Preparing the new System
The following setup is for Debian 10 adjust package names and other options accordingly to a different OS. Install all the requirments as suggested by moodle. For convenience use this command which will install all the packages
apt install apache2 mariadb-server php php-mysql php-mbstring php-curl php-tokenizer php-xmlrpc php-soap php-common php-zip php-gd php-xml php-intl php-json
Now transfer the files from old system to the new one
mkdir ~/imported/
rsync -avz --progress --numeric-ids user:OLDSERVER-IP:backup imported
Create mariadb database and user then import data to the new db. login to mariadb prompt by given the command ‘mysql’.
CREATE DATABASE databasename;
GRANT ALL PRIVILEGES ON databasename.* TO "username"@"hostname" IDENTIFIED BY "password";
FLUSH PRIVILEGES;
Import database
mysql -u <dbuser> -p <dbname < database.sql
Move moodledata to new location
cp -r ~/imported/moodledata /var/www/
chown -R www-data.www-data /var/www/html
Download or clone the latest version from git repo, follow this guide into /var/www/html. At this point you may need to copy some plugins from old installation which you can do once you have started the upgrade. The upgrade page will show what are the missing plugins and you can copy them from back to the new instance.
Update config.php to reflect new paths and in case of new database add those changes.
example config.php
For updating mysql to give 4 byte support follow this link.
<?php // Moodle configuration file
unset($CFG);
global $CFG;
$CFG = new stdClass();
$CFG->dbtype = 'mariadb';
//$CFG->dbtype = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost = 'localhost';
$CFG->dbname = '<dbname>';
$CFG->dbuser = '<dbuser>';
$CFG->dbpass = '<dbpass>';
$CFG->prefix = 'mdl_';
$CFG->dboptions = array (
'dbpersist' => 0,
'dbport' => '',
'dbsocket' => '',
'dbcollation' => 'utf8mb4_unicode_ci', #<-- this is optional for storing emoji's in db. to get it check mysql configuration and update it.
);
$CFG->wwwroot = '<url>'; #<-- This url is important that what ever we give here either url or ip is used to access the portal
$CFG->dataroot = '/var/www/moodledata'; #<-- Change this if data is in a different location
$CFG->admin = 'admin';
$CFG->directorypermissions = 0777;
require_once(dirname(__FILE__) . '/lib/setup.php');
// There is no php closing tag in this file,
// it is intentional because it prevents trailing whitespace problems!
?>
Upgrade the server from web interface
Once all the files are in place open site url. The site will show upgrade page. Once you continue it will show all the plugins that will be upgraded, installed, deleted. The once which are shown ‘missing from disk’ copy them from the back to the new instance and reload. Once the upgrade is done disable maintenance mode.
For reference these where missing files and plugins from the instance that we where upgrading. ~/imported being the root of moodle.
~/imported/learning/mod/attendance/
~/imported/learning/filter/wiris/
~/imported/learning/lib/editor/atto/plugins/wiris/ atto/plugins/
~/imported/learning/lib/editor/tinymce/plugins/tiny_mce_wiris/
~/imported/learning/auth/a2fa/
~/imported/learning/user/profile/field/afaqr/
~/imported/learning/report/benchmark/
chown -R www-data.www-data /var/www/html