Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysqldump+binlog backup script

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

# # pay attention to the creation of various directories and related parameter settings

# # set / root/dba/binlog_control.txt, record the binlog file time, get the time command: stat-c% y mysql_mmm_01-bin.000013

# # set / root/dba/fullbak_file.txt to record the complete directory. When executing for the first time, you need to set a directory to store the binlog generated before the backup, and then automatically update the file each time.

-- full_backup:mysql_full_backup.sh--

#! / bin/bash

# mysql param

User= "root"

Passwd= "123456"

Socket= "/ data/mysql/run/mysql.sock"

Port=3306

Bk_options= "- single-transaction-flush-logs-u$user-p$passwd-master-data=2-all-databases-socket=$socket-port=$port"

# log file

Dtime= `date +% Y% m% d% H% M% S`

Log_base= "/ root/dba/logs"

Log_file= "$log_base/full_backup_$ {dtime} .log"

# control file

Contr_file= "/ root/dba/binlog_control.txt"

Fullbak_file= "/ root/dba/fullbak_file.txt"

# full backup dir

Bk_dir_suffix= `date +% Y% m% d`

Full_dir= "/ backup/mysql_bak/mysql_$bk_dir_suffix"

[- d ${full_dir}] | | mkdir-p ${full_dir}

Echo "mkdir full backup dir: $full_dir" > > $log_file

# full backup file

Full_bk_file= "mysql_full_$bk_dir_suffix.sql"

# mysqldump

Cd $full_dir

Mysqldump $bk_options > $full_bk_file

Echo "full backup file: $full_bk_file" > > $log_file

# backup flush logs--binlog to previous full backup

/ root/dba/mysql_binlog_backup_new.sh

# update fullbak_file control file

Echo $full_dir > $fullbak_file

-- binlog_backup:mysql_binlog_backup_new.sh

#! / bin/bash

# mysql

User= "root"

Passwd= "123456"

Socket= "/ data/mysql/run/mysql.sock"

Port=3306

# log file

Dtime= `date +% Y% m% d% H% M% S`

Log_base= "/ root/dba/logs"

Log_file= "$log_base/binlog_backup_$ {dtime} .log"

# control file

Contr_file= "/ root/dba/binlog_control.txt"

Fullbak_file= "/ root/dba/fullbak_file.txt"

# binlog path

Binlog_path= "/ data/mysql/binlog/"

# binlog file format

Binlog_format= "mysql_mmm_01-bin.* [0-9] *"

# binlog backup dir

Dest_dir= `cat $fullbak_ file`

# current binlog

Curr_binlog= `mysql-u$user-p$passwd-- socket=$socket-- port=$port-e'show master status\ G' | grep "File" | awk'{print $2}'`

Echo "Current binlog: $curr_binlog" > > $log_file

# current binglog file time

Currbinlog_time= `stat-c% y $binlog_path$curr_ binlog`

Echo "Current binlog time: $currbinlog_time" > > $log_file

# lastest backup binlog file time

Previous_time= `cat $contr_ file`

Echo "Previous time: $previous_time" > > $log_file

#!-name "* $curr_binlog*" exclude current binglog

Bk_count= `find-L $binlog_path-name "$binlog_format"!-name "* $curr_binlog*"-newermt "$previous_time"!-newermt "$currbinlog_time" | wc-l`

Echo "backup files count: $bk_count" > > $log_file

If [$bk_count-gt 0]; then

Find-L $binlog_path-name "$binlog_format"!-name "* $curr_binlog*"-newermt "$previous_time"!-newermt "$currbinlog_time" | xargs ls-t > > $log_file

# find the newest bakup file

Last_file= `find-L $binlog_path-name "$binlog_format"!-name "* $curr_binlog*"-newermt "$previous_time"!-newermt "$currbinlog_time" | xargs ls-t | head-n 1`

# newest backup file time

Last_file_time= `stat-c% y $last_ file`

Echo $last_file_time > $contr_file

# cp files

Find-L $binlog_path-name "$binlog_format"!-name "* $curr_binlog*"-newermt "$previous_time"!-newermt "$currbinlog_time"-exec cp {} $dest_dir\

Fi

Find $log_base-mtime + 15-exec rm-f {}\

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report