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 Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/ 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 Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/#findComment-1343629 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. Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/#findComment-1343630 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`' Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/#findComment-1343850 Share on other sites More sharing options...
stubarny Posted May 8, 2012 Author Share Posted May 8, 2012 Many thanks, that's great! Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/#findComment-1344021 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. Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/#findComment-1344970 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. Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/#findComment-1345059 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. Link to comment https://forums.phpfreaks.com/topic/262121-copying-a-single-sql-table-from-one-server-to-another/#findComment-1346835 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.