Commit 8b154305 authored by Adrian Rossouw's avatar Adrian Rossouw

Delete backup files older than the amount specified in the --delete_backups_older_than option.

parent aed3d531
......@@ -10,6 +10,7 @@
* Make sure the site is installed and enabled, and that we have a valid target to back up to.
*/
function drush_provision_drupal_provision_backup_validate($backup_file = NULL) {
if (!@drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
if (drush_get_option('force', false)) {
drush_log("clearing error");
......@@ -83,3 +84,32 @@ function drush_provision_drupal_provision_backup_rollback() {
->fail('Failed deleting backup file @path');
}
}
/**
* Post backup purge. delete older files if instructed too.
*/
function drush_provision_drupal_post_provision_backup() {
if ($timestamp = drush_get_option('delete_backups_older_than',FALSE)) {
$timestamp = mktime() - $timestamp;
$to_delete = array();
$regex = sprintf("/^%s-(.*)\.(.*)\.tar\.gz$/", d()->uri);
$backup_path = d('@server_master')->backup_path;
$files = drush_scan_directory($backup_path , $regex, array('.', '..', 'CVS', '.svn'), 0, FALSE, 'basename');
if (sizeof($files)) {
foreach ($files as $filename => $file) {
$matches = array();
preg_match($regex, $filename, $matches);
$filetime = strtotime($matches[1] .' '. $matches[2]);
if ($filetime < $timestamp) {
$to_delete[] = $backup_path . '/' . $filename;
}
}
foreach ($to_delete as $delete) {
provision_file()->unlink($delete)->succeed("Backup file @path has been deleted")->fail("Backup file @path could not be deleted");
}
drush_set_option('backup_files_deleted', $to_delete);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment