MySQL分庫備份
阿新 • • 發佈:2017-06-04
mysql 分庫備份 逗哥自動化運維
公司一直是全備+binlog,但是如果單個庫出現問題怎麽辦,在進行完整恢復的話,第一會耽誤時間,第二會影響其它數據庫,第三如果數據量大,對於庫的恢復也是問題,所以我提出了,除了全備,還要做分庫備份,只要更靈活,出現問題,直接恢復單庫(方便、快捷)
下面直接上腳本
1,將MySQL自帶庫外的其他庫進行備份
#!/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天數據 #--------------------------------------------------------- #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
效果如下:
[[email protected] mysqlbackup]# tree -L 1 /home/backup/mysqlbackup /home/backup/mysqlbackup ├── luence ├── hpi ├── h ├── my ├── mysql ├── mysqlback.log ├── ypp └── tao
將腳本加入crontab定時任務即可;
[[email protected] mysqlbackup]# crontab -l 0 4 * * * /root/shell/mysqlbackup.sh
本文出自 “逗哥筆記” 博客,請務必保留此出處http://qiuyt.blog.51cto.com/1229789/1931999
MySQL分庫備份