This patch adds an "drush sql import" command to compliment "drush sql dump". The advantage of doing this over "drush sql load" is that the load operation only works when transferring databases on the same machine; with import, you can dump the db on one machine, and then transfer the file to a different machine for import. If the drupal files are already set up on the target, then drush nicely takes care of mysql / postgres abstraction in a clean and tidy way.

The downside of "drush sql import" is that it is every bit as destructive as "drush sql load", but since it reads the input database from STDIN, it does not do much to protect the user who forgets the "-l mysite.com" flag, etc.

It would be very convenient for me to have this facility in drush sql, though; please let me know what you think.

Thanks,

- Greg

Comments

moshe weitzman’s picture

Status: Active » Closed (works as designed)

sql load is quite capable of dumping on one machine and then importing to another. you just have to setup the hosts in the db_url properly. this trick even works for dbs behind a firewall if you setup an ssh tunnel before running the command.

But regardless, it is true that 'dump, gzip, file transfer, unzip, import' is a common pattern. I would like for all of it to be encapsulated by one drush command. Until then, the import step on mysql can be handled by `drush sql connect < export.sql` (assumes your db is in a file called export.sql). It looks like your command is same as sql connect.

greg.1.anderson’s picture

I see. I was in fact able to confirm that the following works:

sudo -u postgres `drush sql connect` < tmp.sql

This is equivalent to drush sql import, but the syntax is a little odd. I also was able to do this:

echo "select * from users;" | sudo -u postgres `drush sql connect`

This is equivalent to drush sql query, and of course `drush sql connect` by itself is equivalent to drush sql cli. In any event, I'm happy enough to use the `drush sql connect` workaround in place of drush import.

I'll fiddle with the db_url settings; I'm reluctant to make an external pointer to my production site, though. Maybe I could make a dev site on the remote machine, and point to that. Of course, at that point I'd have to do a remote production -> dev update before doing the remote sync. I'll experiment a bit and see what I come up with.

Thank you for your thoughts and comments.

Thanks,

- Greg

moshe weitzman’s picture

you could use a read only user when pointing to production DB from a remote web site. just a thought.

greg.1.anderson’s picture

This reply is FYI only; comments are welcome, but no action is necessary.

The RO user would work well unless you made a mistake and logged in through the "shadow" configuration with an admin account. I'm guessing that drupal is not designed to allow multiple drupal instances to share the same password (with the same db prefix), so if someone accidentally got into a situation where two people were writing to the same database from different instances, corruption would likely result. Over the next year I'm going to deploy half a dozen drupal sites, a couple of which will have multiple admins of varying skill levels. Since I will only be peripherally involved with some of these sites, I want to make sure that the configuration is easy to back up and not dangerous.

I looked at a number of possible solutions last night and came up with two that I thought were acceptable.

  • Set up the "shadow" site under a name that does -not- resolve in DNS to this machine; then it will be possible to access the database via drush, but not via a web browser.
  • Set up an override theme in the settings.php of the shadow site; remove all of the normal contents of the theme template and print only an "unavailable" message.

Under this configuration, I think I like the idea of using 'drush sql load' better than a dump / remote copy / import drush command. The advantage of drush sql load is that you can set up the parameters in advance, and then address the sites by name. The parameters for the later would be more complex, although it wouldn't be too bad, I suppose, if you presume that 'drush' is on the path and appropriately configured on the remote machine.

The next thing I'm going to work on is a drush command to put a site into maintenance mode (trivial), and perhaps another command to set the site theme (in case someone wanted to use 'theme locking' to prevent admin access to the destination site during sql load or merge).

Thanks,

- Greg

moshe weitzman’s picture

For those future commands, I'm genuinely unsure how specialized we want to get. This works today: drush eval "variable_set('hello', 'world');\"

greg.1.anderson’s picture

Okay; for now I will use "drush eval" instead of making a bunch of little drush commands that wrap a single variable_set.

Thanks!

- Greg

Chris Charlton’s picture

Subscribing.

greg.1.anderson’s picture

@chris,

There is no point in subscribing to this thread; it is closed. There will be no 'sql import' command. See the thread on drush sql sync for the latest thread on pushing sites cross-machine.

- Greg

amitaibu’s picture

Category: feature » support
Status: Closed (works as designed) » Active

@greg.1.anderson,
Making it a support request then :)
How can I import a local dump file using drush sql sync?

moshe weitzman’s picture

Status: Active » Fixed

sql sync command helps you do the dump and the import in single operation. see its help and the latest example.drushrc.php (especially the sitealias section) for more info.

if you already have a sql dump and just want to import it into your drupal site, you can do

drush sql connect < FILENAME

If you are using postgres, see #2 for similar syntax.

greg.1.anderson’s picture

@anutaubu: FYI, I don't use sudo -u postgres to call drush any more; the problem with doing this is that you have to worry about the different access rights that the postgres user might have compared to the user you are logged in as. It's just not convenient. Instead, I make a file ".pgpass" in my home directory, and populate it as follows:

hostname:port:database:username:password

On my Ubuntu system, my file reads like this:

localhost:55432:*:www-data:mydatabaseuserpassword

When you do this, you can then conveniently call drush directly, just as moshe shows in #10 above.

amitaibu’s picture

[EDIT] Thanks guys, I was actually able to do it with drush sql connect | < FILE (notice the pipe after connect).

Actually, I copy the output of drush sql connect and add to that < FILE.

greg.1.anderson’s picture

Use backticks:

`drush sql connect` < FILE

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Chris Charlton’s picture

Component: Code » PM (dl, en, up ...)

Is there an explanation why the backticks are needed? Would this change in the future?

greg.1.anderson’s picture

Status: Closed (fixed) » Fixed

drush sql-connect will print out the command to connect to your sql client. Wrapping this in backticks executes the command.

drush sql-cli < FILE should also work.

See also #935796: How to restore a database dump with drush..

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dgtlmoon’s picture

@greg.1.anderson You're wrong, you can use sql-cli to import a snapshot because drush will try to bootstrap your site, which may not have any tables in the database and needs to have its snapshot loaded :)