MySQL backup and recovery strategy (c)

mysqlDailyBackup.sh Notes: 

! # / bin / SH
# the Name: mysqlDailyBackup.sh
# PS:. MySQL Daily Backup DataBase
# by the Write: i.Stone
# Last the Modify: 2007-11-17
#
# define variables, tailored to the specific circumstances
#define database directory and data directory
scriptsDir = `pwd`
MySqlDir = / usr / local / MySQL
dataDir = $ MySqlDir / the data
# define the user name and password for the backup of the database
the user = root
UserPwd = 111111
# define a backup directory, daily backup files backed up to $ dataBackupDir / Daily
dataBackupDir = / tmp / mysqlbackup
dailyBackupDir = $ dataBackupDir / Daily
# define the body of the message file
eMailFile = $ dataBackupDir / email.txt
# define e-mail address
eMail = [email protected]
# define log file
logFile = $ dataBackupDir / mysqlbackup. log
# Give the host name of the host database resides
HOSTNAME = `the uname -n` # echo" "> $ eMailFile echo $ (DATE +"% Y-% M-% D% H:% M:% S ") >> $ eMailFile # # refresh the log, the database using the new binary log file $ MySqlDir / bin / mysqladmin -u $ -p $ UserPwd the User flush-logs cd $ dataDir # get the binary log list fileList CAT = `$ HOSTNAME-bin.index` iCounter 0 = for File in $ fileList do   iCounter = `expr $ iCounter + 1` DONE nextNum = 0 iFile = 0 for File in $ fileList do   binLogName =` basename $ file`   nextNum = `expr $ nextNum + 1` # skipped last a binary log (binary log file database currently in use)   IF [[$ nextNum == $ iCounter]]; the then     echo> / dev / null "Skip lastest!"   the else
 
























    dailyBackupDir = $ dest / $ binLogName
# skip binary log files have been backed up
    IF [[-e $ dest]]; the then
      "! $ binLogName Skip exist" echo> / dev / null
    the else
# backup log files to the backup directory
      cp $ $ dailyBackupDir binLogName
      IF [? [$ == 0]]; the then
        iFile iFile = `expr $ + 1`
        "! $ binLogName Backup Success "echo >> $ eMailFile
      fi
    fi
  fi
DONE
IF [[$ iFile == 0]] ; the then
  "! No Binlog Backup" echo >> $ eMailFile
the else
  echo "Backup $ iFile File (S)." >> $ eMailFile
  echo "Backup MySQL Binlog the OK!" >> $ eMailFile

# If you do not transfer the backup to the backup server or backup server for Windows, set the standard green line commented out
# the Move the To Backup Files Backup Server.
# For Linux (MySQL server) to Linux (backup server)

  $scriptsDir/rsyncBackup.sh
  if [[ $? == 0 ]]; then
    echo “Move Backup Files To Backup Server Success!” >> $eMailFile
  else
    echo “Move Backup Files To Backup Server Fail!” >> $eMailFile
  fi
fi
# 发送邮件通知
cat $eMailFile | mail -s “MySQL Backup” $eMail
# 写日志文件
echo “——————————————————–” >> $logFile
cat $eMailFile >> $logFile

rsyncBackup.sh Notes:

! # / bin / SH
# the Name: rsyncBackup.sh
# PS: the Move the To Backup Files Backup Server.
# by the Write: i.Stone
# Last the Modify: 2007-11-17
#
# Please modified depending on the circumstances, attention has finally " / "
# define the database backup directory
dataBackupDir = / tmp / mysqlbackup /
# to store backup data on the backup server defined directory
backupServerDir = / root / mysqlbackup /
# define the backup server
backupserver = 192.168.0.200
#
# synchronize backup files to the backup server
rsync - a -delete $ dataBackupDir -e ssh $ backupServer : $ backupServerDir> / dev / null 2> & 1

rmBackup.sh Notes:

# / bin / SH!
# the Name: rmBackup.sh
# PS:. the Delete Old Backup
# by the Write: i.Stone
# Last the Modify: 2007-11-15
#
# define the backup directory
dataBackupDir = / tmp / mysqlbackup
# delete mtime> 2 log backup file
Find $ dataBackupDir -name. "_ * GZ MySQL" -type F -mtime +2 -exec RM {} \;> / dev / null 2> &. 1

(5)  , to restore the state of the database backup

Mysqldump backup file out with a can directly into the SQL script , direct use of mysql client can be imported. 

/usr/local/mysql/bin/mysql -uroot -pUserPWD db_name < db_name.sql

For any applicable updates log them as input mysql of: 

  % ls -t -r -1 HOSTNAME-bin* | xargs mysqlbinlog | mysql -uUser -pUserPWD 

ls  command to generate a separate log file update list generated according to their order ordering server (Note: If you modify any file, you will change the sort order, which will lead to the update log is using the wrong order)

This set of backup strategy can only be restored to the state when the last backup of the database, to be lost in the time of the crash data as little as possible should be backed up more frequently, in order to restore the data to the collapse of the state use master-slave replication (replication ) . If you use this set of backup scripts, log files and data files on different disks is a good doctrine, so not only can improve data write speed, but also to make the data more secure.

This switched: http://blog.thematice.com  Author: porridge country

Reproduced in: https: //www.cnblogs.com/lvsong/archive/2010/07/30/1788905.html

Guess you like

Origin blog.csdn.net/weixin_34347651/article/details/92959487