Get the App
SLTechnology News&Howtos  ›  Database  › 

Mysqldump+binlog backup script

Shulou Source: shulou.com Published: 2022-06-01 21:40:38 09月30日 Update

# # 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 {}\

Tags: Directory file time complete backup parameter command first time update script Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL OPPO Reno macOS Apple