您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“mysql的備份腳本分享”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
#!/bin/bash ################################################################# # # # Purpose: MySQL backups # # This script does a full or incremental backup # # The full is done via mysqldump# # The incremental is done via flush-logs option # # with mysqladmin# # # # The script is also setup to email if a # # failure occurs # # # # Assumptions: # # 1. Database is in Binary Update Log mode # # 2. The variables are set correctly # ## #Usage:mysql_backup [full|incremental]# ## # # ################################################################# ################################################################# # INITILIAZE VARIABLES # ################################################################# # Set the mode, full|incremental # parameter is passed in MODE=$1 # set the username to connect to mysql for the backup MYSQLUSER=backup # set the password to connect to mysql for the backup MYSQLUSERPW=password # set the Binary Update Logs Path BINLOGPATH=/mysql/logs/emds01/mysql # Set the binary Update Logs name BINLOGNAME=viper2-bin # set the backup path BACKUPPATH=/export/home/mysql/backups # set the email address for errors to be emailed to EMAIL_ADDR=shenandoah.speers@cicadacorp.com # set the log file LOGFILE=$BACKUPPATH/logs/$HOSTNAME_$RUNDATE.log # set the error log file LOGERR=$BACKUPPATH/logs/ERRORS_$HOSTNAME_$RUNDATE.log # set the number of days to keep the backups DAYSTOKEEP=5 # set Run date RUNDATE=`date +%Y%m%d%s` # set hostname HOSTNAME=`uname -n` ################################################################# # GLOBAL FUNCTIONS # ################################################################# copyBinlogs() { # copy binlogs to backup dir echo "Copying binlogs" for FILE in `cat $BINLOGPATH/$BINLOGNAME.index` do SFILE=(${FILE/*//}) echo "- binlog $SFILE" cp $FILE $BACKUPPATH/$SFILE_$RUNDATE gzip $BACKUPPATH/$SFILE_$RUNDATE done } removeOldBackups() { # remove old backups echo "Removing old backups greater then $DAYSTOKEEP days" find $BACKUPPATH -type f -mtime +$DAYSTOKEEP -name *.gz -print -exec rm {} ; find $BACKUPPATH/logs -type f -mtime +$DAYSTOKEEP -name *.log -print -exec rm {} ; } startlog() { # IO redirection for logging touch $LOGFILE exec 6>&1 # Link file descriptor #6 with stdout. # Saves stdout. exec > $LOGFILE # stdout replaced with file $LOGFILE. touch $LOGERR exec 7>&2 # Link file descriptor #7 with stderr. # Saves stderr. exec 2> $LOGERR # stderr replaced with file $LOGERR. } stoplog() { #Clean up IO redirection exec 1>&6 6>&- # Restore stdout and close file descriptor #6. exec 1>&7 7>&- # Restore stdout and close file descriptor #7. } ################################################################# # START OF MAIN # ################################################################# case "$MODE" in 'full') # Daily backup # - copy all bin logs to backup directory # - remove old backups and logs startlog; echo "Start Mysql Daily backup `date +%c`" mysqldump -u $MYSQLUSER -p$MYSQLUSERPW --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs | gzip > $BACKUPPATH/$HOSTNAME_full_$RUNDATE.sql.gz copyBinlogs; removeOldBackups; echo "Finish Mysql Daily backup `date +%c`" stoplog; email; ;; 'incremental') # hourly backup # - flush logs # - copy all bin logs to backup directory if not already done echo "Start Mysql hourly backup `date +%c`" startlog; mysqladmin -u $MYSQLUSER -p$MYSQLUSERPW flush-logs copyBinlogs; echo "Finish Mysql hourly backup `date +%c`" stoplog; ;; *) echo "Usage: mysql_backup [full|incremental]" ;; esac # email any errors encountered if [ -s "$LOGERR" ] then cat "$LOGERR" | mail -s "ERRORS REPORTED: MySQL Backup error Log for $HOSTNAME - $RUNDATE" $EMAIL_ADDR cat "$LOGFILE" | mail -s "MySQL Backup Log for $HOSTNAME - $RUNDATE" $EMAIL_ADDR STATUS=1 else STATUS=0 fi exit $STATUS ################################################################# # END OF MAIN # #################################################################
“mysql的備份腳本分享”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。