Commit 8e1cd180 authored by Guillaume Boudrias's avatar Guillaume Boudrias
Browse files

Issue #2154813 by ergonlogic, helmo: Add hosting_backup_window module

parent 9428a8e0
......@@ -35,9 +35,16 @@ function hosting_backup_queue_hosting_queues() {
*/
function hosting_backup_queue_queue($count) {
$sites = _hosting_backup_queue_get_outstanding_backups($count);
if (module_exists('hosting_backup_window')) {
if (!hosting_backup_window_is_allowed_time_window()) {
return;
}
}
foreach ($sites as $site_id) {
if ($task = _hosting_backup_queue_get_backup_task($site_id)) {
drush_invoke_process('@self', "hosting-task", array($task->nid), array('strict' => FALSE), array('fork' => TRUE));
drush_invoke_process('@self', "hosting-task", array($task->nid), array('strict' => FALSE), array('fork' => TRUE));
}
}
}
......
<?php
/**
* @file
* The hosting feature definition for the backup window.
*/
/**
* Register a hosting feature with Aegir.
*
* This will be used to generate the 'admin/hosting' page.
*
* @return
* associative array indexed by feature key.
*/
function hosting_backup_window_hosting_feature() {
$features['backup_window'] = array(
// title to display in form
'title' => t('Backup window'),
// description
'description' => t('Allows backups to be restricted to certain time periods.'),
// initial status ( HOSTING_FEATURE_DISABLED, HOSTING_FEATURE_ENABLED, HOSTING_FEATURE_REQUIRED )
'status' => HOSTING_FEATURE_DISABLED,
// module to enable/disable alongside feature
'module' => 'hosting_backup_window',
// associate with a specific node type.
// 'node' => 'nodetype',
// which group to display in ( null , experimental )
'group' => 'experimental'
);
return $features;
}
name = Backup window
description = Allows restricting the backup queue to only run at certain times
core = 6.x
package = Hosting
dependencies[] = hosting_site
dependencies[] = hosting_site_backup_manager
dependencies[] = hosting_backup_queue
<?php
// if (hosting_backup_window_is_allowed_time_window()) {
/**
* Checks whether we are currently within the allowed time window for backups.
*
* @TODO : Consider nightly backups.
*/
function hosting_backup_window_is_allowed_time_window($time = NULL) {
if ($time === NULL) {
$time = time();
}
$today = date("D", time());
$start_time = variable_get('hosting_backup_window_time_window_start', '00:00');
$end_time = variable_get('hosting_backup_window_time_window_end', '23:59');
// Date field shenanigans
if (empty($start_time)) {
$start_time = strtotime('00:00'); // Midnight "this morning"
}
else {
$start_time = strtotime($start_time);
}
if (empty($end_time)) {
$end_time = strtotime('23:59'); // Midnight "this morning"
}
else {
$end_time = strtotime($end_time);
}
/*
dsm('Evaluated time: ' . date('m/d/Y h:i:s a', time()));
dsm('Start time : ' . date('m/d/Y h:i:s a', $start_time));
dsm('End time : ' . date('m/d/Y h:i:s a', $end_time));
dsm('Today: ' . $today );
*/
if ($time < $start_time) {
// Before the start time
return FALSE;
}
if ($time > $end_time) {
// After the end time
return FALSE;
}
$allowed_days_array = variable_get('hosting_backup_window_time_window_days', NULL);
// All days allowed by default, don't process if variable is not set
if ($allowed_days_array !== NULL) {
if (!in_array($today, $allowed_days_array)) {
return FALSE;
}
}
return TRUE;
}
/**
* Returns the backup task to run for a given site.
*/
function _hosting_backup_window_get_backup_task($site) {
// Ensure that we have a backup task to run
return hosting_add_task($site, 'backup', array('description' => t('Automated backup')));
}
/**
* Implementation of hook_form_alter()
*/
function hosting_backup_window_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'hosting_site_backup_manager_settings' && user_access('administer hosting backup queue')) {
$days_array = drupal_map_assoc(array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'));
$server_time_message = t('The current server time is @time', array('@time' => date("H:i")));
$form['backup_window'] = array(
'#type' => 'fieldset',
'#title' => t('Backup window'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -10,
);
$form['backup_window']['backup_window_start'] = array(
'#type' => 'textfield',
'#title' => t('Start doing automated backups at'),
'#description' => t('Format: 00:00. ') . $server_time_message,
'#default_value' => variable_get('hosting_backup_window_time_window_start', '00:00'),
'#maxlength' => 5,
'#size' => 6,
);
$form['backup_window']['backup_window_end'] = array(
'#type' => 'textfield',
'#title' => t('Stop doing automated backups'),
'#description' => t('Format: 23:59. ') . $server_time_message,
'#default_value' => variable_get('hosting_backup_window_time_window_end', '23:59'),
'#maxlength' => 5,
'#size' => 6,
);
$form['backup_window']['backup_window_days'] = array(
'#title' => t('Only do backups on those days'),
'#type' => 'select',
'#options' => $days_array,
'#multiple' => TRUE,
'#size' => 7,
'#default_value' => variable_get('hosting_backup_window_time_window_days', $days_array),
);
$form['#validate'][] = 'hosting_backup_window_time_window_form_validate';
$form['#submit'][] = 'hosting_backup_window_time_window_form_submit';
}
}
function hosting_backup_window_time_window_form_validate(&$form, &$form_state) {
$date_format = 'H:i';
$values = $form_state['values'];
if (!empty($values['backup_window_start'])) {
if (strtotime($values['backup_window_start']) == 0) {
form_set_error('backup_window_start', t('Invalid start time'));
}
}
if (!empty($values['backup_window_end'])) {
if (strtotime($values['backup_window_end']) == 0) {
form_set_error('backup_window_end', t('Invalid end time'));
}
}
}
function hosting_backup_window_time_window_form_submit(&$form, &$form_state) {
$values = $form_state['values'];
$value_start = '';
if (!empty($values['backup_window_start'])) {
variable_set('hosting_backup_window_time_window_start', $values['backup_window_start']);
}
else {
variable_del('hosting_backup_window_time_window_start');
}
$value_end = '';
if (!empty($values['backup_window_end'])) {
variable_set('hosting_backup_window_time_window_end', $values['backup_window_end']);
}
else {
variable_del('hosting_backup_window_time_window_end');
}
variable_set('hosting_backup_window_time_window_days', $values['backup_window_days']);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment