Timed export MySQL Local data replace remote database data script sharing _linux Shell

Source: Internet
Author: User
Tags ssh rsync

Copy Code code as follows:

The requirement imports the Tb_a table from the Ser_a library on the 192.168.3.5 to the Ser_b library on the 192.168.3.6 at 5 points per day, and the table name is also called Tb_a
Server-side scripting/usr/shell_clubs/auto_sql/server.sh
#!/bin/sh
#code by Scpman
/usr/bin/find/usr/shell_clubs/auto_sql/-name "*.sql"-mtime +3|xargs rm #del Old SQL
#get New SQL
Time= ' Date +%y%m%d '
mysqldump-uroot-p123456 ser_a tb_a >/usr/shell_clubs/auto_sql/' Date +%y%m%d ' _tb_a.sql
Sleep 2
ssh-p10022 192.168.3.6 <<eee
/usr/shell_clubs/auto_sql/client.sh
EEE
Description: The server-side script will export the specified table and SSH to the target machine via the public key, calling client script
Client Script/usr/shell_clubs/auto_sql/client.sh
#!/bin/sh
#code by Scpman
#client. Sh
Time= ' Date +%y%m%d '
/usr/bin/find/usr/shell_clubs/auto_sql/-name "*.sql"-mtime +3|xargs rm #del Old SQL
/USR/LOCAL/BIN/RSYNC-VZRTOPGL--progress root@192.168.3.5::all/usr/shell_clubs/auto_sql/' date +%Y%m%d ' _tb_a.sql/ usr/shell_clubs/auto_sql/;
counts= ' Ls-l/usr/shell_clubs/auto_sql/| grep "$time" | awk ' {print $NF} ' |wc-l '
Echo $counts
If ["$counts"-eq 1]
Then
echo sql: $counts
Mysql-uroot-ptest123 &LT;&LT;FFF
Use Ser_b;
drop table tb_a;
FFF
Mysql-uroot-ptest123 Ser_b </usr/shell_clubs/auto_sql/' Date +%y%m%d ' _tb_a.sql--default-character-set=utf8
Echo--------------Done
Else
echo SQL wrong
Fi
Description: When a client script is invoked, it deletes the old SQL record under the current path and then synchronizes the tables exported by the server through rsync, and checks to see if synchronization is successful, emptying the existing old tb_a and importing the new tb_a into the library, if not synchronized successfully, Then the script exits (here you can send a mail notification before exiting)
And then the flow is through.
Now add a timer on the 192.168.3.5 (server side)
# #send SQL to 192.168.3.6
0 5 * * * su-root-c "/usr/shell_clubs/auto_sql/server.sh" >>/usr/shell_clubs/auto_sql/res.log

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.