How do I import a database through a Drush(PHP) script? I am trying to write a Drush script locally to speed up some repetitive tasks. I am running in to issues importing a database through the script. I have tried using drush_invoke and drush_invoke_process to run the equivalent of drush sql-cli &lt; C:/path/to/mysql.sql.

I have tried several different variations with the $commandline_args and $commandline_arguments with no luck and Google has not been helping me much.

Does anyone have the correct syntax for this? I would also be fine with another method that works. I would prefer one that allows for a user input path to the SQL file but hard coded would work for now.

EDIT: I am using drush scr to run the script. Just in case it makes a difference.

EDIT 2: The accepted answer from greg_1_anderson works great. To allow for using the path to the file as an argument I found that drupal_get_option works. Just add the argument --arg_name=arg_value like drush scr script.php --file=path/to/file. Then call the arg in the script file.

Full solution:

$file = drush_get_option('file');
$options = array(
  'file' =&gt; $file,
  'file-delete' =&gt; FALSE,
  'strict' =&gt; FALSE,
);
drush_invoke_process($self, 'sql-query', array(), $options);
Basically, you want to run some php code to import an SQL database dump from a file.  There is code that does this in the implementation of drush sql-sync.  It looks like this:

  $options = $global_options + $target_options + array(
    'file' =&gt; $destination_dump_path,
    'file-delete' =&gt; TRUE,
  );
  $return = drush_invoke_process($destination, 'sql-query', array(), $options, $backend_options);
  if ($return['error_status']) {
    // An error was already logged.
    return FALSE;
  }


You could also call drush sql-sync with appropriate options for a more full-featured solution (e.g. if you want to import a local database to a remote site).  For an import to a local site, though, it is more straightforward to simply use the snippet above, which essentially runs drush sql-query --file=/path/to/dump.sql.

n.b. You may simply remove (or make empty) $global_options, $target_options and $backend_options. You should also take of the file-delete flag, unless you really would like Drush to delete your sql dump for you after it has imported it (e.g. for temporary files).