Commit e4baf129 authored by Adrian Rossouw's avatar Adrian Rossouw Committed by adrian

Fix some issues with backups, and add deploy command to re-deploy backups as new sites.

parent 18f4bb03
<?php
function provision_mysql_provision_backup(&$data, $url = NULL) {
function provision_mysql_provision_pre_backup(&$data, $url = NULL) {
provision_log("backup", "Generating mysql dump for $url.");
provision_shell_exec("mysqldump -u%s -p%s %s > sites/%s/database.sql", $data['db_user'], $data['db_passwd'], $data['db_name'], $url);
provision_shell_exec("cd sites/%; tar -rf %s database.sql; rm database.sql", $url, $data['backup_file']);
}
function provision_mysql_provision_pre_backup_rollback(&$data, $url = NULL) {
provision_path("unlink", PROVISION_SITES_PATH . "/$url/database.sql", TRUE, dt("Deleted mysql dump from sites directory"), dt("Could not delete mysql dump from sites directory"));
}
function provision_mysql_provision_post_backup(&$data, $url = NULL) {
provision_mysql_provision_pre_backup_rollback($data, $url);
}
<?php
function provision_mysql_provision_deploy_validate() {
provision_db_connect();
}
function provision_mysql_provision_deploy(&$data, $url) {
# generate a random password for use
$data['db_passwd'] = provision_password();
$data['db_name'] = _provision_mysql_suggest_db_name($data, $url);
$data['db_user'] = $data['db_name'];
_provision_mysql_new_site_db($data['db_name'], $data['db_user'], $data['db_passwd'], $data['db_host']);
_provision_mysql_import_dump(PROVISION_SITES_PATH .'/'. $url .'/database.sql', $data['db_name'], $data['db_user'], $data['db_passwd'], $data['db_host']);
}
function provision_mysql_provision_deploy_rollback(&$data, $url = NULL) {
_provision_mysql_destroy_site_db($data['db_name'], $data['db_user'], $data['db_passwd'], $data['db_host']);
}
// Rollback doesn't apply here yet. Unless we trigger a deploy of the first dump
// made. Which could go on infinitely if something is really long.
function provision_mysql_provision_post_deploy(&$data, $url = NULL) {
provision_path('unlink', 'sites/'. $url .'/database.sql', TRUE,
dt("Removed dump file @path after restoring from it"),
dt("Could not remove dump file @path"), PROVISION_PERM_ERROR);
}
......@@ -144,7 +144,7 @@ function _provision_mysql_suggest_db_name($data, $url) {
else {
// This is a last option, and not ideal
// Provision only users will trigger this mostly.
$url_suggest = substr(0, 14, str_replace(array(".", "-"), '' , ereg_replace("^www\.", "", $url)));
$url_suggest = substr(str_replace(array(".", "-"), '' , ereg_replace("^www\.", "", $url)), 0, 14);
$suggest[] = $url_suggest;
for ($i = 0; $i < 100; $i++) {
$suggest[] = $url_suggest .'_'. $i;
......
<?php
/**
* Restore command implementation
*
* This command when called will
* 1. Make a backup of the current site, before modifications are made.
* 2. Temporarily disable the site by causing apache to redirect to a help page. Restarting apache is required.
* 3. Extract the backup that is being deployd to to a temporary folder in the sites directory.
* 4. Create a new database, belonging to the site's user, and switch it around with the current site's database.
* 5. Import the old database and site.php details.
* 6. Switch around the sites directory of the current site and the backup being deployd.
* 7. Regenerate configuration files.
* 8. TODO: diagnostic to test that everything is ok?
* 9. Remove the temporary redirect and restart apache so the previous site is available again.
* 10. Remove the extranuous db and duplicate site directory.
*
* If at any time an error occurs, before step 9. It should reverse all the changes it has made,
* and leave the current site directory and database in the right place, and remove all cruft that
* was created by this process.
*/
/**
* Make sure we have a valid site being deployd, and that the file being deployd from exists
*/
function provision_drupal_provision_deploy_validate(&$data, $url = null, $backup_file = null) {
_provision_drupal_url_required();
_provision_drupal_valid_not_installed_site();
$exists = provision_path("exists", $backup_file, TRUE,
dt("Restoring site from @path"),
dt("Could not find backup file @path"),
PROVISION_FRAMEWORK_ERROR);
if ($exists) {
$data['backup_file'] = $backup_file;
}
}
/**
* Make a backup before making any changes, and add extract the file we are restoring from
*/
function provision_drupal_provision_pre_deploy(&$data, $url, $backup_file) {
$extracted = provision_path("extract", $data['backup_file'], PROVISION_SITES_PATH ."/$url",
dt('Successfully extracted the contents of @path'),
dt('Failed to extract the contents of @path'),
PROVISION_PERM_ERROR, PROVISION_FRAMEWORK_ERROR);
if ($extracted) {
$data = array_merge(provision_get_site_data($url), $data);
}
}
/**
* Remove the extracted site directory
*/
function provision_provision_pre_deploy_rollback(&$data, $url) {
_provision_recursive_delete(PROVISION_SITES_PATH ."/$url");
}
/**
* Switch the deploy directories around now that we have the new db installed
*/
function provision_drupal_provision_deploy(&$data, $url) {
// make sure it has the latest site data available
_provision_drupal_create_settings_file($data, $url);
}
// Luckily this is reversable =)
function provision_drupal_provision_deploy_rollback(&$data, $url) {
provision_drupal_provision_deploy(&$data, $url);
}
......@@ -176,7 +176,16 @@ function provision_drush_command() {
'callback arguments' => array('restore'),
'description' => 'Restore the site to a previous backup. This will also generate a backup of the site as it was.',
'arguments' => array('domain.com' => dt('The domain of the site to be restored'),
'site_backup.tar.gz' => dt('The backup to restore the site to.')),
'site_backup.tar.gz' => dt('The backup to restore the site to.')),
'bootstrap' => -1
);
$items['provision deploy'] = array(
'callback' => 'provision_command',
'callback arguments' => array('deploy'),
'description' => 'Deploy an existing backup to a new url.',
'arguments' => array('domain.com' => dt('The domain to deploy the site package to.'),
'site_backup.tar.gz' => dt('The backup to deploy.')),
'bootstrap' => -1
);
......
<?php
/**
* Implementation of hook_provision_pre_deploy
*/
function provision_apache_provision_pre_deploy(&$data, $url = NULL) {
_provision_apache_create_vhost_config($data, $url);
}
function provision_apache_provision_pre_deploy_rollback($data, $url = NULL) {
_provision_apache_delete_vhost_config($data, $url);
}
function provision_apache_provision_post_deploy(&$data, $url = NULL) {
_provision_apache_restart_apache();
}
// The old db details should have been switched back by the mysql rollback at this point.
function provision_apache_provision_post_deploy_rollback(&$data, $url = NULL) {
provision_apache_provision_post_deploy($data, $url);
}
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