diff --git a/commands/pm/pm.drush.inc b/commands/pm/pm.drush.inc index fd2346e603606b22bbc540b4f6430b58e086fa13..bf289a9a8b986285a37d958e2ab6ad5f9f5a6c2c 100644 --- a/commands/pm/pm.drush.inc +++ b/commands/pm/pm.drush.inc @@ -97,7 +97,6 @@ function pm_drush_command() { ); $update_options = array( '--lock' => 'Add a persistent lock to remove the specified projects from consideration during updates. Locks may be removed with the --unlock parameter, or overridden by specifically naming the module as a parameter to pm-update or pm-updatecode. The lock does not affect pm-download.', - '--backup-dir' => 'Specify a directory to backup packages into, defaults to a backup directory within your Drupal root.', ); $update_suboptions = array( '--lock' => array( @@ -954,6 +953,16 @@ function drush_pm_post_pm_update() { drush_backend_invoke('updatedb'); } +/** + * Validate callback for updatecode command. Abort if 'backup' folder exists. + */ +function drush_pm_updatecode_validate() { + $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT'); + if (is_dir($drupal_root . DIRECTORY_SEPARATOR . 'backup')) { + return drush_set_error('DRUSH_PM_BACKUP_DIR_FORBIDDEN', dt('It\'s a security risk to have a backup folder in your Drupal root. You must move it elsewhere before proceeding.')); + } +} + /** * Post-command callback for updatecode. Notify about any pending DB updates. */ @@ -1197,6 +1206,16 @@ function pm_drush_engine_package_handler() { */ function pm_drush_engine_version_control() { return array( + 'backup' => array( + 'options' => array( + '--version-control=backup' => 'Default engine. Backup all project files before updates.', + ), + 'sub-options' => array( + '--version-control=backup' => array( + '--backup-dir' => 'Specify a directory to backup packages into. Defaults to .drush-backups within the home directory of the user running the command. It is forbidden to specify a directory inside your drupal root.', + ), + ), + ), 'svn' => array( 'signature' => 'svn info %s', 'options' => array( @@ -1218,16 +1237,6 @@ function pm_drush_engine_version_control() { 'drush [command] cck --svncommitparams=\"--username joe\"' => 'Commit changes as the user \'joe\' (Quotes are required).' ), ), - 'backup' => array( - 'options' => array( - '--version-control=backup' => 'Backup all project files before updates.', - ), - 'sub-options' => array( - '--version-control=backup' => array( - '--backup-dir' => 'Backup destination directory. Defaults to a "/backup" subdirectory inside your Drupal root.', - ), - ), - ), 'bzr' => array( 'signature' => 'bzr root %s', 'options' => array( diff --git a/commands/pm/version_control/backup.inc b/commands/pm/version_control/backup.inc index db7f4a33a31586f03af9a37a6ba4174899c7245e..02b2e4a5ece889c0e2ac71855a8da0b69d481b20 100644 --- a/commands/pm/version_control/backup.inc +++ b/commands/pm/version_control/backup.inc @@ -16,14 +16,15 @@ class drush_pm_version_control_backup implements drush_pm_version_control { // Save the date to be used in the backup directory's path name. $date = date('YmdHis', $_SERVER['REQUEST_TIME']); - $backup_dir = drush_get_option('backup-dir', $drupal_root . '/backup'); - $backup_dir = rtrim($backup_dir, '/'); - @drush_op('mkdir', $backup_dir, 0777); - $backup_dir .= '/modules'; - @drush_op('mkdir', $backup_dir, 0777); - $backup_dir .= "/$date"; - @drush_op('mkdir', $backup_dir, 0777); - $backup_target = $backup_dir . '/'. $project['name']; + $backup_dir = drush_get_option('backup-dir', $_SERVER['HOME'] . DIRECTORY_SEPARATOR . '.drush-backups'); + if (strpos($backup_dir, $drupal_root) === 0) { + return drush_set_error('DRUSH_PM_BACKUP_FAILED', dt('It\'s not allowed to store backups inside the Drupal root directory.')); + } + $backup_dir = rtrim($backup_dir, DIRECTORY_SEPARATOR); + $backup_dir .= DIRECTORY_SEPARATOR . $date . DIRECTORY_SEPARATOR . 'modules'; + drush_mkdir($backup_dir); + + $backup_target = $backup_dir . DIRECTORY_SEPARATOR . $project['name']; // Save for rollback or notifications. $project['backup_target'] = $backup_target; if (!drush_op('rename', $project['full_project_path'], $backup_target)) {