Geekuprising.com Logo
Home Youth Debate About Us Clients Services Technologies Code
You are here:: Code > Databases > Sync Tables between Local and Remote MySQL Servers
spacer
Browse

Sync Tables between Local and Remote MySQL Servers

I find in development I often need to make a minor change to a table in a production application.  I'm not going to do it "live," though. Too many chances to screw stuff up.

What I do instead is make changes on my staging server (my Debian woody laptop) test the heck out if it and then sync to the production server when I'm satisfied.

I also find that I often need to pull a table from a production box down to my staging server.

Pass these little perl scripts a table name and it'll sync to or from your remote MySQL server. Beware- this syncs data AND structure, so think before you use this.

synctable_to.pl

#!/usr/bin/perl
# This'll sync a table to a remote MySQL server.
# Assumes you have mysqldump and mysql on your path
# and that you have remote access to the server.
# local_pass is your local password
# database_name is your database name
# remote_pass is the production server password
# database_name is the database name (shockingly enough).
# remote.mysql.server is the remote server
my $table=shift;
chomp($table);
if($table){
`mysqldump -al --password=local_pass --add-drop-table database_name $table \
 | mysql -h remote.mysql.server --password=remote_pass database_name`;

synctable_from.pl

#!/usr/bin/perl
# This'll sync a table *from* a remote MySQL server.
# Assumes you have mysqldump and mysql on your path
# and that you have remote access to the server.
# local_pass is your local password
# database_name is your database name
# remote_pass is the production server password
# database_name is the database name (shockingly enough).
# remote.mysql.server is the remote server
my $table=shift;
chomp($table);
if($table){
`mysqldump -al -h remote.mysql.server --password=remote_pass --add-drop-table database_name $table \
 | mysql --password=local_pass database_name`;

Discuss this Article!

Copyright 2004 © Geekuprising Internet Consultants