public function backup_migrate_schedule::smart_delete_backups in Backup and Migrate 7.3
Same name and namespace in other branches
- 8.3 includes/schedules.inc \backup_migrate_schedule::smart_delete_backups()
- 6.3 includes/schedules.inc \backup_migrate_schedule::smart_delete_backups()
Delete files keeping the specified number of hourly, daily, weekly and monthly backups.
1 call to backup_migrate_schedule::smart_delete_backups()
- backup_migrate_schedule::remove_expired_backups in includes/
schedules.inc - Remove older backups keeping only the number specified by the aministrator.
File
- includes/
schedules.inc, line 783 - All of the schedule handling code needed for Backup and Migrate.
Class
- backup_migrate_schedule
- A schedule class for crud operations.
Code
public function smart_delete_backups($destination, $files, $keep_subhourly = 3600, $keep_hourly = 24, $keep_daily = 14, $keep_weekly = PHP_INT_MAX, $keep_monthly = PHP_INT_MAX) {
// Each period must be an exact multiple of the next smallest period.
$now = time();
$periods = array(
'subhourly' => array(
'delta' => 1,
'keep' => $keep_subhourly,
'last_time' => 0,
'files' => array(),
),
'hourly' => array(
'delta' => 60 * 60,
'keep' => $keep_hourly,
'last_time' => 0,
'files' => array(),
),
'daily' => array(
'delta' => 60 * 60 * 24,
'keep' => $keep_daily,
'last_time' => 0,
'files' => array(),
),
'weekly' => array(
'delta' => 60 * 60 * 24 * 7,
'keep' => $keep_weekly,
'last_time' => 0,
'files' => array(),
),
);
$keep_files = $filetimes = $times = $groups = $sorted = $saved = array();
foreach ($files as $id => $file) {
if ($file
->is_recognized_type()) {
$time = $file
->info('filetime');
$sorted[$id] = $time;
}
}
// Sort files, oldest first.
asort($sorted);
// Reset internal pointer and get the oldest file time.
$oldest_file_time = reset($sorted);
// Save the oldest file.
$keep_files[key($sorted)] = key($sorted);
foreach ($periods as $i => $period) {
$period_keep_files = array();
$time = $oldest_file_time;
// Set time from which we start saving files.
$period_start_time = $now - ($period['keep'] + 1) * $period['delta'];
// Increase time to within one period time span of the period start
// time. This keeps all the different period starts aligned.
if ($time < $period_start_time) {
$time += (int) ceil(($period_start_time - $time) / $period['delta']) * $period['delta'];
}
$file_id = $this
->find_nearest_file($sorted, $time);
do {
$period_keep_files[$file_id] = $file_id;
$last_file_id = $file_id;
$time += $period['delta'];
$file_id = $this
->find_nearest_file($sorted, $time);
} while ($time < $now);
$keep_files = array_merge($keep_files, $period_keep_files);
}
// Do the delete.
foreach ($files as $id => $file) {
if (!isset($keep_files[$id])) {
$destination
->delete_file($file
->file_id());
}
}
}