| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Provision backup command
|
| 5 |
*
|
| 6 |
* Back up an existing site
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Make sure the site is installed and enabled, and that we have a valid target to back up to.
|
| 11 |
*/
|
| 12 |
function drush_provision_drupal_provision_backup_validate($url = NULL, $backup_file = NULL) {
|
| 13 |
if (!@drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE)) {
|
| 14 |
if (drush_get_option('force', false)) {
|
| 15 |
drush_log("clearing error");
|
| 16 |
drush_set_context('DRUSH_ERROR_CODE', DRUSH_SUCCESS);
|
| 17 |
}
|
| 18 |
}
|
| 19 |
if (!drush_get_option('installed') && !drush_get_option('force', false)) {
|
| 20 |
drush_set_error('PROVISION_DRUPAL_SITE_NOT_FOUND');
|
| 21 |
}
|
| 22 |
|
| 23 |
// This is the actual drupal provisioning requirements.
|
| 24 |
if (!is_dir(drush_get_option('backup_path'))) {
|
| 25 |
drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND');
|
| 26 |
}
|
| 27 |
|
| 28 |
if ($backup_file) {
|
| 29 |
if (provision_path("exists", $backup_file, FALSE,
|
| 30 |
dt("Backing site up to @path."),
|
| 31 |
dt("Back up file @path already exists."),
|
| 32 |
'PROVISION_BACKUP_ALREADY_EXISTS')) {
|
| 33 |
drush_set_option('backup_file', $backup_file);
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
| 37 |
if (!$backup_file) {
|
| 38 |
$suggested = drush_get_option('backup_path') ."/$url-". date("Ymd.His", mktime()) .".tar.gz";
|
| 39 |
|
| 40 |
// Use format of mysite.com-2008-01-02, if already existing, add number.
|
| 41 |
while (is_file($suggested)) {
|
| 42 |
$count++;
|
| 43 |
$suggested = drush_get_option('backup_path') ."/$url-". date("Ymd.His", mktime()) ."_$count.tar.gz";
|
| 44 |
}
|
| 45 |
drush_set_option('backup_file', $suggested);
|
| 46 |
}
|
| 47 |
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Implentation of hook_provision_backup()
|
| 52 |
*/
|
| 53 |
function drush_provision_drupal_provision_backup($url) {
|
| 54 |
$backup_file = drush_get_option('backup_file');
|
| 55 |
// Adds the site directory into the backup file
|
| 56 |
drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
|
| 57 |
if (substr($backup_file, -2) == 'gz') {
|
| 58 |
$command = "tar -C %s -p -c -z -f %s .";
|
| 59 |
} else {
|
| 60 |
$command = "tar -C %s -p -c -f %s .";
|
| 61 |
}
|
| 62 |
$result = provision_shell_exec($command, drush_get_option('sites_path') . "/$url", $backup_file);
|
| 63 |
|
| 64 |
if (!$result && !drush_get_option('force', false)) {
|
| 65 |
drush_set_error('PROVISION_BACKUP_FAILED', dt("Could not back up sites directory for drupal"));
|
| 66 |
}
|
| 67 |
}
|