Duplicating moodle to create a new instance

The following are steps to DUPLICATE an existing Moodle on Ubuntu 20.x using PHP 7.4 to make a  NEW instance.

First you have to create a new database for the new moodle instance. Let's assume your new moodle instance is going to be called "copytwo"


If you are moving an instance from one server to another you will have to edit the SQL to replace the hostname. I suggest VIM or VI. Their details are mentioned in step 5. You will need to make a backup copy of the SQL, then edit the SQL, and replace the old hostname with the new hostname.


1. Set up a second MySQL database


After this, set up mysql. Login as root on mysql. It will require you to use your root moodle password. If you can't remember what you set it to, instructions on resetting it are below in step 2.


mysql -u root -p

1.1 First, create the database:

create database copytwo;

When creating the moodle database, you need to set the owner - the user who will have write permissions. Obviously, "someotherpassword" is not a good password, so you must choose a better one. In this case we are giving the user the same name as the database to make it easier to remember (copytwo).

create user 'copytwo'@'localhost' identified with mysql_native_password by 'someotherpassword'; 


grant all on copytwo.* to 'copytwo'@'localhost'; 


flush privileges;

exit; 



Hence, please note, the above creates two entities:

- A USER called copytwo with a password "someotherpassword"

- A DATABASE called copytwo

1.2 If you forget the password for this user, you can change it again like so:

alter user 'copytwo'@'localhost' identified with mysql_native_password by 'someotherpassword';


flush privileges;

exit;


If you are using mysql version 8 or later, you need "with mysql_native_password" otherwise moodle doesn't authenticate the database owner. At least, that was still true at the time of writing this.


If it complains about the password being too weak, it just means it is not strong enough. Try add some punctuation marks to it and make it longer.



2. Resetting root password on mysql [OPTIONAL]


If you can't login as root on mysql, you need to reset the root password. IF you CAN login as root on your mysql system, IGNORE this step.


sudo su

service mysql stop

killall mysqld

killall mysqld

mysqld_safe --skip-grant-tables &

mysql -u root


You will then be inside the mysql command prompt

Type:


use mysql;

ALTER USER 'root'@'localhost' identified WITH mysql_native_password BY  'somepassword';


flush privileges;

exit;


Obviously choose a better password than "somepassword" and make sure you write it down because you will use it a few times later in this process.


You will now be back in bash commandline: type:


service mysql restart




3. COPY Moodle to new instance


Note we are copying an existing moodle (located in /var/www/moodledata and /var/www/html/moodle) to a new copy called moodle_copytwo and moodledata_copytwo


Remember moodle/ folder contains the app, and moodledata/ contains its data. In these steps we are copying the original moodle app into a new folder called copytwo, to make sure that the web URL contains the new instance name, not just the word moodle.


cp -R /var/www/moodledata /var/www/moodledata_copytwo

cp -R /var/www/html/moodle /var/www/html/copytwo

chown -R www-data /var/www/moodledata_copytwo

chmod -R 777 /var/www/moodledata_copytwo

chown -R www-data /var/www/html/copytwo

chmod -R 777 /var/www/html/copytwo



4. Copy the moodle database into the new database:


mysqldump -u root -p moodle > moodle_copytwo.sql


This dumps out the original mysql into a file called moodle_copytwo.sql.

Now you read it back into mysql into the new database. That might take up to half an hour so be patient.


mysql -u root -p copytwo < moodle_copytwo.sql


This will effectively copy the entire database, settings AND USERS from the original moodle instance to the new one. If you do not want to keep those users, login to moodle and delete them.



5. Edit the moodle config file (VERY IMPORTANT)


You MUST do this step or you will end up logging your new moodle users into the original instance rather than the new instance.


Edit /var/www/html/copytwo/config.php using any editor you are comfortable with; I suggest VIM/Vi.


If you do not know how to use VIM or Vi, I suggest you look at this cheat sheet: https://www.atmos.albany.edu/daes/atmclasses/atm350/vi_cheat_sheet.pdf


Replace the word "moodle" under the "user" entry and the database name entry with "copytwo" as needed. Note that the below exerpt does NOT show the entire config file, JUST the lines you must change. Changes are marked in RED. We change the LOCATION of the moodledata and moodle folder AND the database username and password


Original file:


$CFG->dbtype    = 'mysqli';

$CFG->dblibrary = 'native';

$CFG->dbhost    = 'localhost';

$CFG->dbname    = 'moodle';

$CFG->dbuser    = 'moodle';

$CFG->dbpass    = 'somepassword';

$CFG->prefix    = 'mdl_';

$CFG->dboptions = array (

  'dbpersist' => 0,

  'dbport' => '',

  'dbsocket' => '',

  'dbcollation' => 'utf8mb4_unicode_ci',

);


$CFG->wwwroot   = 'https://my.web.address/moodle/;

$CFG->dataroot  = '/var/www/moodledata/';

$CFG->admin     = 'admin';


Edited:


$CFG->dbtype    = 'mysqli';

$CFG->dblibrary = 'native';

$CFG->dbhost    = 'localhost';

$CFG->dbname    = 'copytwo';

$CFG->dbuser    = 'copytwo';

$CFG->dbpass    = 'someotherpassword';

$CFG->prefix    = 'mdl_';

$CFG->dboptions = array (

  'dbpersist' => 0,

  'dbport' => '',

  'dbsocket' => '',

  'dbcollation' => 'utf8mb4_unicode_ci',

);


$CFG->wwwroot   = 'https://my.web.address/copytwo/;

$CFG->dataroot  = '/var/www/moodledata_copytwo/';

$CFG->admin     = 'admin';


5.b. Migrating to another host


If you are moving an instance from one server to another, you will have to edit the SQL to replace the hostname. You will need to make a backup copy of the SQL, then edit the SQL, and replace the old hostname with the new hostname. If you fail to do this step, the new moodle instance will persist in trying to contact the old server.


# assuming the original SQL file is called original.sql

# and assuming my original webserver was my.originalserver.com and the new server is called my.newserver.com


cp original.sql original-backup.sql

vi original.sql 

:%s/my.originalserver.com/my.newserver.com/g

:wq!

mysql -u root copytwo < original.sql


That last step, importing the SQL, takes some time. And the above assumes you are importing the original SQL with the URL changed, into the new database called copytwo.


6. Final setup and configuration


Go to your website in your web browser. Let's say for argument's sake that you are installing this on your OWN server. You would then go to :


https://127.0.0.1/copytwo/


If however your website is www.my.web.address then you go to :


https://www.my.web.address/copytwo/


By the way: The part /copytwo/ means the folder moodle that we copied earlier in step 3, and which is now located inside /var/www/html/copytwo/ on the server. Anything on your server that is inside the /var/www/html/ folder will be accessible via: http://127.0.0.1/

If it complains about permissions and says that moodledata is insecure, you can try two things:


6.1. Use /var/moodledata instead of /var/www/moodledata:


mv /var/www/moodledata_copytwo/ /var/


AND/OR


6.2. Set permissions on that folder:


chown -R www-data:www-data /var/moodledata_copytwo/ /var/www/moodledata_copytwo/

chmod -R 775 /var/www/moodledata_copytwo /var/moodledata_copytwo

If I have missed any steps or you encountered any errors, let me know.


Advice


I suggest that you keep a freshly-installed copy of moodle data, database SQL and appliction, in a template folder, for future use. The reason is that as each instance gets full of data, it will get bigger and bigger, and take longer and longer to copy. 


To do this, copy /var/www/moodle; /var/www/moodledata; and the SQL, into a template folder. 


Popular posts from this blog

could not find course/ could not find top level course/ could not find course category

migrating moodle from one server to another

installing moosh to make your life easier