PluginProbe
ManageWP Worker / 4.9.36
ManageWP Worker v4.9.36
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / IncrementalBackup / Database / MysqlDumpDumper.php

MysqlDumpDumper.php in ManageWP Worker 4.9.36, at src/MWP/IncrementalBackup/Database/MysqlDumpDumper.php

136 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <[email protected]>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 class MWP_IncrementalBackup_Database_MysqlDumpDumper implements MWP_IncrementalBackup_Database_DumperInterface
12 {
13
14 /**
15 * @var MWP_IncrementalBackup_Database_Configuration
16 */
17 private $configuration;
18
19 /**
20 * @var MWP_IncrementalBackup_Database_DumpOptions
21 */
22 private $options;
23
24 /**
25 * @param MWP_IncrementalBackup_Database_Configuration $configuration
26 * @param MWP_IncrementalBackup_Database_DumpOptions $options
27 */
28 public function __construct(MWP_IncrementalBackup_Database_Configuration $configuration, MWP_IncrementalBackup_Database_DumpOptions $options)
29 {
30 $this->configuration = $configuration;
31 $this->options = $options;
32 }
33
34 /**
35 * {@inheritdoc}
36 */
37 public function dump($table, $realpath)
38 {
39 if (!mwp_is_shell_available()) {
40 throw new MWP_Worker_Exception(MWP_Worker_Exception::SHELL_NOT_AVAILABLE, 'Shell not available.');
41 }
42
43 $mysqldump = mwp_container()->getExecutableFinder()->find('mysqldump', 'mysqldump');
44 $processBuilder = $this->createProcessBuilder(array($table), $mysqldump);
45 $processBuilder->add('--result-file='.$realpath);
46
47 $process = $processBuilder->getProcess();
48
49 mwp_logger()->info('Database dumping process starting', array(
50 'executable_location' => $mysqldump,
51 'command_line' => $process->getCommandLine(),
52 'table' => $table,
53 'destination' => $realpath,
54 ));
55
56 $process->mustRun();
57
58 mwp_logger()->info('Database dump complete', array(
59 'table' => $table,
60 'destination' => $realpath,
61 'size' => @filesize($realpath),
62 ));
63 }
64
65 /**
66 * {@inheritdoc}
67 */
68 public function createStream(array $tables = array())
69 {
70 if (!mwp_is_shell_available()) {
71 throw new MWP_Worker_Exception(MWP_Worker_Exception::SHELL_NOT_AVAILABLE, 'Shell not available.');
72 }
73
74 $mysqldump = mwp_container()->getExecutableFinder()->find('mysqldump', 'mysqldump');
75 $processBuilder = $this->createProcessBuilder($tables, $mysqldump);
76
77 $process = $processBuilder->getProcess();
78
79 mwp_logger()->info('Database dumping process starting', array(
80 'executable_location' => $mysqldump,
81 'command_line' => $process->getCommandLine(),
82 ));
83
84 return new MWP_Stream_ProcessOutput($process);
85 }
86
87 /**
88 * @param array $tables
89 * @param $mysqldump
90 *
91 * @return Symfony_Process_ProcessBuilder
92 */
93 protected function createProcessBuilder(array $tables, $mysqldump)
94 {
95 $processBuilder = Symfony_Process_ProcessBuilder::create()
96 ->setWorkingDirectory(untrailingslashit(ABSPATH))
97 ->setTimeout(3600)
98 ->setPrefix($mysqldump)
99 // Continue even if we get an SQL error.
100 ->add('--force')
101 // User for login if not current user.
102 ->add('--user='.$this->configuration->getUsername())
103 // Password to use when connecting to server. If password is not given it's solicited on the tty.
104 ->add('--password='.$this->configuration->getPassword())
105 // Add a DROP TABLE before each create.
106 ->add('--add-drop-table');
107
108 if ($this->options->isSkipLockTables()) {
109 // Don't lock all tables for read.
110 $processBuilder->add('--lock-tables=false');
111 }
112
113 if ($this->options->isSkipExtendedInsert()) {
114 $processBuilder->add('--extended-insert=false');
115 }
116
117 $processBuilder->add($this->configuration->getDatabase());
118
119 // Dump only specific tables
120 foreach ($tables as $table) {
121 $processBuilder->add($table);
122 }
123
124 if ($this->configuration->isSocket()) {
125 $processBuilder->add('--socket='.$this->configuration->getSocketPath());
126 } else {
127 $processBuilder->add('--host='.$this->configuration->getHost());
128 if (($port = $this->configuration->getPort()) !== null) {
129 $processBuilder->add('--port='.$port);
130 }
131 }
132
133 return $processBuilder;
134 }
135 }
136