您好,登錄后才能下訂單哦!
簡介:
數據庫運行過程中難免會發生誤操作,特別是在測試環境 開發人員或測試人員有時會誤刪或者更新錯誤某些數據。這時可以用binlog閃回DML操作。
條件:
# 腳本 del_time_recovery.sh(根據起止 time恢復)用于回滾delete操作:
#!/bin/bash
# File Name???: del_time_recovery.sh
# Author??????: wang
# Description : delete recover according to starttime and endtime.
Usage() {
cat << EOF
mysql_delete_recovery
OPTIONS:
???-b??????binlog name
???-s??????starttime
???-e??????endtime
???-d??????database name
???-t??????table name
For secrity: This scripts check the full need arguments
EOF
}
while getopts ":b:s:e:d:t:" opt; do
??case $opt in
????b)
??????logname=${OPTARG}
??????;;
????s)
??????starttime=${OPTARG}
??????;;
????e)
??????endtime=${OPTARG}
??????;;
????d)
??????db=${OPTARG}
??????;;
????t)
??????table=${OPTARG}
??????;;
????\?)
??????echo "Invalid option: -$OPTARG" >&2
??????exit 1
??????;;
????:)
??????echo "Option -$OPTARG requires an argument." >&2
??????Usage
??????exit 1
??????;;
??esac
done
if [ $# != 10 ] ; then
????Usage
????exit 1;
fi
PATH=$[PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/usr/local/mysql/bin](http://path/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin:/usr/local/mysql/bin)
export PATH
user=root
pwd='xxxxxxxx'
tmpfile=/tmp/del_recovery_$table.sql
mysqlbinlog --no-defaults -vv --base64-output=DECODE-ROWS --start-datetime="$starttime" --stop-datetime="$endtime" $logname |sed -n '/### DELETE FROM `'${db}'`.`'${table}'`/,/COMMIT/p' | \
sed -n '/###/p'????| \
sed 's/### //g;s/\/\*.*/,/g;s/DELETE FROM/INSERT INTO/g;s/WHERE/SELECT/g;'???> $tmpfile
n=0;
for i in `mysql -u$user -p$pwd?--skip-column-names --silent -e "desc $db.$table" |awk '$0=$1'`;
do
????????((n++));
done
sed -i -r "s/(@$n.*),/\1;/g" $tmpfile
sed -i 's/@[1-9].*=//g' $tmpfile
sed -i 's/@[1-9][0-9]=//g' $tmpfile
# 用法:-b -s ?-e -d -t 分別帶別binlog名字 開始的time 結束的time 庫名 表名,
# 直接使用 ?sh del_time_recovery.sh -b /mysqllog/mysql-bin.000005 -s "2017-11-02 19:10:00" -e "2017-11-02 19:20:00" -d test_db -t test_tb 即可調用
# 腳本 update_time_recovery.sh(根據起止 time恢復)用于回滾update操作:
#!/bin/bash
# File Name : update_time_recovery.sh
# Author : wang
# Description : update recover according to starttime and endtime.
Usage() {
cat << EOF
mysql_update_recovery
OPTIONS:
-b binlog name
-s starttime
-e endtime
-d database name
-t table name
For secrity: This scripts check the full need arguments
EOF
}
while getopts ":b:s:e:d:t:" opt; do
case $opt in
b)
logname=${OPTARG}
;;
s)
starttime=${OPTARG}
;;
e)
endtime=${OPTARG}
;;
d)
db=${OPTARG}
;;
t)
table=${OPTARG}
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
Usage
exit 1
;;
esac
done
if [ $# != 10 ] ; then
Usage
exit 1;
fi
user=root
pwd='xxxxxxx'
tmpfile=/tmp/update_recovery_$table.sql
n=0;
for i in `mysql -u$user -p$pwd --skip-column-names --silent -e "desc $db.$table" |awk '$0=$1'`;
do
((n++));
done
mysqlbinlog --no-defaults -vv --base64-output=DECODE-ROWS --start-datetime="$starttime" --stop-datetime="$endtime" $logname |sed -n '/### UPDATE `'${db}'`.`'${table}'`/,/COMMIT/p' \
| sed '/WHERE/{:a;N;/SET/!ba;s/\([^\n]*\)\n\(.*\)\n\(.*\)/\3\n\2\n\1/}' \
| sed -r '/WHERE/{:a;N;/@'"$n"'/!ba;s/### @2.*//g}' \
| sed 's/### //g;s/\/\*.*/,/g' \
| sed '/WHERE/{:a;N;/@1/!ba;s/,/;/g};s/#.*//g;s/COMMIT,//g' \
| sed '/^$/d' > $tmpfile
n=0;
for i in `mysql -u$user -p$pwd --skip-column-names --silent -e "desc $db.$table" |awk '$0=$1'`;
do
((n++));
sed -i "s/@$n\b/$i/g" $tmpfile
done
sed -i -r "s/($i=.*),/\1/g" $tmpfile
# 用法:-b -s -e -d -t 分別帶別binlog名字 開始的time 結束的time 庫名 表名,
# 直接使用 sh update_time_recovery.sh -b /mysqllog/mysql-bin.000005 -s "2017-11-03 14:30:00" -e "2017-11-03 15:00:00" -d test_db -t test_tb 即可調用
參考:
【MySQL】mysqlbinlog_flashback工具使用
參考:
【MySQL】MyFlash 回滾mysql binlog
網上還有很多類似的開源項目 如:binlog2sql等 都可以參考下。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。