You are here

function _backup_migrate_remove_expired_backups in Backup and Migrate 6

Same name and namespace in other branches
  1. 5 backup_migrate.module \_backup_migrate_remove_expired_backups()

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

1 call to _backup_migrate_remove_expired_backups()
backup_migrate_cron in ./backup_migrate.module
Implementation of hook_cron(),

File

./backup_migrate.module, line 980
Create (manually or scheduled) and restore backups of your Drupal MySQL database with an option to exclude table data (f.e. cache_*)

Code

function _backup_migrate_remove_expired_backups() {
  $num_to_keep = variable_get("backup_migrate_schedule_backup_keep", 0);

  // If num to keep is not 0 (0 is infinity).
  if ($num_to_keep && ($dir = _backup_migrate_check_destination_dir("scheduled"))) {

    // Create a list of backup files indexed by time (with an aditional index to
    // prevent two files with the same create time from overwriting each other).
    $res = opendir($dir);
    $files = array();
    $i = 0;
    if ($res) {

      // Read all files and sort them by modified time.
      while ($file = readdir($res)) {
        $filepath = $dir . "/" . $file;
        if ($info = _backup_migrate_file_info($filepath)) {
          $files[str_pad($info['filemtime'], 10, "0", STR_PAD_LEFT) . "-" . $i++] = $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);
      }
    }
  }
}