Sync drupal database from server

When we need database from server to local system, we use backup_migrate module. If we have server information then we can do better. we have to run some commands and db would be downloaded, unzipped and old database would be replaced with new db. So…

  1. Download database.
ssh [username]@[server] "drush -r DRUPAL\_ROOT\_DIR sql-dump|bzip2 -c" > db.sql.bz2
  1. Now Unzip this bz2 file.
bunzip2 db.sql.bz2
  1. Delete old database.
mysqladmin -f -u [mysql-user] -p[password] drop [db\_name]
  1. Import new (recently downloaded) database.
mysqladmin -u [mysql-user] -p[password] create [db\_name] && mysql -u [mysql-user] -p[password] [db\_name] < db.sql

that’s all.

Now we can remove mysql file by “rm db.sql”.

OR

Download https://gist.github.com/crazyrohila/6157902 script and edit server & mysql information and then run script. This script will ask for server password for ssh login. we can skip that step by putting our public key (~/.ssh/id_rsa.pub) in authorized_keys file on server, more info about it can be found here ssh login without password