backup.provision.inc

  1. 7.x-3.x platform/backup.provision.inc
  2. 7.x-3.x db/backup.provision.inc

Functions

Namesort descending Description
drush_provision_drupal_post_provision_backup
drush_provision_drupal_provision_backup Implentation of hook_provision_backup()
drush_provision_drupal_provision_backup_rollback Remove the backup file if something went wrong
drush_provision_drupal_provision_backup_validate Make sure the site is installed and enabled, and that we have a valid target to back up to.

File

platform/backup.provision.inc
View source
  1. <?php
  2. /**
  3. * Provision backup command
  4. *
  5. * Back up an existing site
  6. */
  7. /**
  8. * Make sure the site is installed and enabled, and that we have a valid target to back up to.
  9. */
  10. function drush_provision_drupal_provision_backup_validate($backup_file = NULL) {
  11. if (!@drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
  12. if (drush_get_option('force', false)) {
  13. drush_log("clearing error");
  14. drush_set_context('DRUSH_ERROR_CODE', DRUSH_SUCCESS);
  15. }
  16. }
  17. if (!drush_get_option('installed') && !drush_get_option('force', false)) {
  18. drush_set_error('PROVISION_DRUPAL_SITE_NOT_FOUND');
  19. }
  20. // This is the actual drupal provisioning requirements.
  21. if (!is_dir(d()->platform->server->backup_path)) {
  22. drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND');
  23. }
  24. if ($backup_file) {
  25. if (provision_file()->exists($backup_file)->status()) {
  26. drush_set_error('PROVISION_BACKUP_ALREADY_EXISTS', dt('Back up file @path already exists.', array('@path' => $backup_file)));
  27. }
  28. else {
  29. drush_log(dt('Backing site up to @path.', array('@path' => $backup_file)));
  30. drush_set_option('backup_file', $backup_file);
  31. }
  32. }
  33. if (!$backup_file) {
  34. $suggested = d()->platform->server->backup_path . '/' . d()->uri . '-' . date("Ymd.His", time()) . '.tar.gz';
  35. // Use format of mysite.com-2008-01-02, if already existing, add number.
  36. $count = 0;
  37. while (is_file($suggested)) {
  38. $count++;
  39. $suggested = d()->platform->server->backup_path . '/' . d()->uri . '-' . date('Ymd.His', time()) . '_' . $count . '.tar.gz';
  40. }
  41. drush_set_option('backup_file', $suggested);
  42. }
  43. }
  44. /**
  45. * Implentation of hook_provision_backup()
  46. */
  47. function drush_provision_drupal_provision_backup() {
  48. $backup_file = drush_get_option('backup_file');
  49. // Adds the site directory into the backup file
  50. drush_log(dt("Adding sites directory to !backup_file", array('!backup_file' => $backup_file)), 'backup');
  51. // synch all filesystem changes back from the remote server.
  52. provision_drupal_fetch_site();
  53. // Check if we are currently cloaking credentials
  54. $cloaked = d()->service('http')->cloaked_db_creds();
  55. $cloaked = drush_get_option('provision_db_cloaking', $cloaked);
  56. if ($cloaked) {
  57. drush_set_option('cloaking_off_temp', TRUE);
  58. // Disable the cloaking of credentials temporarily
  59. drush_log(dt("Temporarily uncloaking database credentials for backup"));
  60. drush_set_option('provision_db_cloaking', FALSE);
  61. // Write the uncloaked credentials to the settings.php
  62. _provision_drupal_create_settings_file();
  63. provision_drupal_push_site();
  64. }
  65. $olddir = getcwd();
  66. if (!chdir(d()->site_path)) {
  67. return drush_set_error('PROVISION_BACKUP_PATH_NOT_FOUND', dt('cannot change directory to %dir', array('%dir' => d()->site_path)));
  68. }
  69. if (substr($backup_file, -2) == 'gz') {
  70. $command = 'tar cpfz %s .';
  71. } else {
  72. $command = 'tar cpf %s .';
  73. }
  74. $result = drush_shell_exec($command, $backup_file);
  75. // Get the size of the backup
  76. $size = filesize($backup_file);
  77. drush_set_option('backup_file_size', $size);
  78. chdir($olddir);
  79. if (drush_get_option('cloaking_off_temp', FALSE)) {
  80. drush_log(dt("Re-cloaking database credentials after backup"));
  81. drush_set_option('provision_db_cloaking', TRUE);
  82. _provision_drupal_create_settings_file();
  83. provision_drupal_push_site();
  84. }
  85. if (!$result && !drush_get_option('force', false)) {
  86. drush_set_error('PROVISION_BACKUP_FAILED', dt("Could not back up sites directory for drupal"));
  87. }
  88. }
  89. function drush_provision_drupal_post_provision_backup() {
  90. drush_log(dt('Backed up site up to @path.', array('@path' => drush_get_option('backup_file'))), 'success');
  91. if (d()->client_name) {
  92. $backup_dir = d()->server->clients_path . '/' . d()->client_name . '/backups';
  93. provision_file()->create_dir($backup_dir, dt('Client backup directory for @client', array('@client' => d()->client_name)), 0750);
  94. provision_file()->symlink(drush_get_option('backup_file'), $backup_dir . '/' . basename(drush_get_option('backup_file')))
  95. ->succeed('Created symlink @path to @target')
  96. ->fail('Could not create symlink @path to @target: @reason');
  97. }
  98. }
  99. /**
  100. * Remove the backup file if something went wrong
  101. */
  102. function drush_provision_drupal_provision_backup_rollback() {
  103. $backup_file = drush_get_option('backup_file');
  104. if (file_exists($backup_file)) {
  105. provision_file()->unlink($backup_file)
  106. ->succeed('Removed stale backup file @path')
  107. ->fail('Failed deleting backup file @path');
  108. }
  109. }