# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/admin/cli/automated_backups.php
--- moodle/admin/cli/automated_backups.php No Base Revision
+++ moodle/admin/cli/automated_backups.php Locally New
@@ -0,0 +1,100 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Automated backups CLI cron
+ *
+ * This script executes
+ *
+ * @package    core
+ * @subpackage cli
+ * @copyright  2010 Sam Hemelryk
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+define('CLI_SCRIPT', true);
+
+require(dirname(dirname(dirname(__FILE__))).'/config.php');
+require_once($CFG->libdir.'/clilib.php');      // cli only functions
+require_once($CFG->libdir.'/cronlib.php');
+
+// now get cli options
+list($options, $unrecognized) = cli_get_params(array('help'=>false),
+                                               array('h'=>'help'));
+
+if ($unrecognized) {
+    $unrecognized = implode("\n  ", $unrecognized);
+    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
+}
+
+if ($options['help']) {
+    $help =
+"Execute automated backups.
+
+This script executes automated backups completely and is designed to be
+called via cron.
+
+Options:
+-h, --help            Print out this help
+
+Example:
+\$sudo -u www-data /usr/bin/php admin/cli/cron.php
+";
+
+    echo $help;
+    die;
+}
+if (CLI_MAINTENANCE) {
+    echo "CLI maintenance mode active, cron execution suspended.\n";
+    exit(1);
+}
+
+if (moodle_needs_upgrading()) {
+    echo "Moodle upgrade pending, cron execution suspended.\n";
+    exit(1);
+}
+
+require_once($CFG->libdir.'/adminlib.php');
+require_once($CFG->libdir.'/gradelib.php');
+
+if (!empty($CFG->showcronsql)) {
+    $DB->set_debug(true);
+}
+if (!empty($CFG->showcrondebugging)) {
+    $CFG->debug = DEBUG_DEVELOPER;
+    $CFG->debugdisplay = true;
+}
+
+$starttime = microtime();
+
+/// emulate normal session
+cron_setup_user();
+
+/// Start output log
+$timenow  = time();
+
+mtrace("Server Time: ".date('r',$timenow)."\n\n");
+
+// Run automated backups if required.
+require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
+require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
+backup_cron_automated_helper::run_automated_backup(backup_cron_automated_helper::RUN_IMMEDIATLY);
+
+mtrace("Automated cron backups completed correctly");
+
+$difftime = microtime_diff($starttime, microtime());
+mtrace("Execution took ".$difftime." seconds");
\ No newline at end of file
Index: moodle/admin/settings/courses.php
--- moodle/admin/settings/courses.php Base (1.50)
+++ moodle/admin/settings/courses.php Locally Modified (Based On 1.50)
@@ -109,7 +109,11 @@
 
 /// "backups" settingpage
     $temp = new admin_settingpage('automated', get_string('automatedsetup','backup'), 'moodle/backup:backupcourse');
-    $temp->add(new admin_setting_configcheckbox('backup/backup_auto_active', get_string('active'), get_string('backupactivehelp'), 0));
+    $temp->add(new admin_setting_configselect('backup/backup_auto_active', get_string('active'),  get_string('autoactivedescription', 'backup'), 0, array(
+        0 => get_string('autoactivedisabled', 'backup'),
+        1 => get_string('autoactiveenabled', 'backup'),
+        2 => get_string('autoactivemanual', 'backup')
+    )));
     $temp->add(new admin_setting_special_backupdays());
     $temp->add(new admin_setting_configtime('backup/backup_auto_hour', 'backup_auto_minute', get_string('executeat'),
             get_string('backupexecuteathelp'), array('h' => 0, 'm' => 0)));
Index: moodle/backup/util/helper/backup_cron_helper.class.php
--- moodle/backup/util/helper/backup_cron_helper.class.php Base (1.1)
+++ moodle/backup/util/helper/backup_cron_helper.class.php Locally Modified (Based On 1.1)
@@ -47,13 +47,21 @@
     /** Course automated backup was skipped */
     const BACKUP_STATUS_SKIPPED = 3;
 
+    /** Run if required by the schedule set in config. Default. **/
+    const RUN_ON_SCHEDULE = 0;
+    /** Run immediatly. **/
+    const RUN_IMMEDIATLY = 1;
 
+    const AUTO_BACKUP_DISABLED = 0;
+    const AUTO_BACKUP_ENABLED = 1;
+    const AUTO_BACKUP_MANUAL = 2;
+
     /**
      * Runs the automated backups if required
      *
      * @global moodle_database $DB
      */
-    public static function run_automated_backup() {
+    public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) {
         global $CFG, $DB;
 
         $status = true;
@@ -61,15 +69,19 @@
         $now = time();
 
         mtrace("Checking automated backup status",'...');
-        $state = backup_cron_automated_helper::get_automated_backup_state();
+        $state = backup_cron_automated_helper::get_automated_backup_state($rundirective);
         if ($state === backup_cron_automated_helper::STATE_DISABLED) {
             mtrace('INACTIVE');
-            return true;
+            return $state;
         } else if ($state === backup_cron_automated_helper::STATE_RUNNING) {
             mtrace('RUNNING');
-            mtrace("automated backup seems to be running. Execution delayed");
-            return true;
+            if ($rundirective == self::RUN_IMMEDIATLY) {
+                mtrace('automated backups are already. If this script is being run by cron this constitues an error. You will need to increase the time between executions within cron.');
         } else {
+                mtrace("automated backup are already running. Execution delayed");
+            }
+            return $state;
+        } else {
             mtrace('OK');
         }
         backup_cron_automated_helper::set_state_running();
@@ -116,8 +128,9 @@
                 if (empty($course->visible) && ($now - $course->timemodified) > 31*24*60*60) {  //Hidden + unmodified last month
                     $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED;
                     $DB->update_record('backup_courses', $backupcourse);
+                    mtrace('Skipping unchanged course '.$course->fullname);
                     $skipped = true;
-                } else if ($backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now) {
+                } else if (($backupcourse->nextstarttime >= 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATLY) {
                     mtrace('Backing up '.$course->fullname, '...');
 
                     //We have to send a email because we have included at least one backup
@@ -408,11 +421,12 @@
      * @global moodle_database $DB
      * @return int One of self::STATE_*
      */
-    public static function get_automated_backup_state() {
+    public static function get_automated_backup_state($rundirective = self::RUN_ON_SCHEDULE) {
         global $DB;
 
         $config = get_config('backup');
-        if (empty($config->backup_auto_active)) {
\ No newline at end of file
+        $active = (int)$config->backup_auto_active;
+        if ($active === self::AUTO_BACKUP_DISABLED || ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL)) {
\ No newline at end of file
             return self::STATE_DISABLED;
         } else if (!empty($config->backup_auto_running)) {
             // TODO: We should find some way of checking whether the automated
Index: moodle/lang/en/backup.php
--- moodle/lang/en/backup.php Base (1.29)
+++ moodle/lang/en/backup.php Locally Modified (Based On 1.29)
@@ -23,6 +23,10 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+$string['autoactivedisabled'] = 'Disabled';
+$string['autoactiveenabled'] = 'Enabled';
+$string['autoactivemanual'] = 'Manual';
+$string['autoactivedescription'] = 'Choose whether or not to do automated backups. If manual is selected automated backups will be possible only by through the automated backups CLI script. This can be done either manually on the command line or through cron.';
 $string['automatedbackupschedule'] = 'Schedule';
 $string['automatedbackupschedulehelp'] = 'Choose which days of the week to perform automated backups.';
 $string['automatedbackupsinactive'] = 'Automated backups haven\'t been enabled by the site admin';
