Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Backup and migrate has some additional commands, but is there something "native" from drush to restore dumps created with drush?
Combining this with site aliases will be a killer feature, but it seems I can't find any doc.
Comments
Comment #1
moshe weitzman CreditAttribution: moshe weitzman commented`drush sql-connect` < example.sql will import any SQL file such as one saved by drush sql-dump. Put your own path and filename where example.sql appears. Those backticks at start and end are important.
Comment #2
corbacho CreditAttribution: corbacho commentedThat was fast. thx moshe.
Comment #3
jonhattanThis also works:
drush sql-cli < example.sql
Note this does not restore but "dumps into" (don't know if it's a correct expression). I mean, if you want a full restore you previously need to drop and recreate the database, or drop each table.
Comment #4
corbacho CreditAttribution: corbacho commentedThanks jonhattan. It's true what you mention. I created finally an alias so I'm dropping the tables first with:
`drush sql-connect` -e "show tables" -s | xargs -I "@@" `drush sql-connect` -e "DROP TABLE @@"
(Snippet found here)
I tried first similar command with drush sql-cli or drush sqlq but it's much slower (4 secs Vs minutes)
Also could be used some trick to make DROP database_name, instead of going through all tables, but didn't know where to start.
I wonder if there is room for a new command restore,
drush restore dump.sql @site
that will prompt "Are you sure you want to override @site with this dump?"
Basically it's already done with sql-sync, only difference is that you don't need to go through the first step of making the first dump.
Anyway... it's quite easy already to restore it. I was only thinking out loud. Thanks again!
Comment #5
corbacho CreditAttribution: corbacho commentedMy whole point was to make an easy and fast recover points when testing and creating patches for Drupal 7. It's easy to do it with files (cvs up -dPC), but not so straightforward with the db.
Similar to one of the functionalities of the Demo module, but faster, since it won't require php processing to restore the database.
Comment #6
moshe weitzman CreditAttribution: moshe weitzman commentedin that case, use site-install or if that is too slow, write patches for the installer :)
Comment #7
jonhattanI traditionally do use this in a local environment, with credentials in ~/.my.cnf
For a drush command it should drop the tables instead of the database, because of permissions.
Comment #9
Mark Nielsen CreditAttribution: Mark Nielsen commentedjonhattan wrote:
My understanding is that it's not necessary to drop the database or tables before restoring from a "dump" file, because the dump file contains instructions to drop the tables first and then recreate and populate them, for example:
DROP TABLE IF EXISTS `access`;
CREATE TABLE `access` (
...etc.
I've been just shoving dump files in on top of existing databases for a couple of months now, using "drush sql-cli < example.sql", and it seems to work OK...
Comment #10
loopduplicateIf you add some tables to your database by installing a module or something and then you restore a backup using
drush sql-cli < example.sql
, those new tables will be orphaned. Whether that creates a problem for you depends on your particular setup.Comment #11
loopduplicatewith that said, I do this if I haven't added tables:
Comment #12
jmsosso CreditAttribution: jmsosso at TUI commentedAs this page is the first result when you search in Google for "drush restore dump" I'm going to add a comment for the current drush version.
With drush 9 this can be easily done with:
Hope this help anyone that lands in this page in 2020 :)
Comment #13
azovsky CreditAttribution: azovsky commentedThanks @jmsosso!
drush sql:drop
anddrush sql:query ...
working good for Drush 9.x and Drupal 8.8.x!Comment #14
arruk CreditAttribution: arruk commentedAgain since this is the first thing that comes up in google:
drush sql-drop -y
drush sqlc < sites/default/files/yourbackup.sql
Comment #15
aipheshttps://www.drush.org/latest/commands/sql_query/
Trying both way sql:query and sqlc and result is a non found file. :/
So, how to target the sql file when it's in the drupal root, same level as site folder ?
Commands would be:
drush @lias sql:drop && drush @lias sql-query --file=../exports-bruts/file.mysql.gz
ordrush @lias sql:drop && drush @lias sqlc < /exports-bruts/file.mysql.gz
Comment #16
jmsosso CreditAttribution: jmsosso at TUI commented@aiphes keep in mind that for drush the current directory is the Drupal root. So if the dump is in the Drupal root, no matter from which directory you run drush, the command will be
drush sql-query --file=file.mysql.gz
If you what to use relative paths, like
../exports-bruts/file.mysql.gz
that means thatexports-bruts
should be one level up from the Drupal root. If you are not sure, the better is to use full paths, like/home/user/project/drupal/html/xxx.sql
:)Comment #17
aiphes@jmsosso
I done what I suggest and I get the same error:
dump is stored in :
/var/www/drupal-8/exports-bruts/bdd.mysql
and when I run :
So I need to use the full path to get this work.. randomly.
[error] File(s) not found: /var/www/drupal-8/exports-bruts/bdd.mysql.gz
:/Very weird thing.