You are here

backup_migrate.module in Backup and Migrate 8.4

Primary hook implementations for Backup Migrate.

File

backup_migrate.module
View source
<?php

/**
 * @file
 * Primary hook implementations for Backup Migrate.
 */
use BackupMigrate\Core\Config\Config;
use BackupMigrate\Core\Services\BackupMigrate;
use BackupMigrate\Core\Main\BackupMigrateInterface;
use Drupal\backup_migrate\Entity\Destination;
use Drupal\backup_migrate\Entity\Schedule;
use Drupal\backup_migrate\Entity\Source;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\ClassLoader\Psr4ClassLoader;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\User;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
use Drupal\Core\Link;

// Autoload the required classes.
// @todo Apparently this isn't working on Windows machines.
// @see https://www.drupal.org/node/2913021
$loader = new Psr4ClassLoader();
$loader
  ->addPrefix('BackupMigrate\\Core\\', __DIR__ . '/lib/backup_migrate_core/src');
$loader
  ->addPrefix('BackupMigrate\\Drupal\\', __DIR__ . '/src');
$loader
  ->register();
define('BACKUP_MIGRATE_MODULE_VERSION', '8.x-4.x-dev');

/**
 * Back up a source to 1 or more destinations.
 *
 * @param string $source_id
 * @param string|array $destination_id
 * @param array $config
 */
function backup_migrate_perform_backup($source_id, $destination_id, $config = []) {
  try {

    // Retrieve the service.
    $bam = backup_migrate_get_service_object($config);

    // Run the backup.
    $bam
      ->backup($source_id, $destination_id);
    drupal_set_message(t('Backup Complete.'));
  } catch (Exception $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
  }
}

/**
 * Restore a source from a destination and file id.
 *
 * @param string $source_id
 * @param string|array $destination_id
 * @param string|null $file_id
 * @param array $config
 * @param string $file_id
 */
function backup_migrate_perform_restore($source_id, $destination_id, $file_id = NULL, $config = []) {
  try {

    // Retrieve the service.
    $bam = backup_migrate_get_service_object($config);

    // Run the backup.
    $bam
      ->restore($source_id, $destination_id, $file_id);
    drupal_set_message(t('Restore Complete.'));
  } catch (Exception $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
    return;
  }
}

/**
 * Get a BackupMigrate service object.
 *
 * @param array $config_array
 *  An array of configuration arrays, keyed by plugin id.
 * @param array $options
 *  A keyed array of options.
 *
 * @return \BackupMigrate\Core\Main\BackupMigrate
 */
function backup_migrate_get_service_object($config_array = [], $options = []) {
  static $bam = NULL;

  // If the static cached object has not been loaded.
  if ($bam === NULL) {

    // Create the service object.
    $bam = new \BackupMigrate\Core\Main\BackupMigrate();

    // Allow other modules to alter the object.
    \Drupal::moduleHandler()
      ->alter('backup_migrate_service_object', $bam, $options);
  }

  // Set the configuration overrides if any were passed in.
  if ($config_array) {
    $bam
      ->setConfig(new Config($config_array));
  }
  return $bam;
}

/**
 * Implements hook_backup_migrate_service_object_alter().
 *
 * Add the core Backup and Migrate plugins to the service object.
 *
 * @param \BackupMigrate\Core\Main\BackupMigrateInterface $bam
 * @param array $options
 */
function backup_migrate_backup_migrate_service_object_alter(BackupMigrateInterface &$bam, $options = []) {
  $sources = $bam
    ->sources();
  $destinations = $bam
    ->destinations();
  $plugins = $bam
    ->plugins();
  $services = $bam
    ->services();

  // Add a temp file manager which can access the drupal temp directory.
  $services
    ->add('TempFileAdapter', new \BackupMigrate\Drupal\File\DrupalTempFileAdapter(\Drupal::service('file_system'), 'temporary://', 'bam'));
  $services
    ->add('TempFileManager', new \BackupMigrate\Core\File\TempFileManager($services
    ->get('TempFileAdapter')));

  // Add a logger which prints everything to the browser.
  $services
    ->add('Logger', new \BackupMigrate\Drupal\Environment\DrupalSetMessageLogger());
  $services
    ->add('ArchiveReader', new \BackupMigrate\Core\Service\TarArchiveReader());
  $services
    ->add('ArchiveWriter', new \BackupMigrate\Core\Service\TarArchiveWriter());

  // If this is a nobrowser op (cron) then do not add the browser plugins.
  // TODO: Make this better.
  if (empty($options['nobrowser'])) {

    // Add a download destination.
    $user = \Drupal::currentUser();
    if ($user
      ->hasPermission('access backup files')) {
      $destinations
        ->add('download', new \BackupMigrate\Drupal\Destination\DrupalBrowserDownloadDestination(new Config([
        'name' => t('Download'),
      ])));
    }

    // Add an upload destination.
    $destinations
      ->add('upload', new \BackupMigrate\Drupal\Destination\DrupalBrowserUploadDestination(new Config([
      'name' => t('Upload'),
    ])));
  }

  // Add a file naming filter.
  $plugins
    ->add('namer', new \BackupMigrate\Core\Filter\FileNamer());

  // Add a compression filter.
  $plugins
    ->add('compressor', new \BackupMigrate\Core\Filter\CompressionFilter());

  // Add the Encrypt utilities filter.
  $plugins
    ->add('encrypt', new \BackupMigrate\Drupal\Filter\DrupalEncrypt());

  // Add the Drupal utilities filter.
  $plugins
    ->add('utils', new \BackupMigrate\Drupal\Filter\DrupalUtils());

  // Add a file metadata filter.
  $plugins
    ->add('metadata', new \BackupMigrate\Core\Filter\MetadataWriter(new Config([
    'generator' => 'Backup and Migrate for Drupal (https://www.drupal.org/project/backup_migrate)',
    'generatorurl' => 'https://www.drupal.org/project/backup_migrate',
    'generatorversion' => BACKUP_MIGRATE_MODULE_VERSION,
  ])));

  // Add the custom configured sources.
  foreach (Source::loadMultiple() as $source) {
    $source
      ->getPlugin()
      ->alterBackupMigrate($bam, $source
      ->get('id'), $options);
  }

  // Add the custom configured destination.
  foreach (Destination::loadMultiple() as $destination) {
    $destination
      ->getPlugin()
      ->alterBackupMigrate($bam, $destination
      ->get('id'), $options);
  }
}

/**
 * Implements hook_cron().
 *
 * Runs all of the enabled schedules if they are due to be run..
 */
function backup_migrate_cron() {
  $bam = backup_migrate_get_service_object([], [
    'nobrowser' => TRUE,
  ]);
  $schedules = Schedule::loadMultiple();
  foreach ($schedules as $schedule) {
    $schedule
      ->run($bam);
  }
}

/**
 * Implements hook_form_alter().
 */
function backup_migrate_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Label the items being deleted on uninstall to make the 'entire site' listing less terrifying.
  if ($form_id === 'system_modules_uninstall_confirm_form') {
    if (isset($form['entity_deletes']['backup_migrate_source'])) {
      $form['text']['#markup'] .= '<p>' . t('Uninstalling Backup and Migrate will delete any custom Backup and Migrate configuration. <strong>Previously created backups will not be deleted.</strong>') . '</p>';
    }
    if (isset($form['entity_deletes']['backup_migrate_source']['#items']['entire_site'])) {
      $form['entity_deletes']['backup_migrate_source']['#items']['entire_site'] = t('Full Site Backup Source');
    }
  }
}

/**
 * Implements hook_help().
 */
function backup_migrate_help($route_name, RouteMatchInterface $route_match) {
  if ($route_name == 'help.page.backup_migrate') {
    $help = [];
    $user = User::load(\Drupal::currentUser()
      ->id());

    // Creates url objects for backup_migrate routes.
    $urls = [
      'restore' => 'backup_migrate.restore',
      'schedules' => 'entity.backup_migrate_schedule.collection',
      'manual' => 'backup_migrate.quick_backup',
      'advanced' => 'backup_migrate.advanced_backup',
      'settings' => 'entity.backup_migrate_settings.collection',
      'destination' => 'entity.backup_migrate_destination.collection',
      'sources' => 'entity.backup_migrate_source.collection',
      'backups' => 'backup_migrate.backups',
      'documentation' => 'https://www.drupal.org/docs/8/modules/backup-and-migrate-drupal-8',
      'phpmyadmin' => 'http://www.phpmyadmin.net',
      'cron' => 'http://drupal.org/cron',
    ];
    foreach ($urls as $key => $destination) {
      $urls[$key] = UrlHelper::isValid($destination, TRUE) ? Url::fromUri($destination) : Url::fromRoute($destination);
      $urls[$key]
        ->setOptions([
        'attributes' => [
          'target' => '_blank',
        ],
      ]);
    }
    if ($user
      ->hasPermission('perform backups')) {
      $help['intro'] = [
        '#markup' => t("<p><strong>Backup and Migrate</strong> makes the task of backing up your Drupal database and migrating data from one Drupal install to another easier. It provides a function to backup the entire database to file or download, and to restore from a previous backup. You can also schedule the backup operation. Compression of backup files is also supported. The database backup files created with this module can be imported into this or any other Drupal installation with the @restore, or you can use a database tool such as @phpmyadmin or the mysql command line command. Access for backup migrate is controlled by module permissions, contact the administrator or any privileged user to get access to full module features.</p>", [
          '@restore' => Link::fromTextAndUrl(t('restore feature'), $urls['restore'])
            ->toString(),
          '@phpmyadmin' => Link::fromTextAndUrl(t('phpMyAdmin'), $urls['phpmyadmin'])
            ->toString(),
        ]),
      ];
      $help['manual'] = [
        '#markup' => t("<h3>@title</h3><p>Use this form to run simple @manual of your database. Visit the @documentation for more help using this module.</p>", [
          '@title' => Link::fromTextAndUrl(t('Backup Tab - Quick Backup'), $urls['manual'])
            ->toString(),
          '@manual' => Link::fromTextAndUrl(t('manual backups'), $urls['manual'])
            ->toString(),
          '@documentation' => Link::fromTextAndUrl(t('documentation page'), $urls['documentation'])
            ->toString(),
        ]),
      ];
      $help['advanced'] = [
        '#markup' => t("<h3>@title</h3><p>Use this form to run manual backups of your database with more advanced options. If you have any @profile saved you can load those settings. You can save any of the changes you make to these settings as a new settings profile.</p>", [
          '@title' => Link::fromTextAndUrl(t('Backup Tab - Advanced Backup'), $urls['advanced'])
            ->toString(),
          '@profile' => Link::fromTextAndUrl(t('settings profiles'), $urls['settings'])
            ->toString(),
        ]),
      ];
    }
    if ($user
      ->hasPermission('restore from backup')) {
      $help['restore'] = [
        '#markup' => t("<h3>@title</h3><p>Upload a backup and migrate backup file. The restore function will not work with database dumps from other sources such as @phpmyadmin.</p>", [
          '@title' => Link::fromTextAndUrl(t('Restore Tab'), $urls['restore'])
            ->toString(),
          '@phpmyadmin' => Link::fromTextAndUrl(t('phpMyAdmin'), $urls['phpmyadmin'])
            ->toString(),
        ]),
      ];
    }
    if ($user
      ->hasPermission('administer backup and migrate')) {
      $help['backups'] = [
        '#markup' => t("<h3>@title</h3><p>Backups are the places you can save your backup files to or them load from.</p><p>Files can be saved to a directory on your web server, downloaded to your desktop or emailed to a specified email account. From the @destination tab you can create, delete and edit destinations or list the files which have already been backed up to the available destinations.</p>", [
          '@title' => Link::fromTextAndUrl(t('Saved Backups'), $urls['backups'])
            ->toString(),
          '@destination' => Link::fromTextAndUrl(t('destination'), $urls['destination'])
            ->toString(),
        ]),
      ];
      $help['schedules'] = [
        '#markup' => t("<h3>@title</h3><p>Automatically backup up your database on a regular schedule using @cron.</p><p>Each schedule will run a maximum of once per cron run, so they will not run more frequently than your cron is configured to run. If you specify a number of backups to keep for a schedule, old backups will be deleted as new ones created. <strong>If specifiy a number of files to keep other backup files in that schedule's @destination will get deleted</strong>.<p>", [
          '@title' => Link::fromTextAndUrl(t('Shedules Tab'), $urls['schedules'])
            ->toString(),
          '@cron' => Link::fromTextAndUrl(t('cron'), $urls['cron'])
            ->toString(),
          '@destination' => Link::fromTextAndUrl(t('destination'), $urls['destination'])
            ->toString(),
        ]),
      ];
      $help['settings'] = [
        '#markup' => t("<h3>@title</h3><p>Settings are profile. Settings store your table exclusion settings as well as your backup file name, compression and timestamp settings. You can use profiles in @schedules and for @manual.</p><p>You can create new profiles using the add profiles tab or by checking the 'Save these settings' button on the advanced backup page.</p>", [
          '@title' => Link::fromTextAndUrl(t('Settings Tab - Settings Profiles'), $urls['settings'])
            ->toString(),
          '@schedules' => Link::fromTextAndUrl(t('schedules'), $urls['schedules'])
            ->toString(),
          '@manual' => Link::fromTextAndUrl(t('quick backups'), $urls['manual'])
            ->toString(),
        ]),
      ];
      $help['destination'] = [
        '#markup' => t("<h3>@title</h3><p>Destinations store your custom created destionation settings as backup server folders or external clouds (Clouds destinations are in work, check the module issues lists).</p>", [
          '@title' => Link::fromTextAndUrl(t('Settings Tab - Destinations'), $urls['destination'])
            ->toString(),
        ]),
      ];
      $help['sources'] = [
        '#markup' => t("<h3>@title</h3><p>Sources store your source settings you want to backup, for now is available 4 default sources to use. Follow module issues list for new source features.</p>", [
          '@title' => Link::fromTextAndUrl(t('Settings Tab - Sources'), $urls['sources'])
            ->toString(),
        ]),
      ];
    }
    $output = '';
    foreach ($help as $key => $value) {
      $output .= render($value);
    }
    return $output;
  }
}

Functions

Namesort descending Description
backup_migrate_backup_migrate_service_object_alter Implements hook_backup_migrate_service_object_alter().
backup_migrate_cron Implements hook_cron().
backup_migrate_form_alter Implements hook_form_alter().
backup_migrate_get_service_object Get a BackupMigrate service object.
backup_migrate_help Implements hook_help().
backup_migrate_perform_backup Back up a source to 1 or more destinations.
backup_migrate_perform_restore Restore a source from a destination and file id.

Constants