linux中mysql备份,增量备份及恢复程序
2019/10/10/17:34:10 阅读:1892 来源:谷歌SEO算法 标签:
SEO博客
一个linux中mysql完全备份,增量备份及恢复脚本实现程序,有需要的朋友可参考一下,可以把它做成定时备份,代码如下:
- #!/bin/bash
- #full&&incrementbackupandrecover
- #说明:事先要确保存在/data/bak目录,且要保证在执行增量备份时已做过至少一次全量备份,否则找不到position文件.
- port='3306'
- back_src_dir="/data/mysql/${port}/logs/binlog"
- back_dir='/data/bak'
- DATE=`date+%Y%m%d`
- user='root'
- pass='cy2009'
- bak_db='test1'
- mysql_bin='/usr/local/mysql-5.1.48/bin'
- socket="/data/mysql/${port}/mysql.sock"
- full_bak()
- {
- cd${back_dir}
- DumpFile=Full_back$DATE.sql
- ${mysql_bin}/mysqldump--lock-all-tables--flush-logs--master-data=2-u${user}-p${pass}${bak_db}>${DumpFile}
- ${mysql_bin}/mysql-u${user}-p${pass}--socket=${socket}-e"unlocktables"
- #把当前的binlog和position信息存入position文件
- cat${DumpFile}|grep'MASTER_LOG_FILE'|awk-F"'"'{print$2}'>${back_dir}/position
- cat${DumpFile}|grep'MASTER_LOG_FILE'|awk-F"="'{print$3}'|awk-F";"'{print$1}'>>${back_dir}/position
- }
- incre_bak()
- {
- #锁定表,刷新log
- ${mysql_bin}/mysql-u${user}-p${pass}--socket=${socket}-e"flushtableswithreadlock"
- ${mysql_bin}/mysqladmin-u${user}-p${pass}--socket=${socket}flush-logs
- #获取上次备份完成时的binlog和position
- cd${back_dir}
- start_binlog=`sed-n'1p'position`
- start_pos=`sed-n'2p'position`
- #获取目前的binlog和position
- mysql-u${user}-p${pass}--socket=${socket}-e"showmasterstatusG"|awk'{print$2}'|sed-n'2,3p'>now_position
- stop_binlog=`sed-n'1p'now_position`
- stop_pos=`sed-n'2p'now_position`
- #如果在同一个binlog中
- if["${start_binlog}"=="${stop_binlog}"];then
- ${mysql_bin}/mysqlbinlog--start-position=${start_pos}--stop-position=${stop_pos}${back_src_dir}/${start_binlog}>>Incr_back$DATE.sql
- #跨binlog备份
- else
- startline=`awk"/${start_binlog}/{printNR}"${back_src_dir}/mysql-bin.index`
- stopline=`wc-l${back_src_dir}/mysql-bin.index|awk'{print$1}'`
- foriin`seq${startline}${stopline}`
- do
- binlog=`sed-n"$i"p${back_src_dir}/mysql-bin.index|sed's/.*///g'`
- case"${binlog}"in
- "${start_binlog}")
- ${mysql_bin}/mysqlbinlog--start-position=${start_pos}${back_src_dir}/${binlog}>>Incr_back$DATE.sql
- ;;
- "${stop_binlog}")
- ${mysql_bin}/mysqlbinlog--stop-position=${stop_pos}${back_src_dir}/${binlog}>>Incr_back$DATE.sql
- ;;
- *)
- ${mysql_bin}/mysqlbinlog${back_src_dir}/${binlog}>>Incr_back$DATE.sql
- ;;
- esac
- done
- fi
- #解除表锁定,并保存目前的binlog和position信息到position文件。
- ${mysql_bin}/mysql-u${user}-p${pass}--socket=${socket}-e"unlocktables"
- cpnow_positionposition
- }
- full_recov()
- {
- cd${back_dir}
- recov_file1=`ls|grep'Full_back'`
- ${mysql_bin}/mysql-u${user}-p${pass}--socket=${socket}-e"use${bak_db};source${back_dir}/${recov_file1};"
- }
- incre_recov()
- {
- cd${back_dir}
- recov_file2=`ls|grep'Incr_back'`
- ${mysql_bin}/mysql-u${user}-p${pass}--socket=${socket}-e"use${bak_db};source${back_dir}/${recov_file2};"
- }
- whiletrue
- do
- echo-e"tt**************************************"
- echo
- echo-e"tttWelcometobackupprogram!"
- echo
- echo-e"ttt(1)FullBackupForMySQL"
- echo-e"ttt(2)IncrementBackupForMySQL"
- echo-e"ttt(3)RecoverFromTheFullBackupFile"
- echo-e"ttt(4)RecoverFromTheIncrementBackupFile"
- echo-e"ttt(5)ExitTheProgram!"
- echo
- echo-e"tt**************************************"
- read-p"Enteryourchoice:"choice
- case$choicein
- )
- echo"now!let'sbackupthedatabyfullmethod......."
- full_bak
- echo"succeed!"
- sleep2
- ;;
- )
- echo"now!let'sbackupthedatabyincrementmethod......"
- incre_bak
- echo"succeed"
- sleep2
- ;;
- )
- echo"now!let'srecoverfromthefullbackfile"
- full_recov
- echo"successful"
- sleep2
- ;;
- )
- echo"now!let'srecoverfromtheincrementbackupfile"
- incre_recov//phpfensi.com
- echo"successful"
- sleep2
- ;;
- )
- break
- ;;
- *)
- echo"WrongOption!tryagain!"
- sleep2
- continue
- ;;
- esac
- done
热门评论