You are here

hosting_backup_queue.module in Hosting Site Backup Manager 6.2

Same filename and directory in other branches
  1. 7.3 hosting_backup_queue/hosting_backup_queue.module

File

hosting_backup_queue/hosting_backup_queue.module
View source
<?php

/**
 * Implementation of hook_perm
 */
function hosting_backup_queue_perm() {
  return array(
    'create backup schedules',
    'administer hosting backup queue',
  );
}

/**
 * Implementation of hook_hosting_queues
 *
 * Return a list of queues that this module needs to manage.
 */
function hosting_backup_queue_hosting_queues() {
  $queue['backup_queue'] = array(
    'name' => t('Backup queue'),
    'description' => t('Process the queue of backups.'),
    'type' => 'serial',
    # run queue sequentially. always with the same parameters.
    'frequency' => strtotime("1 minute", 0),
    'items' => 2,
    'total_items' => 1,
    'singular' => t('backup'),
    'plural' => t('backups'),
    // TODO: Do we need to set this?
    'running_items' => 0,
  );
  return $queue;
}

/**
 * The main queue callback for the backups.
 */
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,
      ));
    }
  }
}

/**
 * Returns the backup task to run for a given site.
 */
function _hosting_backup_queue_get_backup_task($site) {

  // Ensure that we have a backup task to run
  return hosting_add_task($site, 'backup', array(
    'description' => t('Automated backup'),
  ));
}

/**
 * Retrieve a list of sites to backup, now.
 *
 * @param limit
 *   The amount of items to return.
 * @return
 *   An array containing site ids.
 */
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();

  // Get all the nids of sites that have specific settings.
  $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 site backups are globally enabled, add additional sites we don't have settings for already.
  if (variable_get('hosting_backup_queue_default_enabled', TRUE)) {

    // At least have an array with a zero in it.
    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;
    }
  }

  // Now sort and trim the results.
  ksort($return);
  $return_sites = array();
  while (count($return) > 0 && count($return_sites) < $limit) {
    $return_sites[] = array_shift($return);
  }
  return $return_sites;
}

/**
 * Return the number of sites to backup
 */
function hosting_backup_queue_sites_count() {
  if (variable_get('hosting_backup_queue_default_enabled', TRUE)) {

    // Just return the number of enabled sites.
    $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 {

    // We don't support per site config, just yet.
    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));
  }
}

/**
 * Update the last backup timestamp of the given site.
 */
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']))) {

    // Update query:
    drupal_write_record('hosting_backup_queue_sites', $record, 'site_id');
  }
  else {

    // Insert query:
    drupal_write_record('hosting_backup_queue_sites', $record);
  }
}

/**
 * Clear the last backup timestamp for the given site.
 */
function hosting_backup_queue_backup_time_clear($site) {
  db_query('DELETE FROM {hosting_backup_queue_sites} WHERE site_id = %d', $site);
}

/**
 * Ensure that we have denormalised backup data for all enabled sites.
 */
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);
  }
}

/**
* Implementation of hook_form_alter()
*/
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,
    );
  }
}

/**
* Implementation of hook_insert()
*/
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);
  }
}

/**
* Implementation of hook_update()
*/
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))) {

        // Update
        drupal_write_record('hosting_backup_queue_sites_settings', $record, 'site_id');
      }
      else {

        // Insert
        drupal_write_record('hosting_backup_queue_sites_settings', $record);
      }
    }
  }
}

/**
* Implementation of hook_delete()
*/
function hosting_backup_queue_delete($node) {
  db_query("DELETE FROM {hosting_backup_queue_sites_settings} WHERE site_id = %d", $node->nid);
}

/**
* Implementation of hook_nodeapi()
*/
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;
    }
  }
}

Functions

Namesort descending Description
hosting_backup_queue_backup_time_clear Clear the last backup timestamp for the given site.
hosting_backup_queue_backup_time_update Update the last backup timestamp of the given site.
hosting_backup_queue_delete Implementation of hook_delete()
hosting_backup_queue_form_alter Implementation of hook_form_alter()
hosting_backup_queue_hosting_queues Implementation of hook_hosting_queues
hosting_backup_queue_insert Implementation of hook_insert()
hosting_backup_queue_nodeapi Implementation of hook_nodeapi()
hosting_backup_queue_perm Implementation of hook_perm
hosting_backup_queue_queue The main queue callback for the backups.
hosting_backup_queue_sites_count Return the number of sites to backup
hosting_backup_queue_update Implementation of hook_update()
_hosting_backup_queue_ensure_all_sites_denormalised Ensure that we have denormalised backup data for all enabled sites.
_hosting_backup_queue_get_backup_task Returns the backup task to run for a given site.
_hosting_backup_queue_get_outstanding_backups Retrieve a list of sites to backup, now.