-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMySQLBackuper.php
97 lines (91 loc) · 2.97 KB
/
MySQLBackuper.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
<?php
/*!
Makes backup of MySQL database in format similar to mysqldump.
The code was taken from sypex dumper v 1.
Now backups only structure and data, no triggers and prepared statements are backuped.
PHP script needs enough time and memory (and other resources) to finish.
*/
class MySQLBackuper implements IBackuper{
public $db;
const baseDir="base/mysql",baseName="base.sql";
public $bases=null;
/*!
@param PDO $prefs
@param array $prefs : either array( PDO $base1 ) or array( 'base'=>array( PDO $base1 ),'dumpStructure'=>1,'dumpData'=>1 )
*/
function __construct($prefs){
if($prefs instanceof PDO){
$prefs=array('base'=>$prefs);
}
if(is_array($prefs)){
if(is_array($prefs["base"])){
$this->bases=$prefs["base"];
}
else{
$this->bases=array($prefs["base"]);
}
$this->dumpStructure=isset($prefs["dumpStructure"])?$prefs["dumpStructure"]:1;
$this->dumpData=isset($prefs["dumpStructure"])?$prefs["dumpStructure"]:1;
}else throw new Exception("Input must be either PDO object or array with prefs");
}
function prepareForBackup(PDO &$base){
}
function makeBackup(&$arch){
$result="";
foreach($this->bases as &$base){
$res=static::dumpDB($base);
$result.=$res["structure"]."\n".$res["data"]."\n";
}
$arch->addFromString( static::baseDir.'/'.static::baseName, $result );
return;
}
function needBackup(){
return 1;
}
function dumpDB(&$base){
$res="";
$structure="";
$data="";
$tables=$base->query("show full tables;");
$tables->setFetchMode(PDO::FETCH_NUM);
foreach($tables as $table){
if($this->dumpStructure){
$res=$base->query("show create table `{$table[0]}`;");
$tblStrDump=$res->fetch();
//new dBug($res);
//new dBug($tblStrDump);
$structure.="-- Table ".$tblStrDump[0]." - ".$table[1]."\n".$tblStrDump[1]."\n\n";
}
//////////////////////начинается чужой код
if($this->dumpData){
$data .= "--Dumping data of `{$table[0]}`\nINSERT INTO `{$table[0]}` VALUES \n";
$notNum = array();
$res = $base->query("SHOW COLUMNS FROM `{$table[0]}`");
$fields = 0;
foreach($res as $col) {
// TODO: проверить типы SET, ENUM и BIT
$notNum[$fields] = preg_match("/^(tinyint|smallint|mediumint|bigint|int|float|double|real|decimal|numeric|year)/", $col['Type']) ? 0 : 1;
$fields++;
}
$time_old = time();
$z = 0;
// Достаем данные
//$res = mysql_unbuffered_query("SELECT * FROM `{$table[0]}`{$from}");
$res = $base->query("SELECT * FROM `{$table[0]}`;");
$res->setFetchMode(PDO::FETCH_NUM);
foreach($res as $row) {
for($k = 0; $k < $fields; $k++){
if(!isset($row[$k])) {$row[$k] = '\N';}
elseif($notNum[$k]) {$row[$k] = '\'' . $base->quote($row[$k]) . '\'';}
}
$data .= '(' . implode(',', $row) . "),\n";
}
unset($row);
//mysql_free_result($res);
$data = substr_replace($data, "\t;\n\n", -2, 2);
}
}
return array('structure'=>$structure,'data'=>$data);
}
}
?>