当前位置:网站首页>Automatic scheduled backup of remote MySQL scripts
Automatic scheduled backup of remote MySQL scripts
2022-07-28 06:07:00 【Alex_ z0897】
Script complete process
- Connect to remote server
- Get into docker-mysql
- perform mysqladmin -P3306 -u u s e r − p user -p user−ppwd1 flush-logs, Yes binlog Cut off , return binlog file name
- Get the file name locally , Pull the corresponding file from the remote server to the local
- perform mysqlbinlog /bak/incre_dc/ b ∣ m y s q l − u b | mysql -u b∣mysql−uacc -p$pwd1 -f, hold binlog Import the file to local mysql
- hold incre.sh Join the local scheduled task crontab -e that will do , Once every two days
#0 1 */2 * * cd /incre_micro;./incre.sh # there cd Be similar to docker Of workspace - Added the judgment of the last backup time
About some shell Script use :
- There must be no spaces on both sides of the equal sign of variable assignment
#shell : command not found
a="a=000017"
a_str="a="
- The string contains [[]] Is the key , There should be no less space on both sides
# shell [: missing `]
if [[ "$a" == *$a_str* ]];then ...
if [[ "$a" =~ $a_str* ]];then ...
- shell Intercepting string
e="mysql-bin.000016|2021-01-27 11:01:35"
${e%|*}
# mysql-bin.000016
- shell Add and subtract numbers
a='154'
b=$(($a-1))
echo $b
# Greater than -gt (greater than)
# Less than -lt (less than)
# Greater than or equal to -ge (greater than or equal)
# Less than or equal to -le (less than or equal)
# It's not equal -ne (not equal)
- shell Display the penultimate line of the file
tail -n 2 history.log |head -n 1
- shell Time format
echo "`date '+%Y-%m-%d %H:%M:%S'`"
Full script
incre.sh
#!/bin/bash
# Service address
srv=127.0.0.1
# Backup days
delay=1
# Database directory (remote_db)
dir=/data/
# account number (local_db)
acc=root
# password (local_db)
pwd1=root
# Container name (local_db)
container_local=mysql_1
# Historical backup records
history_file=history.log
# Server execution 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)) "
# Last backup time >( same day -delay Days )
if [ ${temp_d//-} -gt $((${d_date//-}-$delay)) ];then
echo "$c_date $b Backup failed : Last backup time :$temp_d"
exit
fi
fi
if [[ $a != 0 ]] && [[ "$a" =~ $a_str* ]];then
echo "Contains the correct file name !!!"
else
echo "$c_date $b Backup failed : Wrong format ( example :a=00001)"
exit
fi
echo "`date '+%Y-%m-%d %H:%M:%S'` scp Start !!! $b "
echo $srv:$dir$b
scp $srv:$dir$b ./
if [ ! -f $history_file ];then
echo " Perform backup for the first time "
echo "$b|$d_date" > $history_file
else
echo "$b|$d_date" >> $history_file
fi
echo "`date '+%Y-%m-%d %H:%M:%S'`, Start importing database "
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'`, Import complete "
echo "`date '+%Y-%m-%d %H:%M:%S'` $b End of backup "
exit
Send this script to remote execution
cat ssh.sh
#!/bin/bash
# Database directory (remote_db)
dir=/data/
# user name (remote_db)
user=root
# password (remote_db)
pwd1=root
# Container name (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"
# current time ( God )> Last modified ( God )
if [ $d_date -gt $ft ]; then
docker exec $container_remote mysqladmin -P3306 -u$user -p$pwd1 flush-logs
echo " The day has flush"
else
echo " The day has flush, There is no need to repeat ,$d_date,$ft"
fi
# Take the file name on the penultimate line
a=$(tail -n 2 $index | head -n 1| awk -F$sp '{print $2 }')
echo "a=$a"
=========================================>>>>>>>>>>>>>>>> Split line
Script from a long time ago , Compare low,
By the way shell Basic use of ,
边栏推荐
- Chapter 8 aggregate function
- 更新包与已安装应用签名不一致
- Marsnft: how do individuals distribute digital collections?
- Addition, deletion and modification of data processing; constraint
- alpine,debian替换源
- Centos7 installing MySQL
- 小程序开发系统有哪些优点?为什么要选择它?
- Pytorch deep learning single card training and multi card training
- raise RuntimeError(‘DataLoader worker (pid(s) {}) exited unexpectedly‘.format(pids_str))RuntimeErro
- Tensorboard visualization
猜你喜欢

Distributed cluster architecture scenario optimization solution: session sharing problem

Manually create a simple RPC (< - < -)

Dataset类分批加载数据集

Sales notice: on July 22, the "great heat" will be sold, and the [traditional national wind 24 solar terms] will be sold in summer.

搭建集群之后崩溃的解决办法

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

小程序开发

微信小程序制作模板套用时需要注意什么呢?

使用神经网络实现对天气的预测

Kotlin语言现在怎么不火了?你怎么看?
随机推荐
Mars number * word * Tibet * product * Pingtai defender plan details announced
项目不报错,正常运行,无法请求到服务
【7】 Consistency between redis cache and database data
Interface anti duplicate submission
4个角度教你选小程序开发工具?
CertPathValidatorException:validity check failed
uView上传组件upload上传auto-upload模式图片压缩
用于排序的sort方法
小程序开发流程详细是什么呢?
Phoenix
word2vec和bert的基本使用方法
小程序商城制作一个需要多少钱?一般包括哪些费用?
Sorting and paging, multi table query after class exercise
字节Android岗4轮面试,收到 50k*18 Offer,裁员风口下成功破局
Regular verification rules of wechat applet mobile number
微信小程序开发制作注意这几个重点方面
【5】 Redis master-slave synchronization and redis sentinel (sentinel)
flutter webivew input唤起相机相册
Flume installation and use
First meet flask