MYSQL binary log management script is used to flush the database every hour to generate new binary logs, back up binary logs to NFS, and compress and store them :#! /Bin/bash # Purpose: Manages mysql binary logs, refreshes binary logs every hour, and copies the logs to the nfs server for future recovery and problem analysis! # Author: carl_zhang # Date: www.2cto.com # some variables parameters # define database information USERNAME = rootPASSWORD = zhang @ 123 HOSTIP = localhostMYSQL =/usr/local/mysql/bin/mysql # Set the binary Log Path BIN_LOG_DIR =/data/dbdata # Set the binary log backup path BIN_LOG_BACKUP_DIR =/data/binlog_backup # define a constant to skip the last log file COUNT = 0 # define a log file LOGFILE =/data /binlog_backup/binlog_backup.log # locate the binary log name prefix BINLOG_PREFIX = 'grep "log-bin ="/etc/my. cnf | awk-F' = ''{print $2}'' # Compare binary data Log File List and backup binary file list if [! -F "$ BIN_LOG_BACKUP_DIR/backup. file "]; thentouch $ BIN_LOG_BACKUP_DIR/backup. filefi # Run flush logs $ MYSQL-u $ USERNAME-p $ PASSWORD-h $ HOSTIP-e "flush logs" FILE_LIST = 'comm-23 $ BIN_LOG_DIR/ $ BINLOG_PREFIX.index $ BIN_LOG_BACKUP_DIR/backup. file '# count the number of log files FILE_COUNT = 'comm-23 $ BIN_LOG_DIR/$ BINLOG_PREFIX.index $ BIN_LOG_BACKUP_DIR/backup. file | wc-l '# program body # start to back up the binary log file for file in $ FILE_LISTdoBINLOG _ NAME = 'basename $ file' let COUNT = $ COUNT + 1if [$ COUNT-eq $ FILE_COUNT]; then # skip the latest binary log file echo "skip the lastest binlog file" >>$ LOGFILEelsecp $ BIN_LOG_DIR/$ BINLOG_NAME $ BIN_LOG_BACKUP_DIR/if [$? -Eq 0]; thenecho "'date-d" today "+ % Y-% m-% d-% H-% M-% s' $ BINLOG_NAME backup success"> $ LOGFILEelseecho "'date -d "today" + % Y-% m-% d-% H-% M-% s' $ BINLOG_NAME backup faild, please check it out ">>$ LOGFILE www.2cto.com exit 5fi # compress binary logs and store gzip $ BIN_LOG_BACKUP_DIR/$ BINLOG_NAMEif [$? -Eq 0]; thenecho "'date-d" today "+ % Y-% m-% d-% H-% M-% s' $ BINLOG_NAME gzip success"> $ LOGFILEelseecho "'date -d "today" + % Y-% m-% d-% H-% M-% s' $ BINLOG_NAME gzip faild, please check it out ">>$ LOGFILEexit 5 fiecho. /$ BINLOG_NAME> $ BIN_LOG_BACKUP_DIR/backup. filfidone by Feihong