刚进入公司时,领导分配的实验任务,这是我写的第一个比较完整和满意的mysql全备,増备及恢复脚本,欢迎指点!
代码如下!
#!/bin/bash# full && increment backup and recover# 说明:事先要确保存在/data/bak目录,且要保证在执行增量备份时已做过至少一次全量备份,否则找不到position文件。port='3306'back_src_dir=/data/mysql/${port}/logs/binlogback_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.sockfull_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 unlock tables#把当前的binlog和position信息存入position文件cat ${dumpfile} |grep 'master_log_file'|awk -f' '{print $2}' > ${back_dir}/positioncat ${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 flush tables with read lock${mysql_bin}/mysqladmin -u${user} -p${pass} --socket=${socket} flush-logs#获取上次备份完成时的binlog和positioncd ${back_dir}start_binlog=`sed -n '1p' position`start_pos=`sed -n '2p' position`#获取目前的binlog和positionmysql -u${user} -p${pass} --socket=${socket} -e show master status/g | awk '{print $2}'| sed -n '2,3p' > now_positionstop_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备份elsestartline=`awk /${start_binlog}/{print nr} ${back_src_dir}/mysql-bin.index`stopline=`wc -l ${back_src_dir}/mysql-bin.index |awk '{print $1}'`for i in `seq ${startline} ${stopline}`dobinlog=`sed -n $ip ${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;; esacdonefi#解除表锁定,并保存目前的binlog和position信息到position文件。${mysql_bin}/mysql -u${user} -p${pass} --socket=${socket} -e unlock tablescp now_position position}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};}while truedoecho -e /t/t**************************************echoecho -e /t/t/twelcome to backup program!echoecho -e /t/t/t(1) full backup for mysqlecho -e /t/t/t(2) increment backup for mysqlecho -e /t/t/t(3) recover from the full backup fileecho -e /t/t/t(4) recover from the increment backup fileecho -e /t/t/t(5) exit the program!echo echo -e /t/t**************************************read -p enter your choice: choicecase $choice in1)echo now! let's backup the data by full method.......full_bakecho succeed!sleep 2;;2)echo now! let's backup the data by increment method......incre_bakecho succeedsleep 2;;3)echo now! let's recover from the full back filefull_recovecho successfulsleep 2;;4)echo now! let's recover from the increment backup fileincre_recovecho successfulsleep 2;;5)break;;*)echo wrong option! try again!sleep 2continue;;esacdone
bitscn.com
