-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrollback.php
69 lines (55 loc) · 2.63 KB
/
rollback.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
session_start();
//检测是否登录,若没登录则转向登录界面
if(!isset($_SESSION['userid'])){
header("Location:index.html");
exit("你还没登录呢。");
}
$id = $_GET['id'];
require 'conn.php';
$result = mysqli_query($conn,"SELECT a.ip,a.dbname,a.user,a.pwd,a.port,b.ops_content,b.binlog_information FROM dbinfo a JOIN sql_order_wait b ON a.dbname = b.ops_db WHERE b.id='".$id ."'");
while($row = mysqli_fetch_array($result))
{
$ip=$row[0];
$db=$row[1];
$user=$row[2];
$pwd=$row[3];
$port=$row[4];
$ops_content=$row[5];
$binlog_information=preg_split("/[\s]+/", $row[6]);
}
mysqli_close($conn);
echo "为防止手滑误更改数据,提供反向SQL回滚功能,结果供参考,如有问题请与DBA联系。"."</br></br>";
echo "你刚才上线时的SQL如下:</br>";
echo '<pre style="font-size:14px">' .$ops_content. '</pre>';
echo "<hr style=FILTER: progid:DXImageTransform.Microsoft.Glow(color=#987cb9,strength=10) width=100% color=#987cb9 SIZE=1>";
echo "</br>";
echo "生成反向SQL如下:</br>";
//print_r($binlog_information);
//后端MySQL 5.6+
$rollback_sql="/usr/bin/python /var/www/html/sqlops/binlog2sql/binlog2sql.py --flashback -h${ip} -u${user} -p'${pwd}' -P${port} --start-file='$binlog_information[0]' --stop-file='$binlog_information[3]' --start-position='$binlog_information[1]' --stop-position='$binlog_information[4]'";
//后端MariaDB
//$rollback_sql="cd /root/binlog2sql/binlog2sql;/usr/bin/python binlog2sql.py --flashback -h${ip} -u${user} -p'${pwd}' -P${port} --start-file='$binlog_information[0]' --stop-file='$binlog_information[2]' --start-position='$binlog_information[1]' --stop-position='$binlog_information[3]'";
//echo $rollback_sql;
##########执行回滚###################
$remote_user="root";
$remote_password="123456";
$script=$rollback_sql;
$connection = ssh2_connect('127.0.0.1',22);
ssh2_auth_password($connection,$remote_user,$remote_password);
$stream = ssh2_exec($connection,$script,NULL,$env=array(),10,10);
$correctStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
//stream_set_blocking($errorStream, true);
stream_set_blocking($correctStream, true);
$message=stream_get_contents($errorStream);
$measage_stdio=stream_get_contents($correctStream);
echo $message."<br>";
echo '<pre>' .nl2br($measage_stdio). '</pre>'."<br>";
//echo nl2br(nl2br($measage_stdio)) . "<br>";
echo "<br>";
echo "<a href='my_order.php'>点击返回工单界面</a></br></br>";
fclose($stream);
fclose($errorStream);
#######################################
?>