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 = ""; |
15
|
|
|
/*@var string*/ |
16
|
|
|
protected $uri = ""; |
17
|
|
|
|
18
|
|
|
public function setUri(string $uri) |
19
|
|
|
{ |
20
|
|
|
$this->uri = $uri; |
21
|
|
|
return $this; |
22
|
|
|
} |
23
|
|
|
|
24
|
2 |
|
public function setCollection(string $collection) |
25
|
|
|
{ |
26
|
2 |
|
$this->collection = $collection; |
27
|
2 |
|
return $this; |
28
|
|
|
} |
29
|
2 |
|
public function setAuthenticationDatabase(string $authenticationDatabase) |
30
|
|
|
{ |
31
|
2 |
|
$this->authenticationDatabase = $authenticationDatabase; |
32
|
2 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function dump(string $destinationPath = "") |
36
|
|
|
{ |
37
|
|
|
$destinationPath = !empty($destinationPath) ? '"' . $destinationPath . '"' : '"' . $this->destinationPath . '"'; |
38
|
|
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
39
|
|
|
$this->command = $this->removeExtraSpaces($dumpCommand); |
40
|
|
|
$this->run(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function restore(string $restorePath = "") |
44
|
|
|
{ |
45
|
|
|
$restorePath = !empty($restorePath) ? '"' . $restorePath . '"' : '"' . $this->restorePath . '"'; |
46
|
|
|
$restoreCommand = $this->prepareRestoreCommand($restorePath); |
47
|
|
|
$this->command = $this->removeExtraSpaces($restoreCommand); |
48
|
|
|
$this->run(); |
49
|
|
|
} |
50
|
|
|
|
51
|
16 |
|
public function getDumpCommand($destinationPath = '') |
52
|
|
|
{ |
53
|
16 |
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
54
|
16 |
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
55
|
|
|
|
56
|
16 |
|
return $this->removeExtraSpaces($dumpCommand); |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
public function getRestoreCommand(string $filePath = '') |
60
|
|
|
{ |
61
|
12 |
|
$filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath; |
62
|
12 |
|
$restoreCommand = $this->prepareRestoreCommand($filePath); |
63
|
|
|
|
64
|
12 |
|
return $this->removeExtraSpaces($restoreCommand); |
65
|
|
|
} |
66
|
|
|
|
67
|
16 |
|
protected function prepareDumpCommand(string $destinationPath): string |
68
|
|
|
{ |
69
|
16 |
|
$archive = $this->isCompress ? "--archive --gzip" : ""; |
70
|
|
|
|
71
|
16 |
|
$dumpCommand = sprintf( |
72
|
16 |
|
'%s %s %s %s %s %s %s %s %s', |
73
|
16 |
|
$this->quoteCommand($this->commandBinaryPath . 'mongodump'), |
74
|
8 |
|
$archive, |
75
|
16 |
|
$this->prepareDatabase(), |
76
|
16 |
|
$this->prepareUserName(), |
77
|
16 |
|
$this->preparePassword(), |
78
|
16 |
|
$this->prepareHost(), |
79
|
16 |
|
$this->preparePort(), |
80
|
16 |
|
$this->prepareCollection(), |
81
|
16 |
|
$this->prepareAuthenticateDatabase() |
82
|
|
|
); |
83
|
|
|
|
84
|
16 |
|
if ($this->uri) { |
85
|
|
|
$dumpCommand = sprintf( |
86
|
|
|
'%s %s --uri %s %s', |
87
|
|
|
$this->quoteCommand($this->commandBinaryPath . 'mongodump'), |
88
|
|
|
$archive, |
89
|
|
|
$this->uri, |
90
|
|
|
$this->prepareCollection() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
16 |
|
if ($this->isCompress) { |
95
|
2 |
|
return "{$dumpCommand} > \"{$destinationPath}{$this->compressExtension}\""; |
96
|
|
|
} |
97
|
|
|
|
98
|
14 |
|
return "{$dumpCommand} --out \"{$destinationPath}\""; |
99
|
|
|
} |
100
|
|
|
|
101
|
12 |
|
protected function prepareRestoreCommand(string $filePath): string |
102
|
|
|
{ |
103
|
|
|
|
104
|
12 |
|
$archive = $this->isCompress ? "--gzip --archive" : ""; |
105
|
|
|
|
106
|
12 |
|
$restoreCommand = sprintf("%s %s %s %s %s %s", |
107
|
12 |
|
$this->quoteCommand($this->commandBinaryPath . 'mongorestore'), |
108
|
6 |
|
$archive, |
109
|
12 |
|
$this->prepareHost(), |
110
|
12 |
|
$this->preparePort(), |
111
|
12 |
|
$this->prepareUserName(), |
112
|
12 |
|
$this->prepareAuthenticateDatabase() |
113
|
|
|
); |
114
|
|
|
|
115
|
12 |
|
if ($this->uri) { |
116
|
|
|
$restoreCommand = sprintf( |
117
|
|
|
'%smongorestore %s --uri %s', |
118
|
|
|
$this->commandBinaryPath, |
119
|
|
|
$archive, |
120
|
|
|
$this->uri |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
12 |
|
if ($this->isCompress) { |
125
|
2 |
|
return "{$restoreCommand} < \"{$filePath}\""; |
126
|
|
|
} |
127
|
|
|
|
128
|
10 |
|
return "{$restoreCommand} \"{$filePath}\""; |
129
|
|
|
} |
130
|
|
|
|
131
|
16 |
|
public function preparePassword() |
132
|
|
|
{ |
133
|
16 |
|
return !empty($this->password) ? "--password {$this->password}" : ""; |
134
|
|
|
} |
135
|
|
|
|
136
|
28 |
|
public function prepareAuthenticateDatabase() |
137
|
|
|
{ |
138
|
28 |
|
return !empty($this->authenticationDatabase) ? "--authenticationDatabase {$this->authenticationDatabase}" : ""; |
139
|
|
|
} |
140
|
|
|
|
141
|
16 |
|
public function prepareCollection() |
142
|
|
|
{ |
143
|
16 |
|
return !empty($this->collection) ? "--collection {$this->collection}" : ""; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|