11<?php
22namespace phpbu \App \Backup \Source ;
33
4+ use phpbu \App \Backup \Restore \Plan ;
45use phpbu \App \Backup \Target ;
56use phpbu \App \Cli \Executable ;
67use phpbu \App \Exception ;
1819 * @link http://phpbu.de/
1920 * @since Class available since Release 1.0.0
2021 */
21- class Mysqldump extends SimulatorExecutable implements Simulator
22+ class Mysqldump extends SimulatorExecutable implements Simulator, Restorable
2223{
2324 /**
24- * Path to executable.
25+ * Path to mysql executable.
26+ *
27+ * @var string
28+ */
29+ private $ pathToMysql ;
30+
31+ /**
32+ * Path to mysqldump executable.
2533 *
2634 * @var string
2735 */
2836 private $ pathToMysqldump ;
2937
38+ /**
39+ * Path to mysqlimport executable.
40+ *
41+ * @var string
42+ */
43+ private $ pathToMysqlimport ;
44+
3045 /**
3146 * Host to connect to
3247 * --host <hostname>
@@ -146,7 +161,7 @@ class Mysqldump extends SimulatorExecutable implements Simulator
146161 private $ extendedInsert ;
147162
148163 /**
149- * Dump blob fields as hex.
164+ * Dump blob fields as hex
150165 * --hex-blob
151166 *
152167 * @var boolean
@@ -162,15 +177,15 @@ class Mysqldump extends SimulatorExecutable implements Simulator
162177 private $ noData ;
163178
164179 /**
165- * Add general transaction id statement.
180+ * Add general transaction id statement
166181 * --set-gids-purged=['ON', 'OFF', 'AUTO']
167182 *
168183 * @var string
169184 */
170185 private $ gtidPurged ;
171186
172187 /**
173- * Dump procedures and functions.
188+ * Dump procedures and functions
174189 * --routines
175190 *
176191 * @var bool
@@ -188,7 +203,9 @@ public function setup(array $conf = [])
188203 {
189204 $ this ->setupSourceData ($ conf );
190205
206+ $ this ->pathToMysql = Util \Arr::getValue ($ conf , 'pathToMysql ' , '' );
191207 $ this ->pathToMysqldump = Util \Arr::getValue ($ conf , 'pathToMysqldump ' , '' );
208+ $ this ->pathToMysqlimport = Util \Arr::getValue ($ conf , 'pathToMysqlimport ' , '' );
192209 $ this ->host = Util \Arr::getValue ($ conf , 'host ' , '' );
193210 $ this ->port = Util \Arr::getValue ($ conf , 'port ' , 0 );
194211 $ this ->protocol = Util \Arr::getValue ($ conf , 'protocol ' , '' );
@@ -212,7 +229,7 @@ public function setup(array $conf = [])
212229 }
213230
214231 /**
215- * Get tables and databases to backup.
232+ * Get tables and databases to backup
216233 *
217234 * @param array $conf
218235 */
@@ -225,7 +242,7 @@ protected function setupSourceData(array $conf)
225242 }
226243
227244 /**
228- * Execute the backup.
245+ * Execute the backup
229246 *
230247 * @see \phpbu\App\Backup\Source
231248 * @param \phpbu\App\Backup\Target $target
@@ -253,6 +270,41 @@ public function backup(Target $target, Result $result) : Status
253270 return $ this ->createStatus ($ target );
254271 }
255272
273+ /**
274+ * Restore the backup
275+ *
276+ * @param \phpbu\App\Backup\Target $target
277+ * @param \phpbu\App\Backup\Restore\Plan $plan
278+ * @return \phpbu\App\Backup\Source\Status
279+ */
280+ public function restore (Target $ target , Plan $ plan ): Status
281+ {
282+ $ executable = $ this ->createMysqlExecutable ();
283+
284+ if ($ this ->filePerTable ) {
285+ $ database = $ this ->databases [0 ];
286+ $ sourceTar = $ target ->getPathname (true ) . '.tar ' ;
287+ $ mysqlimport = $ this ->createMysqlimportExecutable ('<table-file> ' , $ database );
288+
289+ $ executable ->useDatabase ($ database );
290+ $ executable ->useSourceFile ('<table-file> ' );
291+
292+ $ mysqlCommand = $ executable ->getCommandPrintable ();
293+ $ importCommand = $ mysqlimport ->getCommandPrintable ();
294+ $ mysqlComment = 'Restore the structure, execute this for every table file ' ;
295+ $ importComment = 'Restore the data, execute this for every table file ' ;
296+
297+ $ plan ->addRestoreCommand ('tar -xvf ' . $ sourceTar , 'Extract the table files ' );
298+ $ plan ->addRestoreCommand ($ mysqlCommand , $ mysqlComment );
299+ $ plan ->addRestoreCommand ($ importCommand , $ importComment );
300+ } else {
301+ $ executable ->useSourceFile ($ target ->getFilename (true ));
302+ $ plan ->addRestoreCommand ($ executable ->getCommandPrintable ());
303+ }
304+
305+ return Status::create ()->uncompressedFile ($ target ->getPathname ());
306+ }
307+
256308 /**
257309 * Create the Executable to run the mysqldump command.
258310 *
@@ -312,7 +364,7 @@ protected function createStatus(Target $target) : Status
312364 }
313365
314366 /**
315- * Cann compression be handled via pipe operator.
367+ * Can compression be handled via pipe operator.
316368 *
317369 * @param \phpbu\App\Backup\Target $target
318370 * @return bool
@@ -332,4 +384,43 @@ private function getDumpTarget(Target $target) : string
332384 {
333385 return $ target ->getPathnamePlain () . ($ this ->filePerTable ? '.dump ' : '' );
334386 }
387+
388+ /**
389+ * Create the Executable to run the mysql command.
390+ *
391+ * @return \phpbu\App\Cli\Executable\Mysql
392+ */
393+ private function createMysqlExecutable (): Executable \Mysql
394+ {
395+ $ executable = new Executable \Mysql ($ this ->pathToMysql );
396+ $ executable ->credentials ($ this ->user , $ this ->password )
397+ ->useHost ($ this ->host )
398+ ->usePort ($ this ->port )
399+ ->useProtocol ($ this ->protocol )
400+ ->useQuickMode ($ this ->quick )
401+ ->useCompression ($ this ->compress );
402+
403+ return $ executable ;
404+ }
405+
406+ /**
407+ * Create the Executable to run the mysqlimport command.
408+ *
409+ * @param string $sourceFilename
410+ * @param string $targetDatabase
411+ *
412+ * @return \phpbu\App\Cli\Executable\Mysqlimport
413+ */
414+ private function createMysqlimportExecutable (string $ sourceFilename , string $ targetDatabase ): Executable \Mysqlimport
415+ {
416+ $ executable = new Executable \Mysqlimport ($ this ->pathToMysqlimport );
417+ $ executable ->setSourceAndTarget ($ sourceFilename , $ targetDatabase )
418+ ->credentials ($ this ->user , $ this ->password )
419+ ->useHost ($ this ->host )
420+ ->usePort ($ this ->port )
421+ ->useProtocol ($ this ->protocol )
422+ ->useCompression ($ this ->compress );
423+
424+ return $ executable ;
425+ }
335426}
0 commit comments