Where in Moodle code uses 'du' or other /usr/bin/ shell scripts

Where in Moodle code uses 'du' or other /usr/bin/ shell scripts

by Ken Task -
Number of replies: 2
Picture of Particularly helpful Moodlers

First, am not a developer ... more like a 'wanniebe' ... but thought this forum the 'best' place to ask ...

In moodle there is a settings script for System Paths.
One of which is du (disk usage) and the comment under the box says:

"Path to du. Probably something like /usr/bin/du. If you enter this, pages that display directory contents will run much faster for directories with a lot of files"

Have installed an add-on for course sizes and it uses du.  When using 'course sizes' have been watching live processes on a linux box with top and have seen du 'kick in'.

The question:

I'd like to inspect any moodle code that uses du.  Where can I find that code?

Reason: I'd like to work on a local plugin (not an official Moodle local plugin, but a 'home grown' one that doesn't hack moodle core code) that allows a moodle user to take a 'snapshot' of the moodle code and get a mysqldump. 

Extending the reason why ...

There are a few things which, IMHO, Moodle desperately needs to address.  Among those, taking a 'snapshot' of the code directory and DB dump via Moodle Admin UI screens.

Why?  Let's take, for example, a 'sandbox' Moodle implmentation that's purpose is to try out plugins and addons (not a production site).  When one steps through acquiring an addon and is about to install, one hits an 'Acknowledgement' screen which says:

"I understand that it is my responsibility to have full backups of this site prior to installing additional plugins. I accept and understand that plugins (especially but not only those originating in unofficial sources) may contain security holes, can make the site unavailable, or cause private data leaks or loss."

That's a fair warning and is justified.

But, for noobies, that is also daunting, to say the least.  'Roll the dice' and 'go for it'!

What if there was a link <a href="site/local/snapshot/" target="_new">RUN SNAPSHOT</a> that ran a php exec bash shell script which backs up the code directory AND does a mysqldump of the DB?  A 'snapshot'!!!

The user in the sandbox then is assured that no matter what they install, the sandbox can be restored to the point just before they began to 'play'.

Actually, I have such a local plugin (uses php exec) and have tested it ... it works, but I'd like to investigate just how difficult it might be to bring it into Moodle as a 'valid'/true local plugin.

Thanks, in advance, for any pointers.

'spirit of sharing', Ken

Average of ratings: -
In reply to Ken Task

Re: Where in Moodle code uses 'du' or other /usr/bin/ shell scripts

by Darko Miletić -

To respond directly to your question (provided that you have OS with grep cli). The setting name is called pathtodu so we search for that:

grep -r -n --include=*.php "pathtodu"

This gives an output among which are these lines:

lib/moodlelib.php:6358:    if (!empty($CFG->pathtodu) && is_executable(trim($CFG->pathtodu))) {
lib/moodlelib.php:6359: $command = trim($CFG->pathtodu).' -sk '.escapeshellarg($rootdir);

As for your idea it is an interesting thought but it can also be resolved with fairly simple shell script tied to cron.


Average of ratings: Useful (2)
In reply to Darko Miletić

Re: Where in Moodle code uses 'du' or other /usr/bin/ shell scripts

by Ken Task -
Picture of Particularly helpful Moodlers

Thanks, Darko!  That's a handy tip for finding something in code!!! ;)

Got a mac (host on linux - tinker on the mac) and for those that might be looking at this and using Darko's tip on using grep, Apple has evidently compiled grep differently.    So after finding other references on the net used this:

grep -r -n --include=*.php "pathtodu" ./

added the ./ at the end to avoid 'grep: warning: recursive search of stdin' and getting hung.

while inside a moodle30 code directory/folder.

Found the same lines ...

So the moodle script does run the OS du only with escapeshellarg and a built in PHP routine for get_size_something.

And I agree, the 'snapshot' could be run via OS cron job - daily snapshot of the code and DB ... could even add an rsync for the data directory.    Do that already for a daily backup on some servers.

I was talking about 'convenence' (or for 'lazy true server admins) of having a link right on those screens that tend to deter noobie Moodle Admins.

snapshot link in Ack

There's a similar screen when one updates a plugin.   And I've had 'noobie' Moodle admins almost refuse to go forward with updating plugins for fear of messing things up  (that has happened).   Having a 'snapshot' link right there as well would encourage them to 'move forward' (as well as improve the 'damaged trust' issues they have for the code).

Just my two cents, of course ... but will continue and share when I've written up some info on how 2 ... at least with the 'home grown' local/snapshot/ 'semi-plugin' for the 'risk takers'.

The bash shell script I have (and anyone could create), BTW, requires no input by the user (clicker).  There's no dialog box for user input.  So it doesn't need to have PHP protecting the system by escapeshellarg.

Thanks again, Darko!

'spirit of sharing', Ken