forked from daniloaz/myphp-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myphp-backup.php
430 lines (356 loc) · 13.5 KB
/
myphp-backup.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* This file contains the Backup_Database class wich performs
* a partial or complete backup of any given MySQL database
* @author Daniel López Azaña <[email protected]>
* @version 1.0
*/
/**
* Define database parameters here
*/
define("DB_USER", 'your_username');
define("DB_PASSWORD", 'your_password');
define("DB_NAME", 'your_db_name');
define("DB_HOST", 'localhost');
define("BACKUP_DIR", 'myphp-backup-files'); // Comment this line to use same script's directory ('.')
define("TABLES", '*'); // Full backup
//define("TABLES", 'table1, table2, table3'); // Partial backup
define("CHARSET", 'utf8');
define("GZIP_BACKUP_FILE", true); // Set to false if you want plain SQL backup files (not gzipped)
define("DISABLE_FOREIGN_KEY_CHECKS", true); // Set to true if you are having foreign key constraint fails
define("BATCH_SIZE", 1000); // Batch size when selecting rows from database in order to not exhaust system memory
// Also number of rows per INSERT statement in backup file
/**
* The Backup_Database class
*/
class Backup_Database {
/**
* Host where the database is located
*/
var $host;
/**
* Username used to connect to database
*/
var $username;
/**
* Password used to connect to database
*/
var $passwd;
/**
* Database to backup
*/
var $dbName;
/**
* Database charset
*/
var $charset;
/**
* Database connection
*/
var $conn;
/**
* Backup directory where backup files are stored
*/
var $backupDir;
/**
* Output backup file
*/
var $backupFile;
/**
* Use gzip compression on backup file
*/
var $gzipBackupFile;
/**
* Content of standard output
*/
var $output;
/**
* Disable foreign key checks
*/
var $disableForeignKeyChecks;
/**
* Batch size, number of rows to process per iteration
*/
var $batchSize;
/**
* Constructor initializes database
*/
public function __construct($host, $username, $passwd, $dbName, $charset = 'utf8') {
$this->host = $host;
$this->username = $username;
$this->passwd = $passwd;
$this->dbName = $dbName;
$this->charset = $charset;
$this->conn = $this->initializeDatabase();
$this->backupDir = BACKUP_DIR ? BACKUP_DIR : '.';
$this->backupFile = 'myphp-backup-'.$this->dbName.'-'.date("Ymd_His", time()).'.sql';
$this->gzipBackupFile = defined('GZIP_BACKUP_FILE') ? GZIP_BACKUP_FILE : true;
$this->disableForeignKeyChecks = defined('DISABLE_FOREIGN_KEY_CHECKS') ? DISABLE_FOREIGN_KEY_CHECKS : true;
$this->batchSize = defined('BATCH_SIZE') ? BATCH_SIZE : 1000; // default 1000 rows
$this->output = '';
}
protected function initializeDatabase() {
try {
$conn = mysqli_connect($this->host, $this->username, $this->passwd, $this->dbName);
if (mysqli_connect_errno()) {
throw new Exception('ERROR connecting database: ' . mysqli_connect_error());
die();
}
if (!mysqli_set_charset($conn, $this->charset)) {
mysqli_query($conn, 'SET NAMES '.$this->charset);
}
} catch (Exception $e) {
print_r($e->getMessage());
die();
}
return $conn;
}
/**
* Backup the whole database or just some tables
* Use '*' for whole database or 'table1 table2 table3...'
* @param string $tables
*/
public function backupTables($tables = '*') {
try {
/**
* Tables to export
*/
if($tables == '*') {
$tables = array();
$result = mysqli_query($this->conn, 'SHOW TABLES');
while($row = mysqli_fetch_row($result)) {
$tables[] = $row[0];
}
} else {
$tables = is_array($tables) ? $tables : explode(',', str_replace(' ', '', $tables));
}
$sql = 'CREATE DATABASE IF NOT EXISTS `'.$this->dbName."`;\n\n";
$sql .= 'USE `'.$this->dbName."`;\n\n";
/**
* Disable foreign key checks
*/
if ($this->disableForeignKeyChecks === true) {
$sql .= "SET foreign_key_checks = 0;\n\n";
}
/**
* Iterate tables
*/
foreach($tables as $table) {
$this->obfPrint("Backing up `".$table."` table...".str_repeat('.', 50-strlen($table)), 0, 0);
/**
* CREATE TABLE
*/
$sql .= 'DROP TABLE IF EXISTS `'.$table.'`;';
$row = mysqli_fetch_row(mysqli_query($this->conn, 'SHOW CREATE TABLE `'.$table.'`'));
$sql .= "\n\n".$row[1].";\n\n";
/**
* INSERT INTO
*/
$row = mysqli_fetch_row(mysqli_query($this->conn, 'SELECT COUNT(*) FROM `'.$table.'`'));
$numRows = $row[0];
// Split table in batches in order to not exhaust system memory
$numBatches = intval($numRows / $this->batchSize) + 1; // Number of while-loop calls to perform
for ($b = 1; $b <= $numBatches; $b++) {
$query = 'SELECT * FROM `' . $table . '` LIMIT ' . ($b * $this->batchSize - $this->batchSize) . ',' . $this->batchSize;
$result = mysqli_query($this->conn, $query);
$realBatchSize = mysqli_num_rows ($result); // Last batch size can be different from $this->batchSize
$numFields = mysqli_num_fields($result);
if ($realBatchSize !== 0) {
$sql .= 'INSERT INTO `'.$table.'` VALUES ';
for ($i = 0; $i < $numFields; $i++) {
$rowCount = 1;
while($row = mysqli_fetch_row($result)) {
$sql.='(';
for($j=0; $j<$numFields; $j++) {
if (isset($row[$j])) {
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
$row[$j] = str_replace("\r","\\r",$row[$j]);
$row[$j] = str_replace("\f","\\f",$row[$j]);
$row[$j] = str_replace("\t","\\t",$row[$j]);
$row[$j] = str_replace("\v","\\v",$row[$j]);
$row[$j] = str_replace("\a","\\a",$row[$j]);
$row[$j] = str_replace("\b","\\b",$row[$j]);
if ($row[$j] == 'true' or $row[$j] == 'false' or preg_match('/^-?[0-9]+$/', $row[$j]) or $row[$j] == 'NULL' or $row[$j] == 'null') {
$sql .= $row[$j];
} else {
$sql .= '"'.$row[$j].'"' ;
}
} else {
$sql.= 'NULL';
}
if ($j < ($numFields-1)) {
$sql .= ',';
}
}
if ($rowCount == $realBatchSize) {
$rowCount = 0;
$sql.= ");\n"; //close the insert statement
} else {
$sql.= "),\n"; //close the row
}
$rowCount++;
}
}
$this->saveFile($sql);
$sql = '';
}
}
/**
* CREATE TRIGGER
*/
// Check if there are some TRIGGERS associated to the table
/*$query = "SHOW TRIGGERS LIKE '" . $table . "%'";
$result = mysqli_query ($this->conn, $query);
if ($result) {
$triggers = array();
while ($trigger = mysqli_fetch_row ($result)) {
$triggers[] = $trigger[0];
}
// Iterate through triggers of the table
foreach ( $triggers as $trigger ) {
$query= 'SHOW CREATE TRIGGER `' . $trigger . '`';
$result = mysqli_fetch_array (mysqli_query ($this->conn, $query));
$sql.= "\nDROP TRIGGER IF EXISTS `" . $trigger . "`;\n";
$sql.= "DELIMITER $$\n" . $result[2] . "$$\n\nDELIMITER ;\n";
}
$sql.= "\n";
$this->saveFile($sql);
$sql = '';
}*/
$sql.="\n\n";
$this->obfPrint('OK');
}
/**
* Re-enable foreign key checks
*/
if ($this->disableForeignKeyChecks === true) {
$sql .= "SET foreign_key_checks = 1;\n";
}
$this->saveFile($sql);
if ($this->gzipBackupFile) {
$this->gzipBackupFile();
} else {
$this->obfPrint('Backup file succesfully saved to ' . $this->backupDir.'/'.$this->backupFile, 1, 1);
}
} catch (Exception $e) {
print_r($e->getMessage());
return false;
}
return true;
}
/**
* Save SQL to file
* @param string $sql
*/
protected function saveFile(&$sql) {
if (!$sql) return false;
try {
if (!file_exists($this->backupDir)) {
mkdir($this->backupDir, 0777, true);
}
file_put_contents($this->backupDir.'/'.$this->backupFile, $sql, FILE_APPEND | LOCK_EX);
} catch (Exception $e) {
print_r($e->getMessage());
return false;
}
return true;
}
/*
* Gzip backup file
*
* @param integer $level GZIP compression level (default: 9)
* @return string New filename (with .gz appended) if success, or false if operation fails
*/
protected function gzipBackupFile($level = 9) {
if (!$this->gzipBackupFile) {
return true;
}
$source = $this->backupDir . '/' . $this->backupFile;
$dest = $source . '.gz';
$this->obfPrint('Gzipping backup file to ' . $dest . '... ', 1, 0);
$mode = 'wb' . $level;
if ($fpOut = gzopen($dest, $mode)) {
if ($fpIn = fopen($source,'rb')) {
while (!feof($fpIn)) {
gzwrite($fpOut, fread($fpIn, 1024 * 256));
}
fclose($fpIn);
} else {
return false;
}
gzclose($fpOut);
if(!unlink($source)) {
return false;
}
} else {
return false;
}
$this->obfPrint('OK');
return $dest;
}
/**
* Prints message forcing output buffer flush
*
*/
public function obfPrint ($msg = '', $lineBreaksBefore = 0, $lineBreaksAfter = 1) {
if (!$msg) {
return false;
}
if ($msg != 'OK' and $msg != 'KO') {
$msg = date("Y-m-d H:i:s") . ' - ' . $msg;
}
$output = '';
if (php_sapi_name() != "cli") {
$lineBreak = "<br />";
} else {
$lineBreak = "\n";
}
if ($lineBreaksBefore > 0) {
for ($i = 1; $i <= $lineBreaksBefore; $i++) {
$output .= $lineBreak;
}
}
$output .= $msg;
if ($lineBreaksAfter > 0) {
for ($i = 1; $i <= $lineBreaksAfter; $i++) {
$output .= $lineBreak;
}
}
// Save output for later use
$this->output .= str_replace('<br />', '\n', $output);
echo $output;
if (php_sapi_name() != "cli") {
if( ob_get_level() > 0 ) {
ob_flush();
}
}
$this->output .= " ";
flush();
}
/**
* Returns full execution output
*
*/
public function getOutput() {
return $this->output;
}
}
/**
* Instantiate Backup_Database and perform backup
*/
// Report all errors
error_reporting(E_ALL);
// Set script max execution time
set_time_limit(900); // 15 minutes
if (php_sapi_name() != "cli") {
echo '<div style="font-family: monospace;">';
}
$backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, CHARSET);
$result = $backupDatabase->backupTables(TABLES, BACKUP_DIR) ? 'OK' : 'KO';
$backupDatabase->obfPrint('Backup result: ' . $result, 1);
// Use $output variable for further processing, for example to send it by email
$output = $backupDatabase->getOutput();
if (php_sapi_name() != "cli") {
echo '</div>';
}