backup MDL-25183 Added a dedicated CLI script for running automated backups
authorSam Hemelryk <sam@moodle.com>
Fri, 12 Nov 2010 07:05:18 +0000 (07:05 +0000)
committerSam Hemelryk <sam@moodle.com>
Fri, 12 Nov 2010 07:05:18 +0000 (07:05 +0000)
admin/cli/automated_backups.php [new file with mode: 0644]
admin/settings/courses.php
backup/util/helper/backup_cron_helper.class.php
lang/en/backup.php

diff --git a/admin/cli/automated_backups.php b/admin/cli/automated_backups.php
new file mode 100644 (file)
index 0000000..8126c5a
--- /dev/null
@@ -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/automated_backups.php
+";
+
+    echo $help;
+    die;
+}
+if (CLI_MAINTENANCE) {
+    echo "CLI maintenance mode active, backup execution suspended.\n";
+    exit(1);
+}
+
+if (moodle_needs_upgrading()) {
+    echo "Moodle upgrade pending, backup 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 d8c7278..d436a31 100644 (file)
@@ -109,7 +109,11 @@ if ($hassiteconfig
 
 /// "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 03d509d..387cd47 100644 (file)
@@ -47,13 +47,21 @@ abstract class backup_cron_automated_helper {
     /** 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,14 +69,18 @@ abstract class backup_cron_automated_helper {
         $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');
         }
@@ -116,8 +128,9 @@ abstract class backup_cron_automated_helper {
                 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 @@ abstract class backup_cron_automated_helper {
      * @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)) {
+        $active = (int)$config->backup_auto_active;
+        if ($active === self::AUTO_BACKUP_DISABLED || ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL)) {
             return self::STATE_DISABLED;
         } else if (!empty($config->backup_auto_running)) {
             // TODO: We should find some way of checking whether the automated
index 809077f..c85262f 100644 (file)
  * @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';