数据库备份从生产环境备份到客户环境

采用shell脚本方案,从生产环境取数据,备份文件到本地;将备份文件远程复制到客户服务器,再通过定时器还原文件到数据库。

同步的3步操作

1.从数据库上下载脚本到当前服务器【生产服务器】
调用脚本:
syncfrom.sh

#! /bin/bash
source /etc/profile
db=test
for table in `cat /opt/tmp.txt`
do
        echo $table
        mysqldump --defaults-extra-file=.my.cnf $db $table --where="1= 1" --set-gtid-purged=OFF > /opt/backup/$table.sql

2.将当前备份脚本传输到客户服务器【生产服务器】
调用脚本:
sync_file_to_nice2.sh

#!/bin/bash

for table in `cat /opt/tmp.txt`
do
        echo $table
        /opt/sync_file_to_nice.sh $table
done


这里面因为需要expect脚本
所以或循环调用sync_file_to_nice.sh脚本

#!/usr/bin/expect -f
set passwd "abcabc"
set table [lindex $argv 0]

spawn scp /opt/backup/$table.sql root@192.1.1.1:/opt/backup/$table.sql
expect {
   "pass"
        {
          send "$passwd\n"
        }
   "yes/no"
        {
          sleep 5
          send_user "send yes"
          send "yes\n"
        }
   eof
    {
        sleep 5
        send_user "eof\n"
    }
}
set timeout 3000
send "exit\r"
expect eof

3.客户服务器将脚本进行还原【客户服务器】
调用脚本:nice_sync.sh

#!/bin/bash
db=backup
echo "start"
for table in `cat tmp.txt`
do
        echo $table
        mysql --defaults-extra-file=.my.cnf $db < /opt/backup/$table.sql
done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值