1<?php 2// This file is part of Moodle - http://moodle.org/ 3// 4// Moodle is free software: you can redistribute it and/or modify 5// it under the terms of the GNU General Public License as published by 6// the Free Software Foundation, either version 3 of the License, or 7// (at your option) any later version. 8// 9// Moodle is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU General Public License for more details. 13// 14// You should have received a copy of the GNU General Public License 15// along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17/** 18 * Adhoc task that performs asynchronous backups. 19 * 20 * @package core 21 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25namespace core\task; 26 27use async_helper; 28 29defined('MOODLE_INTERNAL') || die(); 30 31require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); 32require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php'); 33 34/** 35 * Adhoc task that performs asynchronous backups. 36 * 37 * @package core 38 * @copyright 2018 Matt Porritt <mattp@catalyst-au.net> 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41class asynchronous_backup_task extends adhoc_task { 42 43 /** 44 * Run the adhoc task and preform the backup. 45 */ 46 public function execute() { 47 global $DB; 48 $started = time(); 49 50 $backupid = $this->get_custom_data()->backupid; 51 $backuprecordid = $DB->get_field('backup_controllers', 'id', array('backupid' => $backupid), MUST_EXIST); 52 mtrace('Processing asynchronous backup for backup: ' . $backupid); 53 54 // Get the backup controller by backup id. 55 $bc = \backup_controller::load_controller($backupid); 56 $bc->set_progress(new \core\progress\db_updater($backuprecordid, 'backup_controllers', 'progress')); 57 58 // Do some preflight checks on the backup. 59 $status = $bc->get_status(); 60 $execution = $bc->get_execution(); 61 62 // Check that the backup is in the correct status and 63 // that is set for asynchronous execution. 64 if ($status == \backup::STATUS_AWAITING && $execution == \backup::EXECUTION_DELAYED) { 65 // Execute the backup. 66 $bc->execute_plan(); 67 68 // Send message to user if enabled. 69 $messageenabled = (bool)get_config('backup', 'backup_async_message_users'); 70 if ($messageenabled && $bc->get_status() == \backup::STATUS_FINISHED_OK) { 71 $asynchelper = new async_helper('backup', $backupid); 72 $asynchelper->send_message(); 73 } 74 75 } else { 76 // If status isn't 700, it means the process has failed. 77 // Retrying isn't going to fix it, so marked operation as failed. 78 $bc->set_status(\backup::STATUS_FINISHED_ERR); 79 mtrace('Bad backup controller status, is: ' . $status . ' should be 700, marking job as failed.'); 80 81 } 82 83 // Cleanup. 84 $bc->destroy(); 85 86 $duration = time() - $started; 87 mtrace('Backup completed in: ' . $duration . ' seconds'); 88 } 89} 90 91