View source
<?php
function hosting_backup_queue_perm() {
return array(
'create backup schedules',
'administer hosting backup queue',
);
}
function hosting_backup_queue_hosting_queues() {
$queue['backup_queue'] = array(
'name' => t('Backup queue'),
'description' => t('Process the queue of backups.'),
'type' => 'serial',
'frequency' => strtotime("1 minute", 0),
'items' => 2,
'total_items' => 1,
'singular' => t('backup'),
'plural' => t('backups'),
'running_items' => 0,
);
return $queue;
}
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,
));
}
}
}
function _hosting_backup_queue_get_backup_task($site) {
return hosting_add_task($site, 'backup', array(
'description' => t('Automated backup'),
));
}
function _hosting_backup_queue_get_outstanding_backups($limit = 20) {
$return = array();
$interval = variable_get('hosting_backup_queue_default_interval', strtotime('1 day', 0));
_hosting_backup_queue_ensure_all_sites_denormalised();
$all_sites_with_settings = array();
$result = db_query('SELECT b.site_id, b.status, b.schedule, bq.last_backup FROM {hosting_backup_queue_sites_settings} b INNER JOIN {hosting_site} s ON b.site_id = s.nid INNER JOIN {node} n ON s.vid = n.vid LEFT JOIN {hosting_backup_queue_sites} bq ON bq.site_id = s.nid WHERE s.status = %d', HOSTING_SITE_ENABLED);
while ($record = db_fetch_object($result)) {
$all_sites_with_settings[] = $record->site_id;
if ($record->status == 'enabled') {
if ($record->last_backup <= time() - $record->schedule) {
$return[$record->last_backup] = $record->site_id;
}
}
}
if (variable_get('hosting_backup_queue_default_enabled', TRUE)) {
if (empty($all_sites_with_settings)) {
$all_sites_with_settings[] = 0;
}
$result = db_query("SELECT s.nid, b.last_backup FROM {hosting_site} s INNER JOIN {node} n ON s.vid = n.vid LEFT JOIN {hosting_backup_queue_sites} b ON b.site_id = s.nid WHERE s.status = %d AND b.last_backup <= %d AND NOT(s.nid IN (" . db_placeholders($all_sites_with_settings) . ")) ORDER BY b.last_backup, n.nid ASC LIMIT %d", array_merge(array(
HOSTING_SITE_ENABLED,
time() - $interval,
), $all_sites_with_settings, array(
$limit,
)));
while ($node = db_fetch_object($result)) {
$return[$node->last_backup] = $node->nid;
}
}
ksort($return);
$return_sites = array();
while (count($return) > 0 && count($return_sites) < $limit) {
$return_sites[] = array_shift($return);
}
return $return_sites;
}
function hosting_backup_queue_sites_count() {
if (variable_get('hosting_backup_queue_default_enabled', TRUE)) {
$max = db_result(db_query("SELECT COUNT(*) FROM {hosting_site} s INNER JOIN {node} n ON s.vid = n.vid WHERE s.status = %d", HOSTING_SITE_ENABLED));
$disabled_sites = db_result(db_query("SELECT COUNT(*) FROM {hosting_backup_queue_sites_settings} b INNER JOIN {hosting_site} s ON b.site_id = s.nid INNER JOIN {node} n ON s.vid = n.vid WHERE s.status = %d AND b.status = 'disabled'", HOSTING_SITE_ENABLED));
return max(0, $max - $disabled_sites);
}
else {
return db_result(db_query("SELECT COUNT(*) FROM {hosting_backup_queue_sites_settings} b INNER JOIN {hosting_site} s ON b.site_id = s.nid INNER JOIN {node} n ON s.vid = n.vid WHERE s.status = %d AND b.status = 'enabled'", HOSTING_SITE_ENABLED));
}
}
function hosting_backup_queue_backup_time_update($site, $timestamp = NULL) {
$record = array(
'site_id' => $site,
'last_backup' => !is_null($timestamp) ? $timestamp : time(),
);
if (db_result(db_query('SELECT count(*) FROM {hosting_backup_queue_sites} WHERE site_id = %d', $record['site_id']))) {
drupal_write_record('hosting_backup_queue_sites', $record, 'site_id');
}
else {
drupal_write_record('hosting_backup_queue_sites', $record);
}
}
function hosting_backup_queue_backup_time_clear($site) {
db_query('DELETE FROM {hosting_backup_queue_sites} WHERE site_id = %d', $site);
}
function _hosting_backup_queue_ensure_all_sites_denormalised() {
$sites = db_query('SELECT n.nid FROM {hosting_site} s INNER JOIN {node} n ON s.vid = n.vid LEFT JOIN {hosting_backup_queue_sites} b ON b.site_id = s.nid WHERE s.status = %d AND b.last_backup IS NULL', HOSTING_SITE_ENABLED);
while ($row = db_fetch_object($sites)) {
$timestamp = db_result(db_query("SELECT MAX(timestamp) FROM {hosting_site_backups} WHERE site = %d", $row->nid));
if (empty($timestamp)) {
$timestamp = 0;
}
hosting_backup_queue_backup_time_update($row->nid, $timestamp);
}
}
function hosting_backup_queue_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'site_node_form' && user_access('create backup schedules')) {
$form['hosting_backup_queue'] = array(
'#type' => 'fieldset',
'#title' => t('Backup schedule'),
);
$form['hosting_backup_queue']['hosting_backup_queue_settings'] = array(
'#type' => 'radios',
'#title' => t('Backup schedule settings'),
'#default_value' => $form['#node']->hosting_backup_queue_settings ? $form['#node']->hosting_backup_queue_settings : 'default',
'#required' => TRUE,
'#options' => array(
'default' => t('Default settings'),
'custom' => t('Site specific settings'),
),
);
$form['hosting_backup_queue']['hosting_backup_queue_status'] = array(
'#type' => 'radios',
'#title' => t('Scheduler status'),
'#default_value' => $form['#node']->hosting_backup_queue_status ? $form['#node']->hosting_backup_queue_status : variable_get('hosting_backup_queue_default_enabled', TRUE) ? 'enabled' : 'disabled',
'#required' => TRUE,
'#options' => array(
'enabled' => t('Backups enabled'),
'disabled' => t('Backups disabled'),
),
);
$intervals = drupal_map_assoc(array(
strtotime('1 hour', 0),
strtotime('6 hours', 0),
strtotime('12 hours', 0),
strtotime('1 day', 0),
strtotime('7 days', 0),
strtotime('28 days', 0),
strtotime('1 year', 0),
), 'format_interval');
$form['hosting_backup_queue']['hosting_backup_queue_schedule'] = array(
'#type' => 'select',
'#title' => t('Backup interval'),
'#default_value' => $form['#node']->hosting_backup_queue_schedule ? $form['#node']->hosting_backup_queue_schedule : variable_get('hosting_backup_queue_default_interval', strtotime('1 day', 0)),
'#required' => TRUE,
'#options' => $intervals,
);
return $form;
}
if ($form_id == 'hosting_site_backup_manager_settings' && user_access('administer hosting backup queue')) {
$form['hosting_backup_queue_default_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Backup sites by default'),
'#description' => t('Sites without a specific backup schedule specified will get the settings here. These settings can be overriden by configuring specific sites.'),
'#default_value' => variable_get('hosting_backup_queue_default_enabled', TRUE),
'#weight' => -19,
);
$intervals = drupal_map_assoc(array(
strtotime('1 hour', 0),
strtotime('6 hours', 0),
strtotime('12 hours', 0),
strtotime('1 day', 0),
strtotime('7 days', 0),
strtotime('28 days', 0),
strtotime('1 year', 0),
), 'format_interval');
$form['hosting_backup_queue_default_interval'] = array(
'#type' => 'select',
'#title' => t('Default backup interval'),
'#default_value' => variable_get('hosting_backup_queue_default_interval', strtotime('1 day', 0)),
'#options' => $intervals,
'#weight' => -20,
);
}
}
function hosting_backup_queue_insert($node) {
if (!empty($node->hosting_backup_queue_settings) && $node->hosting_backup_queue_settings != 'default') {
$record = array(
'site_id' => $node->nid,
'status' => $node->hosting_backup_queue_status,
'schedule' => $node->hosting_backup_queue_schedule,
);
drupal_write_record('hosting_backup_queue_sites_settings', $record);
}
}
function hosting_backup_queue_update($node) {
if (!empty($node->hosting_backup_queue_settings)) {
if ($node->hosting_backup_queue_settings == 'default') {
hosting_backup_queue_delete($node);
}
else {
$record = array(
'site_id' => $node->nid,
'status' => $node->hosting_backup_queue_status,
'schedule' => $node->hosting_backup_queue_schedule,
);
if (db_result(db_query('SELECT count(*) FROM {hosting_backup_queue_sites_settings} WHERE site_id = %d', $node->nid))) {
drupal_write_record('hosting_backup_queue_sites_settings', $record, 'site_id');
}
else {
drupal_write_record('hosting_backup_queue_sites_settings', $record);
}
}
}
}
function hosting_backup_queue_delete($node) {
db_query("DELETE FROM {hosting_backup_queue_sites_settings} WHERE site_id = %d", $node->nid);
}
function hosting_backup_queue_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->type == 'site') {
switch ($op) {
case 'insert':
hosting_backup_queue_insert($node);
break;
case 'update':
hosting_backup_queue_update($node);
break;
case 'delete':
hosting_backup_queue_delete($node);
break;
case 'load':
$additions = array();
if ($record = db_fetch_object(db_query("SELECT status, schedule FROM {hosting_backup_queue_sites_settings} WHERE site_id = %d", $node->nid))) {
$additions['hosting_backup_queue_settings'] = 'custom';
$additions['hosting_backup_queue_status'] = $record->status;
$additions['hosting_backup_queue_schedule'] = $record->schedule;
}
else {
$additions['hosting_backup_queue_settings'] = 'default';
$additions['hosting_backup_queue_status'] = variable_get('hosting_backup_queue_default_enabled', TRUE) ? 'enabled' : 'disabled';
$additions['hosting_backup_queue_schedule'] = variable_get('hosting_backup_queue_default_interval', strtotime('1 day', 0));
}
return $additions;
break;
}
}
}