# 獲取該sql中的所有列
col_list = sorted(list(set(re.findall(‘@\d+’, rollback_sql))))
# 因為第一個列前面沒有逗號或者and,所以單獨替換
rollback_sql = rollback_sql.replace(‘@1=’, result_dict[table_name][0][1] + ‘=’)
for col in col_list[1:]:
i = int(col[1:]) - 1
rollback_sql = rollback_sql.replace(col+‘=’, ‘,’ + result_dict[table_name][i][1]+‘=’,1)
rollback_sql = re.sub(‘\n$’,‘;\n’,rollback_sql)
#print rollback_sql
fileOutput.write(rollback_sql)
except IndexError,e:
print “Error:%s” % str(e)
sys.exit()
print “done!”
def usage():
help_info=“”“==========================================================================================
Command line options :
--help # OUT : print help info
-f, --binlog # IN : binlog file. (required)
-o, --outfile # OUT : output rollback sql file. (default ‘rollback.sql’)
-h, --host # IN : host. (default ‘127.0.0.1’)
-u, --user # IN : user. (required)
-p, --password # IN : password. (required)
-P, --port # IN : port. (default 3306)
--start-datetime # IN : start datetime. (default ‘1970-01-01 00:00:00’)
--stop-datetime # IN : stop datetime. default ‘2070-01-01 00:00:00’
--start-position # IN : start position. (default ‘4’)
--stop-position # IN : stop position. (default ‘18446744073709551615’)
-d, --database # IN : List entries for just this database (No default value)。
--only-primary # IN : Only list primary key in where condition (default 0)
Sample :
shell》 python binlog_rollback.py -f ‘mysql-bin.000001’ -o ‘/tmp/rollback.sql’ -h 192.168.0.1 -u ‘user’ -p ‘pwd’ -P 3307 -d dbname
==========================================================================================”“”
print help_info
sys.exit()
if __name__ == ‘__main__’:
getopts_parse_binlog()
init_col_name()
gen_rollback_sql()
演示
#首先創(chuàng)建一個只讀賬號
root:test》 grant select on *.* to ‘query’@‘127.0.0.1’ identified by ‘123456’;
Query OK, 0 rows affected, 1 warning (0.01 sec)
#測試表結(jié)構(gòu)如下
root:test》 CREATE TABLE `table1` (
-》 `id` int(11) NOT NULL AUTO_INCREMENT,
-》 `c1` int(11) DEFAULT NULL,
-》 `c2` varchar(20) DEFAULT NULL,
-》 `c3` int(11) DEFAULT NULL,
-》 PRIMARY KEY (`id`)
-》 );
Query OK, 0 rows affected (0.09 sec)
#插入三條數(shù)據(jù)
root:test》 insert into table1(c1,c2,c3) values (1,‘a(chǎn)’,1),(2,‘b’,2),(3,‘c’,3);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
root:test》 select * from table1;
+----+------+------+------+
| id | c1 | c2 | c3 |
+----+------+------+------+
| 1 | 1 | a | 1 |
| 2 | 2 | b | 2 |
| 3 | 3 | c | 3 |
+----+------+------+------+
3 rows in set (0.00 sec)
#更新一條數(shù)據(jù)
root:test》 update table1 set c3=10 where id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
root:test》 select * from table1;
+----+------+------+------+
| id | c1 | c2 | c3 |
+----+------+------+------+
| 1 | 1 | a | 1 |
| 2 | 2 | b | 2 |
| 3 | 3 | c | 10 |
+----+------+------+------+
3 rows in set (0.00 sec)
#刪除一條數(shù)據(jù)
root:test》 delete from table1 where id=1;
Query OK, 1 row affected (0.01 sec)
root:test》 select * from table1;
+----+------+------+------+
| id | c1 | c2 | c3 |
+----+------+------+------+
| 2 | 2 | b | 2 |
| 3 | 3 | c | 10 |
+----+------+------+------+
2 rows in set (0.00 sec)
評論
查看更多