stubarny Posted May 5, 2012 Share Posted May 5, 2012 Hello, I am trying to copy a single sql table from a mysql database on one server to another mysql database on another server. I need to do this twice a day so really would like an automated php/sql cronjob solution - please could you tell me if this is possible? Thanks for your help, Stu Quote Link to comment Share on other sites More sharing options...
smoseley Posted May 7, 2012 Share Posted May 7, 2012 mysqldump remotedb > /path/to/file scp remote:/path/to/file /localpath/to/file mysql localdb < /localpath/to/file Quote Link to comment Share on other sites More sharing options...
smoseley Posted May 7, 2012 Share Posted May 7, 2012 Or you could cluster the second DB as a slave to keep it in sync with the main DB. Quote Link to comment Share on other sites More sharing options...
smoseley Posted May 7, 2012 Share Posted May 7, 2012 mysqldump remotedb > /path/to/file scp remote:/path/to/file /localpath/to/file mysql localdb < /localpath/to/file Duh... What am I saying!? You could just add this to your daily crontab: mysqldump -h remotehostname -u remoteuser -p remotepassword remotedbname | mysql -u localuser -p localpassword localdbname In fact, I would add a shell script that does the following in sequence (grabbed the date stuff online... may need some checking).... and add this to your crontab #!/usr/bin/ksh # GET TODAY'S DATE date '+%m %d %Y' | { read MONTH DAY YEAR TODAY="$YEAR-$MONTH-$DAY" DAY=`expr "$DAY" - 1` case "$DAY" in 0) MONTH=`expr "$MONTH" - 1` case "$MONTH" in 0) MONTH=12 YEAR=`expr "$YEAR" - 1` ;; esac DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1` esac # GET YESTERDAY'S DATE YESTERDAY="$YEAR-$MONTH-$DAY" } # CREATE A NEW DB FOR TODAY mysql -u localuser -p localpassword -e'CREATE DATABASE `dbname-$TODAY`' # COPY DATA FROM REMOTE mysqldump -h remotehostname -u remoteuser -p remotepassword remotedbname | mysql -u localuser -p localpassword 'dbname-$TODAY' # DROP DB FROM YESTERDAY mysql -u localuser -p localpassword -e'DROP DATABASE `dbname-$YESTERDAY`' Quote Link to comment Share on other sites More sharing options...
stubarny Posted May 8, 2012 Author Share Posted May 8, 2012 Many thanks, that's great! Quote Link to comment Share on other sites More sharing options...
fenway Posted May 12, 2012 Share Posted May 12, 2012 Many thanks, that's great! Unless it's InnoDB. Quote Link to comment Share on other sites More sharing options...
smoseley Posted May 12, 2012 Share Posted May 12, 2012 Unless it's InnoDB. How do you figure? Did you look at the latest version I made? It's creating a new DB backup every day. Quote Link to comment Share on other sites More sharing options...
fenway Posted May 19, 2012 Share Posted May 19, 2012 Did you specify "single-transaction"? Otherwise, your snapshot is inconsistent. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.