4webby BLOG

20 11 2008

MySQL: how to synchronize remote and local databases with shell and ssh

by Daniel | hits(369)

TAGS: tips trick DB shell mysql synchronize mysqldump ssh

 

Well, while synchronizing your local developement application with a production application is quite strightforward nowadays using Capistrano or rsync, database syncronization can be sometime tricky!

But here we go with an useful tip to keep your local and remote databases synchronized using a shell console via ssh!

The prerequisite is that both databases, local and remote, must exist!

Syncronize local database with remote one:

ssh user@www.my_domain.com "mysqldump
-u my_remote_db_username --password=my_remote_db_password my_remote_db_name"
| mysql -u my_local_db_username --password=my_local_db_password --host=localhost -C my_local_db_name

Syncronize remote database with local one:

mysqldump -u my_local_db_username --password=my_local_db_password --host=localhost -C my_local_db_name | ssh user@www.my_domain.com "mysql -u my_remote_db_username --password=my_remote_db_password my_remote_db_name" 

Do a DB backup if you try this script for the first time 'cause all the data in the database that receive data will be overwritten (namely LOST).

Try it out!

 

Useful links:

view/hide comments | add comment

4webby.com

Tags

powered by 4webby.com