Newer
Older
Adrian Rossouw
committed
<?php
/**
* Provision backup command
*
* Back up an existing site
*/
/**
* 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) {
Adrian Rossouw
committed
if (!@drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
if (drush_get_option('force', false)) {
drush_log("clearing error");
drush_set_context('DRUSH_ERROR_CODE', DRUSH_SUCCESS);
}
}
if (!drush_get_option('installed') && !drush_get_option('force', false)) {
drush_set_error('PROVISION_DRUPAL_SITE_NOT_FOUND');
}
Adrian Rossouw
committed
// This is the actual drupal provisioning requirements.
drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND');
Adrian Rossouw
committed
}
Adrian Rossouw
committed
Adrian Rossouw
committed
if ($backup_file) {
if (provision_file()->exists($backup_file)->status()) {
drush_set_error('PROVISION_BACKUP_ALREADY_EXISTS', dt('Back up file @path already exists.', array('@path' => $backup_file)));
}
else {
drush_log(dt('Backing site up to @path.', array('@path' => $backup_file)), 'message');
drush_set_option('backup_file', $backup_file);
Adrian Rossouw
committed
}
}
Adrian Rossouw
committed
Adrian Rossouw
committed
if (!$backup_file) {
$suggested = d()->platform->server->backup_path . '/' . d()->uri . '-' . date("Ymd.His", mktime()) . '.tar.gz';
Adrian Rossouw
committed
// Use format of mysite.com-2008-01-02, if already existing, add number.
Adrian Rossouw
committed
$count++;
$suggested = d()->platform->server->backup_path . '/' . d()->uri . '-' . date('Ymd.His', mktime()) . '_' . $count . '.tar.gz';
Adrian Rossouw
committed
}
drush_set_option('backup_file', $suggested);
Adrian Rossouw
committed
}
Adrian Rossouw
committed
}
/**
* Implentation of hook_provision_backup()
*/
function drush_provision_drupal_provision_backup() {
Adrian Rossouw
committed
$backup_file = drush_get_option('backup_file');
Adrian Rossouw
committed
// Adds the site directory into the backup file
Adrian Rossouw
committed
drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
$olddir = getcwd();
// we need to do this because some retarded implementations of tar (e.g. SunOS) don't support -C
Adrian Rossouw
committed
chdir(d()->site_path);
if (substr($backup_file, -2) == 'gz') {
// same as above: some do not support -z
$command = "tar cpf - . | gzip -c > %s";
$command = "tar cpf %s .";
$result = provision_shell_exec($command, $backup_file);
chdir($olddir);
Adrian Rossouw
committed
anarcat
committed
if (!$result && !drush_get_option('force', false)) {
drush_set_error('PROVISION_BACKUP_FAILED', dt("Could not back up sites directory for drupal"));
Adrian Rossouw
committed
}
}
/**
* Remove the backup file if something went wrong
*/
function drush_provision_drupal_provision_backup_rollback() {
$backup_file = drush_get_option('backup_file');
if (file_exists($backup_file)) {
provision_file()->unlink($backup_file)
->succeed('Removed stale backup file @path')
->fail('Failed deleting backup file @path');
}
}
Adrian Rossouw
committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* 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);
}
}
}