当前位置:网站首页>自动定时备份远程mysql脚本
自动定时备份远程mysql脚本
2022-07-28 05:22:00 【Alex_z0897】
脚本完整流程
- 连接远程服务器
- 进入docker-mysql
- 执行mysqladmin -P3306 -u u s e r − p user -p user−ppwd1 flush-logs,对binlog进行截断,返回binlog文件名
- 本地拿到文件名,从远程服务器拉取对应文件到本地
- 执行mysqlbinlog /bak/incre_dc/ b ∣ m y s q l − u b | mysql -u b∣mysql−uacc -p$pwd1 -f, 把binlog文件导入本地mysql
- 把incre.sh加入本地定时任务crontab -e 即可,两天执行一次
#0 1 */2 * * cd /incre_micro;./incre.sh #这里的cd 类似于docker的workspace - 加入了上一次备份时间的判断
关于一些shell脚本使用:
- 变量赋值等号两边不能有空格
#shell : command not found
a="a=000017"
a_str="a="
- 字符串包含[[]]是关键,两边空格一个都不能少
# shell [: missing `]
if [[ "$a" == *$a_str* ]];then ...
if [[ "$a" =~ $a_str* ]];then ...
- shell截取字符串
e="mysql-bin.000016|2021-01-27 11:01:35"
${e%|*}
# mysql-bin.000016
- shell数字加减
a='154'
b=$(($a-1))
echo $b
#大于 -gt (greater than)
#小于 -lt (less than)
#大于或等于 -ge (greater than or equal)
#小于或等于 -le (less than or equal)
#不相等 -ne (not equal)
- shell显示文件倒数第二行
tail -n 2 history.log |head -n 1
- shell时间格式化
echo "`date '+%Y-%m-%d %H:%M:%S'`"
完整的脚本
incre.sh
#!/bin/bash
#服务地址
srv=127.0.0.1
#备份天数
delay=1
#数据库所在目录(remote_db)
dir=/data/
#账号(local_db)
acc=root
#密码(local_db)
pwd1=root
#容器名(local_db)
container_local=mysql_1
#历史备份记录
history_file=history.log
#服务器执行日志
file_name=file.log
perfix=mysql-bin.
a=0
a_str='a='
echo "$(ssh -Tq $srv < ./ssh.sh)" > $file_name
a=$(tail -n 1 $file_name)
echo "$a"
b=$perfix${a:2}
c_date=`date '+%Y-%m-%d %H:%M:%S'`
d_date=`date '+%Y-%m-%d'`
if [ -f $history_file ];then
temp_h=$(tail -n 1 $history_file)
temp_d=${temp_h#*|}
tmep=${temp_h%|*}
echo "$b $temp $((${d_date//-}-$delay)) "
#上次备份时间>(当天-delay天数)
if [ ${temp_d//-} -gt $((${d_date//-}-$delay)) ];then
echo "$c_date $b 备份失败:上次备份时间:$temp_d"
exit
fi
fi
if [[ $a != 0 ]] && [[ "$a" =~ $a_str* ]];then
echo "Contains the correct file name !!!"
else
echo "$c_date $b 备份失败:格式有误(例:a=00001)"
exit
fi
echo "`date '+%Y-%m-%d %H:%M:%S'` scp 开始!!! $b "
echo $srv:$dir$b
scp $srv:$dir$b ./
if [ ! -f $history_file ];then
echo "首次执行备份"
echo "$b|$d_date" > $history_file
else
echo "$b|$d_date" >> $history_file
fi
echo "`date '+%Y-%m-%d %H:%M:%S'`,开始导入数据库"
echo "exec ==> docker exec $container_local bash -c \"mysqlbinlog /bak/incre_dc/$b | mysql -u$acc -p$pwd1 -f \""
docker exec $container_local bash -c "mysqlbinlog /bak/incre_dc/$b | mysql -u$acc -p$pwd1 -f"
echo "`date '+%Y-%m-%d %H:%M:%S'`,导入完成"
echo "`date '+%Y-%m-%d %H:%M:%S'` $b 备份结束"
exit
把此脚本发远程执行
cat ssh.sh
#!/bin/bash
#数据库所在目录(remote_db)
dir=/data/
#用户名(remote_db)
user=root
#密码(remote_db)
pwd1=root
#容器名(remote_db)
container_remote=mysql-m1_1
index=mysql-bin.index
sp=mysql-bin.
d_date=`date '+%Y%m%d'`
cd $dir
ft=$(stat -c %Y $index)
ft=`date -d @$ft "+%Y%m%d"`
echo "$d_date $ft"
#当前时间(天)>上次修改时间(天)
if [ $d_date -gt $ft ]; then
docker exec $container_remote mysqladmin -P3306 -u$user -p$pwd1 flush-logs
echo "当天已flush"
else
echo "当天已flush,无须重复执行,$d_date,$ft"
fi
#取倒数第二行文件名
a=$(tail -n 2 $index | head -n 1| awk -F$sp '{print $2 }')
echo "a=$a"
=========================================>>>>>>>>>>>>>>>>分割线
很久之前撸的脚本,比较low,
顺便回顾一下shell的基本使用,
边栏推荐
- MySQL trigger
- Phoenix
- Two methods of covering duplicate records in tables in MySQL
- 微信小程序开发详细步骤是什么?
- 撞脸ins? Mars数字藏品平台探索数字藏品轻社交
- 初识Flask
- Xshell suddenly failed to connect to the virtual machine
- Books - investment ideas and Strategies
- Mars数*字*藏*品*平*台守卫者计划细节公布
- 1: Why should databases be divided into databases and tables
猜你喜欢

XShell突然间无法连接虚拟机

tcp和udp的区别和联系

On July 7, the national wind 24 solar terms "Xiaoshu" came!! Attachment.. cooperation.. completion.. advance.. report

Idempotent component

Manually create a simple RPC (< - < -)

数字藏品成文旅产业新热点

Data warehouse tool superset installation (fully available)

【4】 Redis persistence (RDB and AOF)

小程序搭建制作流程是怎样的?

Micro service architecture cognition and service governance Eureka
随机推荐
Variables, process control and cursors
The project does not report an error, operates normally, and cannot request services
4个角度教你选小程序开发工具?
分布式集群架构场景化解决方案:集群时钟同步问题
mysql5.6(根据.ibd,.frm文件)恢复单表数据
Interface anti duplicate submission
Structured streaming in spark
Sqoop安装及使用
Data warehouse tool superset installation (fully available)
mysql视图,存储过程与存储函数
预告来袭:【豆冰冰】发.售,现.金.抽.奖等你来拿
字节Android岗4轮面试,收到 50k*18 Offer,裁员风口下成功破局
JS!!
【5】 Redis master-slave synchronization and redis sentinel (sentinel)
Mars数*字*藏*品*平*台守卫者计划细节公布
Phoenix
【3】 Redis features and functions
tcp和udp的区别和联系
Linux(centOs7) 下安装redis
Sales notice: on July 22, the "great heat" will be sold, and the [traditional national wind 24 solar terms] will be sold in summer.