您好,登錄后才能下訂單哦!
公司一直是全備+binlog,但是如果單個庫出現(xiàn)問題怎么辦,在進(jìn)行完整恢復(fù)的話,第一會耽誤時間,第二會影響其它數(shù)據(jù)庫,第三如果數(shù)據(jù)量大,對于庫的恢復(fù)也是問題,所以我提出了,除了全備,還要做分庫備份,只要更靈活,出現(xiàn)問題,直接恢復(fù)單庫(方便、快捷)
下面直接上腳本
1,將MySQL自帶庫外的其他庫進(jìn)行備份
#!/bin/bash #--------------------------------------------------------- # $Name: Store_backup.sh # $Version: v1.0 # $Author: 邱月濤 # $organization: www.yi******.cn # $Create Date: 2017-06-03 # $Description: MySQL store Backup #用于MySQL 分庫備份,并保留30天數(shù)據(jù) #--------------------------------------------------------- #source /etc/init.d/functions DAY=`date +%F` BACK_DIR="/home/backup/mysqlbackup" DBUSER="root" DBPASSWD="你自己的password" BIN_PATH="/usr/bin" LOG="/home/backup/mysqlbackup/mysqlback.log" ## Close all tables and refresh log #### "$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "flush logs" #"$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "flush tables with read lock" echo "mysqldump start `date +%F\ %H\:%M\:%S`" >> "$LOG" ## Each backup database to the target location #### for i in `"$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "show databases" |grep -vE "Database|information_schema"` do if [ ! -d "$BACK_DIR"/"$i" ]; then mkdir -p "$BACK_DIR"/"$i" fi "$BIN_PATH"/mysqldump -u "$DBUSER" -p"$DBPASSWD" --default-character-set=utf8 --opt --lock-tables -f --log-error="$LOG" "$i" > "$BACK_DIR"/"$i"/"$DAY".sql done echo "mysqldump stop `date +%F\ %H\:%M\:%S`" >> "$LOG" echo "" >>"$LOG" ## Delete 30 days ago backup files #### for RM in `"$BIN_PATH"/mysql -u "$DBUSER" -p"$DBPASSWD" -e "show databases" |grep -vE "Database|information_schema"` do rm -rf "$BACK_DIR"/"$RM"/$(date +%F --date='30 days ago').sql done
效果如下:
[root@files mysqlbackup]# tree -L 1 /home/backup/mysqlbackup /home/backup/mysqlbackup ├── luence ├── hpi ├── h ├── my ├── mysql ├── mysqlback.log ├── ypp └── tao
將腳本加入crontab定時任務(wù)即可;
[root@files mysqlbackup]# crontab -l 0 4 * * * /root/shell/mysqlbackup.sh
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。