Backup.php
7.5 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace addons\database\library;
use Exception;
use PDO;
use ZipArchive;
class Backup
{
private $host = '';
private $user = '';
private $name = '';
private $pass = '';
private $port = '';
private $tables = ['*'];
private $ignoreTables = [];
private $db;
private $ds = "\n";
public function __construct($host = null, $user = null, $name = null, $pass = null, $port = 3306)
{
if ($host !== null) {
$this->host = $host;
$this->name = $name;
$this->port = $port;
$this->pass = $pass;
$this->user = $user;
}
$this->db = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->name . '; port=' . $port, $this->user, $this->pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$this->db->exec('SET NAMES "utf8"');
}
/**
* 设置备份表
* @param $table
* @return $this
*/
public function setTable($table)
{
if ($table) {
$this->tables = is_array($table) ? $table : explode(',', $table);
}
return $this;
}
/**
* 设置忽略备份的表
* @param $table
* @return $this
*/
public function setIgnoreTable($table)
{
if ($table) {
$this->ignoreTables = is_array($table) ? $table : explode(',', preg_replace('/\s+/', '', $table));
}
return $this;
}
public function backup($backUpdir = 'download/')
{
$sql = $this->_init();
$zip = new ZipArchive();
$date = date('YmdHis');
if (!is_dir($backUpdir)) {
@mkdir($backUpdir, 0755);
}
$name = "backup-{$this->name}-{$date}";
$filename = $backUpdir . $name . ".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE) !== true) {
throw new Exception("Could not open <$filename>\n");
}
$zip->addFromString($name . ".sql", $sql);
$zip->close();
}
private function _init()
{
# COUNT
$ct = 0;
# CONTENT
$sqldump = '';
# COPYRIGHT & OPTIONS
$sqldump .= "-- SQL Dump by Erik Edgren\n";
$sqldump .= "-- version 1.0\n";
$sqldump .= "--\n";
$sqldump .= "-- SQL Dump created: " . date('F jS, Y \@ g:i a') . "\n\n";
$sqldump .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";";
$sqldump .= "\n\n\n\n-- --------------------------------------------------------\n\n\n\n";
$tables = $this->db->query("SHOW FULL TABLES WHERE Table_Type != 'VIEW'");
# LOOP: Get the tables
foreach ($tables AS $table) {
// 忽略表
if (in_array($table[0], $this->ignoreTables)) {
continue;
}
# COUNT
$ct++;
/** ** ** ** ** **/
# DATABASE: Count the rows in each tables
$count_rows = $this->db->prepare("SELECT * FROM " . $table[0]);
$count_rows->execute();
$c_rows = $count_rows->columnCount();
# DATABASE: Count the columns in each tables
$count_columns = $this->db->prepare("SELECT COUNT(*) FROM " . $table[0]);
$count_columns->execute();
$c_columns = $count_columns->fetchColumn();
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
# MYSQL DUMP: Remove tables if they exists
$sqldump .= "--\n";
$sqldump .= "-- Remove the table if it exists\n";
$sqldump .= "--\n\n";
$sqldump .= "DROP TABLE IF EXISTS `" . $table[0] . "`;\n\n\n";
/** ** ** ** ** **/
# MYSQL DUMP: Create table if they do not exists
$sqldump .= "--\n";
$sqldump .= "-- Create the table if it not exists\n";
$sqldump .= "--\n\n";
# LOOP: Get the fields for the table
foreach ($this->db->query("SHOW CREATE TABLE " . $table[0]) AS $field) {
$sqldump .= str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $field['Create Table']);
}
# MYSQL DUMP: New rows
$sqldump .= ";\n\n\n";
/** ** ** ** ** **/
# CHECK: There are one or more columns
if ($c_columns != 0) {
# MYSQL DUMP: List the data for each table
$sqldump .= "--\n";
$sqldump .= "-- List the data for the table\n";
$sqldump .= "--\n\n";
# MYSQL DUMP: Insert into each table
$sqldump .= "INSERT INTO `" . $table[0] . "` (";
# ARRAY
$rows = Array();
# LOOP: Get the tables
foreach ($this->db->query("DESCRIBE " . $table[0]) AS $row) {
$rows[] = "`" . $row[0] . "`";
}
$sqldump .= implode(', ', $rows);
$sqldump .= ") VALUES\n";
# COUNT
$c = 0;
# LOOP: Get the tables
foreach ($this->db->query("SELECT * FROM " . $table[0]) AS $data) {
# COUNT
$c++;
/** ** ** ** ** **/
$sqldump .= "(";
# ARRAY
$cdata = Array();
# LOOP
for ($i = 0; $i < $c_rows; $i++) {
if (is_null($data[$i])) {
$cdata[] = "null";
} else {
$new_lines = preg_replace('/\s\s+/', '\r\n\r\n', addslashes($data[$i]));
$cdata[] = "'" . $new_lines . "'";
}
}
$sqldump .= implode(', ', $cdata);
$sqldump .= ")";
$sqldump .= ($c % 600 != 0 ? ($c_columns != $c ? ',' : ';') : '');
# CHECK
if ($c % 600 == 0) {
$sqldump .= ";\n\n";
} else {
$sqldump .= "\n";
}
# CHECK
if ($c % 600 == 0) {
$sqldump .= "INSERT INTO " . $table[0] . "(";
# ARRAY
$rows = Array();
# LOOP: Get the tables
foreach ($this->db->query("DESCRIBE " . $table[0]) AS $row) {
$rows[] = "`" . $row[0] . "`";
}
$sqldump .= implode(', ', $rows);
$sqldump .= ") VALUES\n";
}
}
}
}
$sqldump .= "\n\n\n";
// Backup views
$tables = $this->db->query("SHOW FULL TABLES WHERE Table_Type = 'VIEW'");
# LOOP: Get the tables
foreach ($tables AS $table) {
// 忽略表
if (in_array($table[0], $this->ignoreTables)) {
continue;
}
foreach ($this->db->query("SHOW CREATE VIEW " . $table[0]) AS $field) {
$sqldump .= "--\n";
$sqldump .= "-- Remove the view if it exists\n";
$sqldump .= "--\n\n";
$sqldump .= "DROP VIEW IF EXISTS `{$field[0]}`;\n\n";
$sqldump .= "--\n";
$sqldump .= "-- Create the view if it not exists\n";
$sqldump .= "--\n\n";
$sqldump .= "{$field[1]};\n\n";
}
}
return $sqldump;
}
}