You are here

function _backup_migrate_schedule_remove_expired_backups in Backup and Migrate 5.2

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

1 call to _backup_migrate_schedule_remove_expired_backups()
backup_migrate_schedules_run in includes/schedules.inc
Run the preconfigured schedules. Called on cron.

File

includes/schedules.inc, line 361
All of the schedule handling code needed for Backup and Migrate.

Code

function _backup_migrate_schedule_remove_expired_backups($destination_id, $num_to_keep) {
  require_once './' . drupal_get_path('module', 'backup_migrate') . '/includes/destinations.inc';

  // If num to keep is not 0 (0 is infinity).
  if ($num_to_keep && ($destination = backup_migrate_get_destination($destination_id))) {
    if ($destination_files = backup_migrate_destination_get_files($destination)) {

      // Sort the files by modified time.
      foreach ($destination_files as $file) {
        $files[str_pad($file['filemtime'], 10, "0", STR_PAD_LEFT) . "-" . $i++] = $file['filepath'];
      }

      // 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++) {
          $filepath = array_shift($files);
          file_delete($filepath);
        }
      }
    }
  }
}