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
|
|
|
$archive = $this->isCompress ? "--archive --gzip" : ""; |
52
|
|
|
|
53
|
|
|
$dumpCommand = sprintf( |
54
|
|
|
'%smongodump %s %s %s %s %s %s %s %s', |
55
|
|
|
$this->dumpCommandPath, |
56
|
|
|
$archive, |
57
|
|
|
$this->getDatabaseOption(), |
58
|
|
|
$this->getUsernameOption(), |
59
|
|
|
$this->getPasswordOption(), |
60
|
|
|
$this->getHostOption(), |
61
|
|
|
$this->getPortOption(), |
62
|
|
|
$this->getCollectionOption(), |
63
|
|
|
$this->getAuthenticateDatabase(), |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if ($this->uri) { |
67
|
|
|
$dumpCommand = sprintf( |
68
|
|
|
'%smongodump %s --uri %s %s', |
69
|
|
|
$this->dumpCommandPath, |
70
|
|
|
$archive, |
71
|
|
|
$this->uri, |
72
|
|
|
$this->getCollectionOption() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($this->isCompress) { |
77
|
|
|
return "{$dumpCommand} > {$destinationPath}{$this->compressExtension}"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return "{$dumpCommand} --out {$destinationPath}"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getDatabaseOption() |
84
|
|
|
{ |
85
|
|
|
return !empty($this->dbName) ? "--db {$this->dbName}" : ""; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getUsernameOption() |
89
|
|
|
{ |
90
|
|
|
return !empty($this->username) ? "--username {$this->username}" : ""; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getPasswordOption() |
94
|
|
|
{ |
95
|
|
|
return !empty($this->password) ? "--password {$this->password}" : ""; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getHostOption() |
99
|
|
|
{ |
100
|
|
|
return !empty($this->host) ? "--host {$this->host}" : ""; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getPortOption() |
104
|
|
|
{ |
105
|
|
|
return !empty($this->port) ? "--port {$this->port}" : ""; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getAuthenticateDatabase() |
109
|
|
|
{ |
110
|
|
|
return !empty($this->authenticationDatabase) ? "--authenticationDatabase {$this->authenticationDatabase}" : ""; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getCollectionOption() |
114
|
|
|
{ |
115
|
|
|
return !empty($this->collection) ? "--collection {$this->collection}" : ""; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getArchiveOption() |
119
|
|
|
{ |
120
|
|
|
return |
121
|
|
|
} |
|
|
|
|
122
|
|
|
|
123
|
|
|
protected function prepareRestoreCommand(string $filePath): string |
124
|
|
|
{ |
125
|
|
|
|
126
|
|
|
$archive = $this->isCompress ? "--gzip --archive" : ""; |
127
|
|
|
|
128
|
|
|
$restoreCommand = sprintf("%smongorestore %s %s %s %s %s", |
129
|
|
|
$this->dumpCommandPath, |
130
|
|
|
$archive, |
131
|
|
|
$this->getHostOption(), |
132
|
|
|
$this->getPortOption(), |
133
|
|
|
$this->getUsernameOption(), |
134
|
|
|
$this->getAuthenticateDatabase() |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
if ($this->uri) { |
138
|
|
|
$restoreCommand = sprintf( |
139
|
|
|
'%smongorestore %s --uri %s', |
140
|
|
|
$this->dumpCommandPath, |
141
|
|
|
$archive, |
142
|
|
|
$this->uri |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($this->isCompress) { |
147
|
|
|
|
148
|
|
|
return "{$restoreCommand} < {$filePath}"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return "{$restoreCommand} {$filePath}"; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|