You are here

function backup_migrate_schedule::remove_expired_backups in Backup and Migrate 8.2

Same name and namespace in other branches
  1. 8.3 includes/schedules.inc \backup_migrate_schedule::remove_expired_backups()
  2. 6.3 includes/schedules.inc \backup_migrate_schedule::remove_expired_backups()
  3. 6.2 includes/schedules.inc \backup_migrate_schedule::remove_expired_backups()
  4. 7.3 includes/schedules.inc \backup_migrate_schedule::remove_expired_backups()
  5. 7.2 includes/schedules.inc \backup_migrate_schedule::remove_expired_backups()

Remove older backups keeping only the number specified by the aministrator.

1 call to backup_migrate_schedule::remove_expired_backups()
backup_migrate_schedule::cron in includes/schedules.inc
Perform the cron action. Run the backup if enough time has elapsed.

File

includes/schedules.inc, line 372

Class

backup_migrate_schedule
A schedule class for crud operations.

Code

function remove_expired_backups() {
  backup_migrate_include('destinations');
  $num_to_keep = $this->keep;

  // If num to keep is not 0 (0 is infinity).
  if ($num_to_keep && ($destination = $this
    ->get_destination())) {
    $i = 0;
    if ($destination
      ->op('delete') && ($destination_files = $destination
      ->list_files())) {

      // Sort the files by modified time.
      foreach ($destination_files as $file) {
        if ($file
          ->is_recognized_type() && $destination
          ->can_delete_file($file
          ->file_id())) {
          $files[str_pad($file
            ->info('filetime'), 10, "0", STR_PAD_LEFT) . "-" . $i++] = $file;
        }
      }

      // If we are beyond our limit, remove as many as we need.
      $num_files = count($files);
      if ($num_files > $num_to_keep) {
        $num_to_delete = $num_files - $num_to_keep;

        // Sort by date.
        ksort($files);

        // Delete from the start of the list (earliest).
        for ($i = 0; $i < $num_to_delete; $i++) {
          $file = array_shift($files);
          $destination
            ->delete_file($file
            ->file_id());
        }
      }
    }
  }
}