Now You Can Be the "Hero!" ;) Fix broken images after moving Moodle to a new host. PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Ken Task   
Friday, 15 July 2011 14:49
AddThis Social Bookmark Button

CASE: moved an entire 1.9.x Moodle site from one host to another and the new host has a different hostname.


For this example: oldhost.someisd.net to NEWhostname.someisd.net.
Very likely senario IF virtualizing what used to be a standalone Moodle server and, at the same time, changing the hostname (one desires to keep the old site up for a while).

What gets broke ...
images (in labels and else where) are no longer showing - they display the 'broken image' box.  The files are there in the course files area, but images links are broken.  Ok, easy fix is to edit the resource, view the source, and manually change the URL to the broken image.  That's fine if there's only one or two.
But the entire site is another thing!

This could have been avoided IF one were to edit the SQL dump from the OLD instance in a text editor capable of search and replace BEFORE importing the sql file into the new sites database.  Sometimes, however, that's heavy processing … even for a server.  SQL files in 1.9.x can reach several (even hundreds) Megabytes in size.  Also, editing an SQL dump is also a little risky if one errors with the global search and replace.  The key, of course, anytime one does global search and replace to be VERY EXACTING and run a test first to see what one is finding to replace.

So is there a 'less guru-ish' way?  Well, yes, but it too has some 'dangers'.

There is a hidden, not well known, and 'experimental' script available only to Moodle Administrators for a global Search and Replace for the entire DB that support the instance.

So, images got broke because they now have a URL to them in the DB pointed to:
http://oldhostname.someisd.net AND we changed the sites/DNS to http://NEWhostname.someisd.net

To fix it in one swoop … in each Moodle instance, in the System Admins hidden category and course, make a link to:

http://NEWhostname.someisd.net/[moodle_instance]/admin/replace.php

where [moodle_instance] IS the Moodle you're in and to which your account has admin access rights.
Example, IF, one had a Moodle instance in STEM, then your link there would be:
http://NEWhostname.someisd.net/STEM/admin/replace.php

Have that link popup in a new Window, allowing resize/scrolled.
Then give 'er a run - but do so ONLY AFTER MAKING A BACKUP OF WHAT YA GOT!

Advise making sure one finds exactly what you need to change when replacing.
Best to search for longer strings in this case:

oldhostname.someisd.net and replace with NEWhostname.someisd.net

Those 'dots' are important, spaces are important ... be VERY EXACTING.  No such thing as 'good enough' in this case.

BTW, should you try that replace script remotely, you should be prompted to login first and to actually run it, one must login as an Admin level user.

Here's a little of what it looks like when executing.
Note the following also shows that this utility does find add-ons (lightboxgallery IS an add-on)

(mysql): UPDATE mdl_lightboxgallery SET FOLDER = REPLACE(FOLDER, 'oldhostname.someisd.net', 'NEWhostname.someisd.net')
Success
(mysql): UPDATE mdl_lightboxgallery SET NAME = REPLACE(NAME, 'oldhostname.someisd.net', 'NEWhostname.someisd.net')
Success
(mysql): UPDATE mdl_lightboxgallery SET DESCRIPTION = REPLACE(DESCRIPTION, 'oldhostname.someisd.net', 'NEWhostname.someisd.net')
Success
(mysql): UPDATE mdl_lightboxgallery_captions SET IMAGE = REPLACE(IMAGE, 'oldhostname.someisd.net', 'NEWhostname.someisd.net')
Success
(mysql): UPDATE mdl_lightboxgallery_captions SET CAPTION = REPLACE(CAPTION, 'oldhostname.someisd.net', 'NEWhostname.someisd.net')
Success


Images in LightBox Gallery would use the errant FULL URL to images contained there in.

Truth in advertising
:

Yes, I've tried it on production systems and it does as advertised - as long as it's something simple.
The utility might be only in CVS updates to 1.9.x and not the full downloaded package (haven't checked that out).
See if you have a replace.php file in /MOODLE_INSTANCE/admin/.
The utility is NOT a hack of core code.

YOUR MILEAGE MIGHT VARY!  BACKUP BEFORE YOU PLAY!

And last, but not least, the 'discalmer' … (just like the programmer of the uitility) author of this tip should NOT be held responsible and won't accept responsibility for actions of others! ;)

Last Updated on Friday, 15 July 2011 23:22
 
 
Total Views: 1053