-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDumper.php
141 lines (117 loc) · 4.79 KB
/
MongoDumper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* This class allows you to dump any local mongoDB database, utilizing shell command
* to do so. If there is an error, please verify that the backup folder has the
* correct permissions and this script has execute permissions.
*
* Example:
* $dumper = new MongoDumper("/var/www/html/db-backups");
* $dumper->run("mydb", true); // 'true' shows debug info
* $dumper->run("mydb2", true); // 'true' shows debug info
* $dumper->run("mydb3");
*
*
* Used @ :
* copyMongoCollection.php
*/
namespace Project\Common\Mongo;
class MongoDumper
{
private $_BACKUP_FOLDER = null;
private $_CURRENT_DATE_TIME = null;
private $current_dump_path = null;
private $database = 'db_name';
private $host_from = 'source endpoint';
private $host_from_port = 27017;
private $host_to = 'destination endpoint';
private $host_to_port = 27017;
private $collection = '';
private $user = 'uname';
private $pass = 'pwd';
private $files_to_delete = array();
private $debug = false;
private $zip = false; //local safekeeping
public function __construct($backup_folder, $zip = false)
{
$now = new \DateTime;
$this->_BACKUP_FOLDER = rtrim($backup_folder, '/');
$this->zip = $zip;
$results = shell_exec("mkdir -p " . $this->_BACKUP_FOLDER);
$this->echo_if_debug("<p><strong>Creating directory '" . $this->_BACKUP_FOLDER . "'. Status:'" . $results . "'</strong></p>");
$this->_CURRENT_DATE_TIME = $now->format('d-m-Y_H-i');
}
public function run($collection, $debug = false)
{
$this->debug = ($debug === true);
try {
$this->current_dump_path = $this->_BACKUP_FOLDER . "/" . $this->database . "_" . $collection . "_" . $this->_CURRENT_DATE_TIME;
$this->collection = $collection;
$this->echo_if_debug("<p><strong>Backing up '" . $collection . "' to '" . $this->current_dump_path . "'</strong></p>");
$this->echo_if_debug("<ol>");
$this->echo_if_debug("<li>Executing mongodump...</li>");
$this->mongodump();
if ($this->zip) {
$this->echo_if_debug("<li>Zipping files...</li>");
$this->zip_files();
}
$this->mongorestore();
$this->echo_if_debug("<li>Deleting dump folder...</li>");
$this->delete_dump_folder();
$this->echo_if_debug("<li>Complete!</li>");
$this->echo_if_debug("</ol>");
return;
} catch (\Exception $ex) {
return false;
}
}
private function echo_if_debug($string)
{
if ($this->debug) {
echo $string;
}
}
private function mongodump()
{
$command = "mongodump -h " . $this->host_from . ":" . $this->host_from_port . " -u " . $this->user . " -p " . $this->pass . " -d " . $this->database . " -c " . $this->collection . " -o " . $this->current_dump_path;
$results = shell_exec($command);
$this->echo_if_debug("<ul><li>" . $command . "</li><li>" . $results . "</li></ul>");
}
private function mongorestore()
{
$command = "mongorestore -h " . $this->host_to . ":" . $this->host_to_port . " -u " . $this->user . " -p " . $this->pass . " -d " . $this->database . " -c " . $this->collection . " " . $this->current_dump_path . "/" . $this->database . "/" . $this->collection . ".bson";
$results = shell_exec($command);
$this->echo_if_debug("<ul><li>" . $command . "</li><li>" . $results . "</li></ul>");
}
private function zip_files()
{
$database_dump_folder = $this->current_dump_path . "/" . $this->database;
// Initialize archive object
$zip = new \ZipArchive;
$zip->open($this->current_dump_path . '.zip', \ZipArchive::CREATE);
// Create recursive directory iterator
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($database_dump_folder),
\RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
// Get real path for current file
$filePath = $file->getRealPath();
// Add current file to archive
$zip->addFile($filePath);
// add file to delete queue
$this->files_to_delete[] = $filePath;
}
$zip->close();
}
private function delete_dump_folder()
{
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->current_dump_path, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $file) {
$file->isDir() ? rmdir($file) : unlink($file);
}
rmdir($this->current_dump_path);
}
}