-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbackup_db.php
331 lines (277 loc) · 7.08 KB
/
backup_db.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
<?php
class Db_Tool_Backup
{
/**
* @var string
*/
protected $_backup_dir;
/**
* @var int
*/
protected $_keep_files;
/**
* @var string
*/
protected $_compression;
/**
* @var string
*/
protected $_db_host;
/**
* @var string
*/
protected $_db_port;
/**
* @var string
*/
protected $_db_protocol;
/**
* @var string
*/
protected $_db_user;
/**
* @var string
*/
protected $_db_passwd;
/**
* @var string
*/
protected $_extra_config;
/**
* @var array
*/
protected $_previous_count = array();
/**
* @var array
*/
protected $_backup_files = array();
/**
* Database names to backup
* Loaded via config
*
* @var array
*/
protected $_db_names = array();
/**
* Initialize by loading the config
*
*/
public function __construct()
{
$config = $this->_get_config();
if (empty($config))
{
throw new Exception('Unable to read config');
}
// Load config
foreach ($config as $key => $val)
{
$prop = '_'.$key;
if (property_exists($this, $prop))
{
$this->{$prop} = $val;
}
}
}
/**
* Returns the config file data
*
* @return array
*/
protected function _get_config()
{
$file = dirname(__FILE__).'/config.php';
return include $file;
}
/**
* Run backup
*
* @throws Exception
*/
public function backup()
{
$now = new DateTime('now', new DateTimeZone('UTC'));
$suffix = $now->format('Y-m-d-H-i-s');
if ( ! empty($this->_db_names))
{
foreach ($this->_db_names as $name)
{
// Back it up
$this->_single_backup($name, $suffix);
}
}
}
/**
* Performs backup for a single database
*
* @param string $db_name
* @param string $suffix
* @return boolean
*/
protected function _single_backup($db_name, $suffix)
{
$filename = $this->_backup_dir.'/'.$db_name.'/'.$db_name.'_'.$suffix.'.sql';
$params = array();
if (!empty($this->_extra_config)) {
// Allows hiding the username/password from the command line
// although the main goal is to just suppress the insure command warning.
// The username and passwords are still readable on the file, regardless.
$params[] = '--defaults-extra-file=' . $this->_extra_config;
} else {
// Defaults to passing username/password into the command line
$params[] = '-u '.$this->_db_user;
$params[] = '-p'.$this->_db_passwd;
}
$params[] = '-h '.$this->_db_host;
$params[] = '--port '.$this->_db_port;
if ($this->_db_protocol) {
$params[] = '--protocol '.$this->_db_protocol;
}
$params[] = '--column-statistics=0';
$command = "mysqldump " . implode(' ', $params) . ' ' . $db_name;
if ($this->_compression) {
$command .= " | {$this->_compression} > $filename.bz2";
} else {
$command = "mysqldump " . implode(' ', $params) . " $db_name > $filename";
}
return system($command);
}
/**
* Deletes backup files that should not be kept
*
* @return boolean
*/
public function cleanup()
{
foreach ($this->_db_names as $name)
{
$this->_single_cleanup($name);
}
}
/**
* Cleans up backup for a single db
*
* @param string $db_name
* @return boolean
*/
protected function _single_cleanup($db_name)
{
$this->_get_backup_files($db_name);
$delete_files = $this->_get_files_to_delete($db_name);
if ( ! empty($delete_files))
{
foreach ($delete_files as $key => $file)
{
$filename = $this->_backup_dir.'/'.$db_name.'/'.$file;
$this->_delete($filename);
}
return TRUE;
}
return FALSE;
}
/**
* Returns all files to be deleted
*
* @param string $db_name
* @return array $files | false
*/
protected function _get_files_to_delete($db_name)
{
// Only return files that should not be kept
if ( ! empty($this->_backup_files[$db_name]))
{
$ret = array();
$count = count($this->_backup_files[$db_name]);
if ($count > $this->_keep_files)
{
for ($c = $this->_keep_files; $c < $count; $c++)
{
if (
isset($this->_backup_files[$db_name][$c]) &&
$this->_backup_files[$db_name][$c] != '.' &&
$this->_backup_files[$db_name][$c] != '..'
)
{
$ret[] = $this->_backup_files[$db_name][$c];
}
}
}
return $ret;
}
return FALSE;
}
/**
* Returns all backup files in descending order
*
* @param string $db_name
* @return array $files | boolean false
*/
protected function _get_backup_files($db_name)
{
$dir = $this->_backup_dir.'/'.$db_name;
$files = scandir($dir, 1);
if ( ! empty($files))
{
$this->_backup_files[$db_name] = $files;
return $this->_backup_files[$db_name];
}
return FALSe;
}
/**
* Deletes the backup file
*
* @param string $filename
* @return boolean
*/
protected function _delete($filename)
{
if (is_file($filename))
{
return unlink($filename);
}
return FALSE;
}
/**
* Writes the counter back to the log file
*
* @param string $db_name
* @param int $count
* @return boolean
*/
protected function _set_count($db_name, $count)
{
$filename = dirname(__FILE__).'/'.$db_name.'_count';
$file = fopen($filename, 'wb');
if ($file)
{
return fwrite($file, (string) $count);
}
return FALSE;
}
/**
* Retrieves the backup count from the text file
*
* @param string $db_name
* @return int
*/
protected function _get_count($db_name)
{
$filename = dirname(__FILE__).'/'.$db_name.'_count';
$file = fopen($filename, 'rb');
if ($file)
{
$count = fgets($file, 4096);
if ( ! is_numeric($count))
{
$count = FALSE;
}
fclose($file);
$this->_previous_count[$db_name] = $count;
return $this->_previous_count[$db_name];
}
}
}
// Perform backup
$backup_manager = new Db_Tool_Backup;
$backup_manager->backup();
$backup_manager->cleanup();