-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupMySQL.php
More file actions
231 lines (193 loc) · 5.96 KB
/
Copy pathBackupMySQL.php
File metadata and controls
231 lines (193 loc) · 5.96 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Sauvegarde MySQL
*
* @package BackupMySQL
* @author Roland AZIAWOR <krolandaziawor@gmail.com> developpeur freelance TOGO-Lomé
*
*/
error_reporting(E_ALL);
ini_set('display_errors', true);
/**
* Sauvegarde MySQL
*/
class BackupMySQL extends mysqli {
/**
* Dossier des fichiers de sauvegardes
* @var string
*/
protected $dossier;
/**
* Nom du fichier
* @var string
*/
protected $nom_fichier;
/**
* Ressource du fichier GZip
* @var ressource
*/
protected $gz_fichier;
/**
* Constructeur
* @param array $options
*/
public function __construct($options = array()) {
$default = array(
'host' => ini_get('mysqli.default_host'),
'username' => ini_get('mysqli.default_user'),
'passwd' => ini_get('mysqli.default_pw'),
'dbname' => '',
'port' => ini_get('mysqli.default_port'),
'socket' => ini_get('mysqli.default_socket'),
// autres options
'dossier' => './',
'nbr_fichiers' => 5,
'nom_fichier' => 'backup'
);
$options = array_merge($default, $options);
extract($options);
// Connexion de la connexion DB
@parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
if($this->connect_error) {
$this->message('Erreur de connexion (' . $this->connect_errno . ') '. $this->connect_error);
return;
}
// Controle du dossier
$this->dossier = $dossier;
if(!is_dir($this->dossier)) {
$this->message('Erreur de dossier "' . htmlspecialchars($this->dossier) . '"');
return;
}
// Controle du fichier
$this->nom_fichier = $nom_fichier . date('Ymd-His') . '.sql.gz';
$this->gz_fichier = @gzopen($this->dossier . $this->nom_fichier, 'w');
if(!$this->gz_fichier) {
$this->message('Erreur de fichier "' . htmlspecialchars($this->nom_fichier) . '"');
return;
}
// Demarrage du traitement
$this->sauvegarder();
$this->purger_fichiers($nbr_fichiers);
}
/**
* Message d'information ( commenter le "echo" pour rendre le script invisible )
* @param string $message HTML
*/
protected function message($message = ' ') {
echo '<p style="padding:0; margin:1px 10px; font-family:sans-serif;">'. $message .'</p>';
}
/**
* Protection des quot SQL
* @param string $string
* @return string
*/
protected function insert_clean($string) {
// Ne pas changer l'ordre du tableau !!!
$s1 = array( "\\" , "'" , "\r", "\n", );
$s2 = array( "\\\\" , "''" , '\r', '\n', );
return str_replace($s1, $s2, $string);
}
/**
* Sauvegarder les tables
*/
protected function sauvegarder() {
$this->message('Sauvegarde...');
$sql = '--' ."\n";
$sql .= '-- '. $this->nom_fichier ."\n";
gzwrite($this->gz_fichier, $sql);
// Liste les tables
$result_tables = $this->query('SHOW TABLE STATUS');
if($result_tables && $result_tables->num_rows) {
while($obj_table = $result_tables->fetch_object()) {
$this->message('- ' . htmlspecialchars($obj_table->{'Name'}));
// DROP ...
$sql = "\n\n";
$sql .= 'DROP TABLE IF EXISTS `'. $obj_table->{'Name'} .'`' .";\n";
// CREATE ...
$result_create = $this->query('SHOW CREATE TABLE `'. $obj_table->{'Name'} .'`');
if($result_create && $result_create->num_rows) {
$obj_create = $result_create->fetch_object();
$sql .= $obj_create->{'Create Table'} .";\n";
$result_create->free_result();
}
// INSERT ...
$result_insert = $this->query('SELECT * FROM `'. $obj_table->{'Name'} .'`');
if($result_insert && $result_insert->num_rows) {
$sql .= "\n";
while($obj_insert = $result_insert->fetch_object()) {
$virgule = false;
$sql .= 'INSERT INTO `'. $obj_table->{'Name'} .'` VALUES (';
foreach($obj_insert as $val) {
$sql .= ($virgule ? ',' : '');
if(is_null($val)) {
$sql .= 'NULL';
} else {
$sql .= '\''. $this->insert_clean($val) . '\'';
}
$virgule = true;
} // for
$sql .= ')' .";\n";
} // while
$result_insert->free_result();
}
gzwrite($this->gz_fichier, $sql);
} // while
$result_tables->free_result();
}
gzclose($this->gz_fichier);
$this->message('<strong style="color:green;">' . htmlspecialchars($this->nom_fichier) . '</strong>');
$this->message('Sauvegarde terminée !');
}
/**
* Purger les anciens fichiers
* @param int $nbr_fichiers_max Nombre maximum de sauvegardes
*/
protected function purger_fichiers($nbr_fichiers_max) {
$this->message();
$this->message('Purge des anciens fichiers...');
$fichiers = array();
// On recupere le nom des fichiers gz
if($dossier = dir($this->dossier)) {
while(false !== ($fichier = $dossier->read())) {
if($fichier != '.' && $fichier != '..') {
if(is_dir($this->dossier . $fichier)) {
// Ceci est un dossier ( et non un fichier )
continue;
} else {
// On ne prend que les fichiers se terminant par ".gz"
if(preg_match('/\.gz$/i', $fichier)) {
$fichiers[] = $fichier;
}
}
}
} // while
$dossier->close();
}
// On supprime les anciens fichiers
$nbr_fichiers_total = count($fichiers);
if($nbr_fichiers_total >= $nbr_fichiers_max) {
// Inverser l'ordre des fichiers gz pour ne pas supprimer les derniers fichiers
rsort($fichiers);
// Suppression...
for($i = $nbr_fichiers_max; $i < $nbr_fichiers_total; $i++) {
$this->message('<strong style="color:red;">' . htmlspecialchars($fichiers[$i]) . '</strong>');
unlink($this->dossier . $fichiers[$i]);
}
}
$this->message('Purge terminée !');
}
}
// Instance de la classe ( a copier autant que necessaire, mais attention au timeout )
// Rq: pour les parametres, reprendre une ou plusieurs cles de $default ( dans la methode __construct() )
new BackupMySQL(array(
'username' => 'root',
'passwd' => 'root',// si vous avez evidemment un mot de passe
'dbname' => 'test'
));
//new BackupMySQL(array(
// 'username' => 'root',
// 'passwd' => 'root',
// 'dbname' => 'mabase',
// 'dossier' => './dossier2/'
// ));
?>