When I run drush quickstart-create I get an on screen report during the process:

"You are about to DROP your 'testqstrt_local' database and then CREATE a new one. Do you want to continue? (y/n): y"

Is it possible to change the default behaviour to dump any existing database to a SQL file at a defined path, similar to the '.old' folder that is created for any existing website files?

I've been playing with this for a little while but can't find a solution.

Cheers,
Crom

Comments

MichaelCole’s picture

Category: support » feature

Hi Crom, this is an interesting idea. Yes, it should do this, and would be easy to accomplish inside the drush command.

In the meantime, here's what I do in a shell script. This script backups up the database if the file doesn't exist, and restores it if it does:

quickstart@qs091:~/websites$ cat reinstall_me.sh 
#!/bin/bash

# change to website folder passed in from command line as parameter 1
cd ~/websites/$1

# does backupfile not exist?
if [ ! -f backup.sql.gz ] 
then
  # Not exist, then clear Drupal caches
  drush -y cc all      
  # backup database
  drush sql-dump --gzip --result-file=backup.sql
fi

# in all cases, empty the database
drush sql-drop -y
# restore from backup.  Unzip and pipe to mysql
gzip -d backup.sql.gz -c | `drush sql-connect`

#firefox http://$1 &

Room for improvement, but this is a good start if you want to write a shell script yourself.

Cheers,

Mike

Crom’s picture

Thanks Mike - I used the commands in your script to get what I wanted done which is great.

I've used a function within your quickstart.inc file and tweaked it to do the db check and backup. Some of the lines are specific to my requirements/setup but in case it's useful here it is - I'm sure it can ben improved but it works so happy for the moment ;-)

Can't work out how to configure that Drush y/n check but I'm only starting with drush...

Cheers,
Crom

/**
 * Create the database.  Runs SQL.  Replaces .'s with _'s in domain name
 * to get the database name, database username, and password.
 *
 * @param <type> $domain
 * @param <type> $params
 */
function quickstart_create_database($domain, $params) {
  $dbuser = $params['dbuser'];
  $dbpassword = $params['dbpassword'];
  $domain_ = $params['domain_'];
  $dbdumpdir = quickstart_fixpath("/srv/www/" . $domain . "/db_dumps");
  $codepath = $params['codepath'];
  
  //create directory for database dumps
  if (!file_exists($dbdumpdir)) {
    quickstart_shell_exec("mkdir -p $dbdumpdir");
    drush_log("Created database dump directory at $dbdumpdir", "ok");
  }
  //backup database if it exists
  $link = mysql_connect('localhost', $dbuser, $dbpassword);
  if (!$link) {
    echo "Can't connect to MySQL server!\n";
  }else{
    echo "Connected to MySQL server - testing for existing database\n";
  	// make $domain_ the current db
    $db_selected = mysql_select_db($domain_, $link);
    
    if (!$db_selected) {
      echo "Database $domain_ does not exist...skipping backup\n";
    }else{
      // Clear Drupal caches
      drush_backend_invoke("-r $codepath cc all");    
      // backup database
      $logfilename = $dbdumpdir.'/backup_'.date("Ymd--His").'.sql.gz';
      drush_backend_invoke("-r $codepath sql-dump --gzip --result-file=$logfilename");
      if (file_exists($logfilename)) {
        echo "DB correctly backed up to $logfilename...continuing";
      }else{
        $ech = chr(27)."[1;33m"."Database backup file at $logfilename not found".chr(27)."[0m".chr(27)."[0;31m"." Are you sure you want to do this?  Type 'yes' to continue: ".chr(27)."[0m";
    	echo $ech;
        $handle = fopen ("php://stdin","r");
        $line = fgets($handle);
        if(trim($line) != 'yes'){
          echo "ABORTING!\n";
          exit;
        }
      }
    }
  }
   
 $sql = str_replace("#DOMAIN#", $domain_, DATABASE_CREATE);
 quickstart_mysql_exec($sql, $dbuser, $dbpassword);
}
MichaelCole’s picture

Hi you might be able to change:

drush_backend_invoke("-r $codepath sql-dump --gzip --result-file=$logfilename");

to:

drush_backend_invoke("-y -r $codepath sql-dump --gzip --result-file=$logfilename");

Haven't tested it.

MichaelCole’s picture

Component: Code » Drush
kenorb’s picture

Is the --gzip argument is supported? I can't find it in --help?

MichaelCole’s picture

Uuuuuuhhhh.... Neither can I D-: It probably doesn't exist and is ignored. Examples online pipe through gzip. Mea culpa.

DjebbZ’s picture

If I'm not mistaking, the "DROP database and CREATE a new one" stuff comes from the drush site-install command. Should be better to file an issue there to change the options of the command, so that drush qc just have to rely on it instead of "forking" it. I'm pretty sure there was already an issue in Drush about not DROPing the database but only the tables (found it : #1105514: site-install - Never try to drop an existing database. Just drop tables.). Drush folks may be happy about adding an option to back it up before.

kenorb’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

Drupal 5 and Drupal 6 is no longer officially supported. If you think this issue is still relevant for 8.x, feel free to re-open.