-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlamez57Export.php
More file actions
194 lines (163 loc) · 6.21 KB
/
Flamez57Export.php
File metadata and controls
194 lines (163 loc) · 6.21 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
$type = 'mysql'; //数据库类型
$db_name = $argv[1].''; //数据库名
/* php Flamez57Export.php 库名*/
/*
$host = '127.0.0.1';
$port = '3306';
$username = 'root';
$password = '123456';
$charset = 'utf8';
*/
$host = 'ip';
$port = '3306';
$username = 'root';
$password = 'root';
$charset = 'utf8';
$with_cmt = true;
$is_alter_type = false; //是否返回ALTER TABLE 方式,否则 DROP TABLE , CREATE TABLE
$utf8Type = 'utf8mb4'; //$db_name == 'discovery' ? 'utf8mb4' : 'utf8'; //强制转换目标字符集
$dataType = isset($argv[2]) && $argv[2] == 'array' ? 'array' : 'str';
$withData = true;//explode('|', 'sl_admin|sl_common_setting|sl_cron_task|sl_district|sl_express|sl_member_act_tag|sl_menu|sl_pay_client|sl_pay_method|sl_rbac_node|sl_rbac_node_group|sl_rbac_role|sl_rbac_role_node|sl_rbac_role_user|sl_refund_reason|sl_seller_node'); //bool|array 表名
//这里是要绕过得表
$excludeTbArr = explode(
'|',
'yp_single_page_log'
);
$backupFilePath = dirname(__FILE__);
$dsn = "$type:host=$host;port=$port;dbname=$db_name;charset=$charset";
//sl_admin
try {
//建立持久化的PDO连接
$pdo = new PDO($dsn, $username, $password, [PDO::ATTR_PERSISTENT=>true]);
} catch (Exception $e) {
die('连接数据库失败!'.$e);
}
class Act{
private static $pdo = null;
public function __construct(){
global $pdo;
static::$pdo = &$pdo;
}
public function getTables($database){
$sql = "SHOW TABLES FROM `$database`";
$stmt = static::$pdo->query($sql);
$arr = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$arr[] = $row["Tables_in_$database"];
}
return $arr;
}
public function exportDDL($database, $with_cmt=true, $is_alter_type=false, $utf8Type=false, $withData = false){
global $excludeTbArr;
$tables = $this->getTables($database);
$strArr = [];
$sql = $tmp = '';
$stmt = $row = $lines = null;
foreach ($tables as $key => $v) {
if (in_array($v, $excludeTbArr)) {
continue;
}
$sql = "SHOW CREATE TABLE `$v`";
$stmt = static::$pdo->query($sql);
$tableSql = '';
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$tmp = $row['Create Table'];
//remove index
$tmp = preg_replace('#,[\s]+KEY[\s\S]+[)]\n#', "\n", $tmp);
$tmp = preg_replace('#,[\s]+UNIQUE\sKEY[\s\S]+\n#', "\n", $tmp);
if($with_cmt==false){
$tmp = preg_replace("#\\sCOMMENT\\s'[^']+'#U", '', $tmp);
}
if($utf8Type === 'utf8'){
$tmp = str_replace('CHARSET=utf8mb4', 'CHARSET=utf8', $tmp);
} elseif($utf8Type === 'utf8mb4'){
$tmp = str_replace('CHARSET=utf8 ', 'CHARSET=utf8mb4 ', $tmp);
}
if($is_alter_type){
$tableSql = $tableSql . "-- 表的结构: $v --\n";
$lines = explode("\n", $tmp);
if(!empty($lines)){
$lines = array_slice($lines, 1, -1);
}
foreach ($lines as $lk => $lval) {
if(strpos($lval, ' KEY ')===false){
$tableSql = $tableSql . "ALTER TABLE `$v` " . trim($lval, ', ') .";\n";
}
}
$tableSql = $tableSql . "\n";
}else{
$tableSql = $tableSql . "-- 表的结构: $v --\n";
$tableSql = $tableSql . "DROP TABLE IF EXISTS `$v`;\n";
$tableSql = $tableSql . $tmp .";\n\n";
}
//reset AUTO_INCREMENT
$tableSql = preg_replace("#\\sAUTO_INCREMENT=[0-9]+\\s#U", ' ', $tableSql);
$strArr[$v] = $tableSql;
if ($withData === true || in_array($v, $withData)) {
$strArr[$v . '_insert_sql'] = $this->exportSql($v);
}
$stmt = null;
}
return $strArr;
}
public function exportSql($tableName)
{
$sql = "SELECT * FROM `$tableName`";
$stmt = static::$pdo->query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $this->genInsertBatch($tableName, $rows, true);
}
private function genInsertBatch($tableName, $rows, $byReplace = false)
{
if (empty($rows)) {
return false;
}
$fields = [];
$row = reset($rows);
foreach ($row as $field => $val) {
$fields[] = "`$field`";
}
$insertSql = $byReplace ? 'REPLACE' : 'INSERT';
$sql = "/**/ {$insertSql} INTO " . $tableName . " (" . implode(',', $fields) . ") VALUES ";
foreach ($rows as $k => $row) {
$rowStr = '';
foreach ($row as $field => $val) {
$rowStr .= sprintf("'%s',", addslashes(trim($val)));
}
$sql = $sql . sprintf('(%s),', substr($rowStr, 0, -1));
}
$sql = substr($sql, 0, strlen($sql) - 1) . ';';
return $sql;
}
public function saveToFile($db_name, $with_cmt, $is_alter_type, $utf8Type, $withData, $dataType)
{
global $backupFilePath;
$backupFileName = $db_name;
$backupFile = $backupFilePath . '/' . $backupFileName . '.sql';
$oldData = '';
if (is_file($backupFile)) {
$oldData = file_get_contents($backupFile);
}
$newData = $this->exportDDL($db_name, $with_cmt, $is_alter_type, $utf8Type, $withData);
if ($dataType == 'array') {
$newData = json_encode($newData, 256);
} else {
$newDataStr = '';
foreach ($newData as $item) {
$newDataStr .= $item;
}
$newData = $newDataStr;
}
if (md5($oldData) != md5($newData)) {
//转存旧数据
if (!empty($oldData)) {
$oldBackupFile = $backupFilePath . '/' . $backupFileName . '_' . date('YmdHis') . '.sql';
file_put_contents($oldBackupFile, $oldData);
}
file_put_contents($backupFile, $newData);
}
}
}
$act = new Act();
echo $act->saveToFile($db_name, $with_cmt, $is_alter_type, $utf8Type, $withData, $dataType);