|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Backup package, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2015 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* This project is fork of "kbond/php-backup", for full credits info, please |
|
11
|
|
|
* view CREDITS file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace RunOpenCode\Backup\Source; |
|
14
|
|
|
|
|
15
|
|
|
use RunOpenCode\Backup\Backup\File; |
|
16
|
|
|
use RunOpenCode\Backup\Contract\EventDispatcherAwareInterface; |
|
17
|
|
|
use RunOpenCode\Backup\Contract\SourceInterface; |
|
18
|
|
|
use RunOpenCode\Backup\Event\BackupEvents; |
|
19
|
|
|
use RunOpenCode\Backup\Event\EventDispatcherAwareTrait; |
|
20
|
|
|
use RunOpenCode\Backup\Exception\SourceException; |
|
21
|
|
|
use RunOpenCode\Backup\Utils\Filename; |
|
22
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class MySqlDumpSource |
|
26
|
|
|
* |
|
27
|
|
|
* Fetch database dump for backup. |
|
28
|
|
|
* |
|
29
|
|
|
* @package RunOpenCode\Backup\Source |
|
30
|
|
|
*/ |
|
31
|
|
|
class MySqlDumpSource implements SourceInterface, EventDispatcherAwareInterface |
|
32
|
|
|
{ |
|
33
|
|
|
use EventDispatcherAwareTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $database; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $user; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $password; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $host; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var int |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $port; |
|
59
|
|
|
|
|
60
|
6 |
|
public function __construct($database, $user, $password = null, $host = null, $port = 3306) |
|
61
|
|
|
{ |
|
62
|
6 |
|
$this->database = $database; |
|
63
|
6 |
|
$this->user = $user; |
|
64
|
6 |
|
$this->password = $password; |
|
65
|
6 |
|
$this->host = $host; |
|
66
|
6 |
|
$this->port = $port; |
|
67
|
6 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
6 |
|
public function fetch() |
|
73
|
|
|
{ |
|
74
|
6 |
|
$process = $this->buildProcess(); |
|
75
|
|
|
|
|
76
|
6 |
|
$process->run(); |
|
77
|
|
|
|
|
78
|
6 |
|
if (!$process->isSuccessful()) { |
|
79
|
4 |
|
throw new SourceException(sprintf('Unable to dump MySql database "%s", reason: "%s".', $this->database, $process->getErrorOutput())); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
$mySqlDump = Filename::temporaryFile(sprintf('mysql-dump-%s-%s-%s.sql', $this->database, (is_null($this->host) ? 'localhost' : $this->host), date('Y-m-d-H-i-s'))); |
|
83
|
|
|
|
|
84
|
2 |
|
if (file_put_contents($mySqlDump, $process->getOutput()) === false) { |
|
85
|
|
|
|
|
86
|
|
|
throw new \RuntimeException(sprintf('Unable to save MySql dump of database into "%s".', $mySqlDump)); |
|
87
|
|
|
|
|
88
|
|
|
} else { |
|
89
|
|
|
|
|
90
|
2 |
|
$this->getEventDispatcher()->addListener(BackupEvents::TERMINATE, function() use ($mySqlDump) { |
|
91
|
2 |
|
unlink($mySqlDump); |
|
92
|
2 |
|
}); |
|
93
|
|
|
|
|
94
|
2 |
|
return array(File::fromLocal($mySqlDump, dirname($mySqlDump))); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Builds mysqldump process. |
|
100
|
|
|
* |
|
101
|
|
|
* @return \Symfony\Component\Process\Process |
|
102
|
|
|
*/ |
|
103
|
6 |
|
protected function buildProcess() |
|
104
|
|
|
{ |
|
105
|
6 |
|
$processBuilder = new ProcessBuilder(); |
|
106
|
|
|
|
|
107
|
|
|
$processBuilder |
|
108
|
6 |
|
->add('mysqldump') |
|
109
|
6 |
|
->add(sprintf('-u%s', $this->user)); |
|
110
|
|
|
|
|
111
|
6 |
|
if (!is_null($this->host)) { |
|
112
|
|
|
|
|
113
|
4 |
|
if (!is_null($this->port)) { |
|
114
|
|
|
$processBuilder->add(sprintf('-h%s:%s', $this->host, $this->port)); |
|
115
|
|
|
} else { |
|
116
|
4 |
|
$processBuilder->add(sprintf('-h%s', $this->host, $this->port)); |
|
117
|
|
|
} |
|
118
|
2 |
|
} |
|
119
|
|
|
|
|
120
|
6 |
|
if (null !== $this->password) { |
|
121
|
|
|
$processBuilder->add(sprintf('-p%s', $this->password)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
6 |
|
$processBuilder->add($this->database); |
|
125
|
|
|
|
|
126
|
6 |
|
return $processBuilder->getProcess(); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|