General developer forum

Questions concerning development of a local plugin for taking a 'snapshot' site backup

 
Picture of Ken Task
Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Particularly helpful Moodlers

First, am not a developer ... but exploring ...

Related to https://moodle.org/mod/forum/discuss.php?d=326197

Brief background: the local plugin calls a bash shell script which executes tar on code directory and a mysqldump on the database for Moodle to a directory outside of moodledata.

This would be linked from the screens via language edits that would link the word 'snapshot' to the local plugin to perform a 'snapshot' backup of code and DB so that Moodle admins (just Moodle UI) could follow the recommended best practice of backing up code and DB before installing any new addon.

From previous experience as a semi-developer for FMPro am aware that what is done needs protections such that only certain users can execute, thus the following question:

Restrict access to the 'snapshot' local plugin where one must be logged on (session key?) and be an admin level user only?

Have found, in other existing scripts, references for such sorts of restrictions.  The following an example:

require_once("../config.php");
require_once($CFG->libdir.'/adminlib.php');
require_login();
if (!has_capability('moodle/site:config', context_system::instance())) {
    header('HTTP/1.1 403 Forbidden');
    die();
}

The rest of the .php page would be related to the bash shell script performing the snapshot backup.

Are there other libraries that need be loaded/required to assure the user is admin level?   Any certain order in loading them?  If so, which prey tell! ;)

Thanks, in advance, Ken


 
Average of ratings: -
Picture of Ken Task
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Particularly helpful Moodlers

Well, guess I'll answer my own ... just in case anyone else would be interested in providing a way to take a snapshot backup on demand ...

The line:

require_login();

Does force user to login.  So the running of the bash shell script is protected somewhat.   Would like to restrict to admin level users only, however.

Found this line in other code:

if (!has_capability('moodle/site:config', context_system::instance())) {
    header('HTTP/1.1 403 Forbidden');
    die();
}

added to script, appears not to have caused any issue/failure if I login as an admin level, but in attempting to login as a user that is not an admin, get this in apache error logs:

PHP Fatal error:  Uncaught Error: Call to undefined function has_capability(
) in ... script

Where is that function 'has_capibility'?  Do I need to load another library?  or change or remove the context part as there really is no context (I don't think).

Thanks in advance, for any hints/clues.

Ken


 
Average of ratings: -
Picture of Ken Task
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Particularly helpful Moodlers

OK, got it so no user below admin level can access now.

Now the next step ...

How does one pass variables from config.php for the DB user, password, and database to a bash shell script?

Thanks, in advance, for any hints clues.

Ken

 
Average of ratings: -
Picture of Ken Task
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Particularly helpful Moodlers

Well, progress ... no bash shell script required ... it's all in PHP now.   Discovered one can concactate the needed $CFG->db---- variables from config.php into another PHP script ... lots of 'ticks' and 'dots' to account for ... but it works.

Got it working on 3.1,3.2, and 3.3.

Not a true plugin .... just enough to meet Moodle requirements for versions.

May not be for everyone .... but suits my needs ... now I hope the Moodle Admins will at least be more at ease if they know there is a backup a code and a DB dump.

Is it secure?   Think so.  Tested and one must be: 1) logged on 2) a Site Admin level user to access or one gets a 'white screen of death'. ;)

Now to go tinker with an 'update' script (built same way) which uses git to update a moodle within a series. ;)

'spirit of sharing', Ken


 
Average of ratings: -
Picture of Nicolas Martignoni
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developersGroup TestersGroup Translators

Hi Ken,

Sorry for the delay. I did it this my on my own admin tool plugin: https://github.com/martignoni/moodle-tool_moodlebox/blob/master/index.php

Good luck!

Nicolas

 
Average of ratings: -
Picture of Ken Task
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Particularly helpful Moodlers

Thanks for sharing.   Looks interesting ... but for a Rasberry only and really for bootup/shutdown of the server itself (from what I can gather).   Sill though, might be able to pick up  some ideas from it.

Again ... thanks for sharing! ;)

'spirit of sharing', Ken


 
Average of ratings: -
Picture of Nicolas Martignoni
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developersGroup TestersGroup Translators

Hi Ken,

Yes indeed. I was sharing the way (copied from core plugins, BTW) to control admin access to a plugin (at the beginning of the linked file). Anything after line 46 may not be relevant for this discussion.

Ciao, Nicolas

 
Average of ratings: -
Picture of Ken Task
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Particularly helpful Moodlers

Thanks ... here's what I have prior to the shell_exec lines

require_once('../../config.php');
require_once('../../lib/adminlib.php');
require_login();
if (!has_capability('moodle/site:config', context_system::instance())) {
    header('HTTP/1.1 403 Forbidden');
    die();
}

The snapshot plugin isn't in the menus of Moodle at all ... like some admin/tools/ ... one has to know it exist and direct the browser there.    Am using language edits to find screens admin level users would see that suggest before proceeding, they should take a backup of the site - link to snapshot pops up on new window and executes ... there is no interaction.  For Admin level users I normally create a System Admin category and a System Admins course where special info and links reside - one of which is now 'snapshot'.

Tested by logging on as a student account ... ie,, not admin level ... and then using URL line tried to go to local/snapshot/.   Got a 'white screen' ... which is ok by me.   Someone or bot exploring the moodle doesn't need any help nor clue. ;)

Don't imagine that snapshot could be used by those who remotely host on shared systems but those that have VPS (Linux) might find it handy.   IF an Admin level takes the pre-caution .... they should ... to make a backup of code and a DB dump just prior to installing a plugin or updating the site, right now, without snapshot, they have to use command line or access at least two apps on their systems ... something for db backups and whatever they have for managing files.    That's a two stepper.    Folks I work with seem too busy to venture off the Moodle admin screens with another browser instance. :\

'spirit of sharing', Ken


 
Average of ratings: -
Picture of Bobby Siegfried
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
 

Hi Ken,

Thanks for sharing all of this. I have actually been looking at ways to do some of this as well - although I haven't given much thought to doing it as a plugin executable from the GUI. With a site as large as ours, I'm always afraid of apache timing out before the job would complete. I've been working with Moosh (command-line tools) to do some actions, but currently Moosh commands are limited (only sql dump is available to address the tasks you are looking to do).

Another possible suggestion: have you looked at getting your Moodle instance into version-control? We are moving toward using Git which will alleviate the need to snapshot the moodle directory. In this scenario, we just need to capture an SQL dump, assuming moodledata is already being backed up regularly.

I'm happy to share my code as well.

 
Average of ratings: -
Picture of Ken Task
Re: Questions concerning development of a local plugin for taking a 'snapshot' site backup
Group Particularly helpful Moodlers

Size of site matters ... so you are wise to be concerned about using apache.  The sites I admin could be considered large *IF* one considered number of active users alone.   It's more a combination of factors, one of which is usage.   A K12 entity that has issued devices to all students and teachers have students use those devices pointed at moodle takes some heavy hits during the day.

All sites I maintain were installed with git and are maintained by git.   Have been doing the updates and upgrades via bash shell with combo of git and the scripts that reside in admin/cli/.   Even have those bash shell scripts to update or upgrade in Webmin's 'custom commands' for easy one click's.

The Moodle Admins of those sites care not to use command line ... some like to play with addons/plugins in their production server, even if I've attempted to discourage and have even setup a sandbox for them to try stuff out ... they still use production.   They even have limited access to Webmin's Custom commands and still won't take the time to do that! :\   So that's really the issue am trying to cover with snapshot.   They might click on a link in Moodle!!!!

Have used git only to update and upgrade .... never to 'roll back' a site.

By all means, share what you'd care to share ...

Funny, just today got a notice of an update available for a plugin.   Server using mdeploy and is a 3.1.highest .... the update was for a 3.2 or 3.3 and the update failed to pass checking (mdeploy bug?) 

Didn't see the screen seen before in previous versions of Moodle that suggested taking a snapshot.     So maybe this is all for nothing. :\

'spirit of sharing', Ken

 
Average of ratings: -