You are here

public function Schedule::run in Backup and Migrate 8.4

Same name and namespace in other branches
  1. 5.0.x src/Entity/Schedule.php \Drupal\backup_migrate\Entity\Schedule::run()

Parameters

BackupMigrateInterface $bam: The Backup and Migrate service object used to execute the backups

bool $force: Run the schedule even if it is not due to be run.

File

src/Entity/Schedule.php, line 61

Class

Schedule
Defines the Schedule entity.

Namespace

Drupal\backup_migrate\Entity

Code

public function run(BackupMigrateInterface $bam, $force = FALSE) {
  $next_run_at = $this
    ->getNextRun();
  $time = \Drupal::time();
  $should_run_now = $time
    ->getRequestTime() >= $next_run_at;
  $enabled = $this
    ->get('enabled');
  if ($force || $should_run_now && $enabled) {

    // Set the last run time before attempting backup.
    // This will prevent a failing schedule from retrying on every cron run.
    $this
      ->setLastRun($time
      ->getRequestTime());
    try {
      $config = [];
      if ($settings_profile_id = $this
        ->get('settings_profile_id')) {

        // Load the settings profile if one is selected.
        $profile = SettingsProfile::load($settings_profile_id);
        if (!$profile) {
          throw new BackupMigrateException("The settings profile '%profile' does not exist", [
            '%profile' => $settings_profile_id,
          ]);
        }
        $config = $profile
          ->get('config');
      }
      \Drupal::logger('backup_migrate')
        ->info("Running schedule %name", [
        '%name' => $this
          ->get('label'),
      ]);

      // TODO: Set the config (don't just use the defaults).
      // Run the backup.
      // Set the schedule id in file metadata so that we can delete our own backups later.
      // This requires the metadata writer to have knowledge of 'bam_scheduleid' which is
      // a somewhat tight coupling that I'd like to unwind.
      $config['metadata']['bam_scheduleid'] = $this->id;
      $bam
        ->setConfig(new Config($config));
      $bam
        ->backup($this
        ->get('source_id'), $this
        ->get('destination_id'));

      // Delete old backups.
      if ($keep = $this
        ->get('keep')) {
        $destination = $bam
          ->destinations()
          ->get($this
          ->get('destination_id'));

        // If the destination can be listed then get the list of files.
        if ($destination instanceof ListableDestinationInterface) {

          // Get a list of files to delete. Don't attempt to delete more
          // than 10 files in one go.
          $delete = $destination
            ->queryFiles([
            'bam_scheduleid' => $this->id,
          ], 'datestamp', SORT_DESC, 10, $keep);
          foreach ($delete as $file) {
            $destination
              ->deleteFile($file
              ->getFullName());
          }
        }
      }
    } catch (BackupMigrateException $e) {
      \Drupal::logger('backup_migrate')
        ->error("Scheduled backup '%name' failed: @err", [
        '%name' => $this
          ->get('label'),
        '@err' => $e
          ->getMessage(),
      ]);
    }
  }
}