|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\Dumper\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use CodexShaper\Dumper\Dumper; |
|
6
|
|
|
|
|
7
|
|
|
class MongoDumper extends Dumper |
|
8
|
|
|
{ |
|
9
|
|
|
/*@var int*/ |
|
10
|
|
|
protected $port = 27017; |
|
11
|
|
|
/*@var string*/ |
|
12
|
|
|
protected $collection = ""; |
|
13
|
|
|
/*@var string*/ |
|
14
|
|
|
protected $authenticationDatabase = "admin"; |
|
15
|
|
|
/*@var string*/ |
|
16
|
|
|
protected $uri = ""; |
|
17
|
|
|
|
|
18
|
|
|
public function setUri(string $uri) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->uri = $uri; |
|
21
|
|
|
return $this; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function setCollection(string $collection) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->collection = $collection; |
|
27
|
|
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
public function setAuthenticationDatabase(string $authenticationDatabase) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->authenticationDatabase = $authenticationDatabase; |
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function dump(string $destinationPath = "") |
|
36
|
|
|
{ |
|
37
|
|
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
|
38
|
|
|
$this->command = $this->prepareDumpCommand($destinationPath); |
|
39
|
|
|
$this->run(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function restore(string $restorePath = "") |
|
43
|
|
|
{ |
|
44
|
|
|
$restorePath = !empty($restorePath) ? $restorePath : $this->restorePath; |
|
45
|
|
|
$this->command = $this->prepareRestoreCommand($restorePath); |
|
46
|
|
|
$this->run(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function prepareDumpCommand(string $destinationPath): string |
|
50
|
|
|
{ |
|
51
|
|
|
$databaseArg = !empty($this->dbName) ? "--db " . escapeshellarg($this->dbName) : ""; |
|
52
|
|
|
$username = !empty($this->username) ? "--username " . escapeshellarg($this->username) : ""; |
|
53
|
|
|
$password = !empty($this->password) ? "--password " . escapeshellarg($this->password) : ""; |
|
54
|
|
|
$host = !empty($this->host) ? "--host " . escapeshellarg($this->host) : ""; |
|
55
|
|
|
$port = !empty($this->port) ? "--port " . escapeshellarg($this->port) : ""; |
|
56
|
|
|
$collection = !empty($this->collection) ? "--collection " . escapeshellarg($this->collection) : ""; |
|
57
|
|
|
$authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : ""; |
|
58
|
|
|
$archive = ""; |
|
59
|
|
|
|
|
60
|
|
|
if ($this->isCompress) { |
|
61
|
|
|
|
|
62
|
|
|
$archive = "--archive --gzip"; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$dumpCommand = sprintf( |
|
66
|
|
|
'%smongodump %s %s %s %s %s %s %s %s', |
|
67
|
|
|
$this->dumpCommandPath, |
|
68
|
|
|
$archive, |
|
69
|
|
|
$databaseArg, |
|
70
|
|
|
$username, |
|
71
|
|
|
$password, |
|
72
|
|
|
$host, |
|
73
|
|
|
$port, |
|
74
|
|
|
$collection, |
|
75
|
|
|
$authenticationDatabase |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
if ($this->uri) { |
|
79
|
|
|
$dumpCommand = sprintf( |
|
80
|
|
|
'%smongodump %s --uri %s %s', |
|
81
|
|
|
$this->dumpCommandPath, |
|
82
|
|
|
$archive, |
|
83
|
|
|
$this->uri, |
|
84
|
|
|
$collection |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($this->isCompress) { |
|
89
|
|
|
return "{$dumpCommand} > {$destinationPath}{$this->compressExtension}"; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return "{$dumpCommand} --out {$destinationPath}"; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function prepareRestoreCommand(string $filePath): string |
|
96
|
|
|
{ |
|
97
|
|
|
$username = !empty($this->username) ? "--username " . escapeshellarg($this->username) : ""; |
|
98
|
|
|
$host = !empty($this->host) ? "--host " . escapeshellarg($this->host) : ""; |
|
99
|
|
|
$port = !empty($this->port) ? "--port " . escapeshellarg($this->port) : ""; |
|
100
|
|
|
$authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : ""; |
|
101
|
|
|
|
|
102
|
|
|
$archive = ""; |
|
103
|
|
|
|
|
104
|
|
|
if ($this->isCompress) { |
|
105
|
|
|
|
|
106
|
|
|
$archive = "--gzip --archive"; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$restoreCommand = sprintf("%smongorestore %s %s %s %s %s", |
|
110
|
|
|
$this->dumpCommandPath, |
|
111
|
|
|
$archive, |
|
112
|
|
|
$host, |
|
113
|
|
|
$port, |
|
114
|
|
|
$username, |
|
115
|
|
|
$authenticationDatabase |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
if ($this->uri) { |
|
119
|
|
|
$restoreCommand = sprintf( |
|
120
|
|
|
'%smongorestore %s --uri %s', |
|
121
|
|
|
$this->dumpCommandPath, |
|
122
|
|
|
$archive, |
|
123
|
|
|
$this->uri |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if ($this->isCompress) { |
|
128
|
|
|
|
|
129
|
|
|
return "{$restoreCommand} < {$filePath}"; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return "{$restoreCommand} {$filePath}"; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|