Skip to content

Commit 2fc6ca4

Browse files
committed
Initilize project
1 parent 6a1740b commit 2fc6ca4

File tree

8 files changed

+828
-0
lines changed

8 files changed

+828
-0
lines changed

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "codexshaper/database-backup-restore",
3+
"description": "Database Backup and Restore",
4+
"keywords": [
5+
"databse",
6+
"databse-backup",
7+
"databse-restore",
8+
"dumper",
9+
"mysql",
10+
"pgsql",
11+
"sqlite",
12+
"mongodb"
13+
],
14+
"type": "library",
15+
"license": "MIT",
16+
"homepage": "https://github.com/Codexshaper/database-backup-restore",
17+
"authors": [
18+
{
19+
"name": "Md Abu Ahsan Basir",
20+
"email": "maab.career@gmail.com",
21+
"homepage": "https://github.com/maab16",
22+
"role": "Owner and Developer"
23+
}
24+
],
25+
"minimum-stability": "dev",
26+
"require": {
27+
"php" : "^7.2",
28+
"symfony/process": "^4.2|^5.0"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"CodexShaper\\Dumper\\": "src/"
33+
}
34+
}
35+
}

src/Contracts/Dumper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace CodexShaper\Dumper\Contracts;
3+
4+
interface Dumper
5+
{
6+
public function dump();
7+
public function restore();
8+
}

src/Drivers/MongoDumper.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
$command = $this->prepareDumpCommand($destinationPath);
39+
$this->run($command);
40+
}
41+
42+
public function restore(string $restorePath = "")
43+
{
44+
$restorePath = !empty($restorePath) ? $restorePath : $this->restorePath;
45+
$command = $this->prepareRestoreCommand($restorePath);
46+
$this->run($command);
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+
$databaseArg = !empty($this->dbName) ? "--db " . escapeshellarg($this->dbName) : "";
98+
$username = !empty($this->username) ? "--username " . escapeshellarg($this->username) : "";
99+
$password = !empty($this->password) ? "--password " . escapeshellarg($this->password) : "";
100+
$host = !empty($this->host) ? "--host " . escapeshellarg($this->host) : "";
101+
$port = !empty($this->port) ? "--port " . escapeshellarg($this->port) : "";
102+
$collection = !empty($this->collection) ? "--collection " . escapeshellarg($this->collection) : "";
103+
$authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : "";
104+
105+
$archive = "";
106+
107+
if ($this->isCompress) {
108+
109+
$archive = "--gzip --archive";
110+
}
111+
112+
$restoreCommand = sprintf("%smongorestore %s %s %s %s %s",
113+
$this->dumpCommandPath,
114+
$archive,
115+
$host,
116+
$port,
117+
$username,
118+
$authenticationDatabase
119+
);
120+
121+
if ($this->uri) {
122+
$restoreCommand = sprintf(
123+
'%smongorestore %s --uri %s',
124+
$this->dumpCommandPath,
125+
$archive,
126+
$this->uri
127+
);
128+
}
129+
130+
if ($this->isCompress) {
131+
132+
return "{$restoreCommand} < {$filePath}";
133+
}
134+
135+
return "{$restoreCommand} {$filePath}";
136+
}
137+
}

src/Drivers/MysqlDumper.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace CodexShaper\Dumper\Drivers;
4+
5+
use CodexShaper\Dumper\Dumper;
6+
7+
class MysqlDumper extends Dumper
8+
{
9+
/*@var bool*/
10+
protected $singleTransaction = false;
11+
/*@var bool*/
12+
protected $skipLockTables = false;
13+
/*@var bool*/
14+
protected $quick = false;
15+
/*@var bool*/
16+
protected $skipComments = true;
17+
/*@var string*/
18+
protected $defaultCharacterSet = '';
19+
/*@var bool*/
20+
protected $createTables = true;
21+
22+
public function useSingleTransaction()
23+
{
24+
$this->singleTransaction = true;
25+
return $this;
26+
}
27+
public function useSkipLockTables()
28+
{
29+
$this->skipLockTables = true;
30+
return $this;
31+
}
32+
public function useQuick()
33+
{
34+
$this->quick = true;
35+
return $this;
36+
}
37+
public function doNotUseSkipComments()
38+
{
39+
$this->skipComments = false;
40+
return $this;
41+
}
42+
public function doNotUseCreateTables()
43+
{
44+
$this->createTables = false;
45+
return $this;
46+
}
47+
public function setDefaultCharacterSe(string $charecterSet)
48+
{
49+
$this->defaultCharacterSe = $charecterSet;
50+
return $this;
51+
}
52+
53+
public function dump(string $destinationPath = "")
54+
{
55+
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
56+
$this->runCommand($destinationPath, "dump");
57+
}
58+
59+
public function restore(string $restorePath = "")
60+
{
61+
$restorePath = !empty($restorePath) ? $restorePath : $this->restorePath;
62+
$this->runCommand($restorePath, 'restore');
63+
}
64+
65+
protected function prepareDumpCommand(string $credentialFile, string $destinationPath): string
66+
{
67+
$databaseArg = escapeshellarg($this->dbName);
68+
69+
$includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : "";
70+
$includeTablesArg = !empty($includeTables) ? '--tables ' . escapeshellarg($includeTables) : '';
71+
72+
$ignoreTablesArgs = [];
73+
foreach ($this->ignoreTables as $tableName) {
74+
$ignoreTablesArgs[] = "--ignore-table=" . $databaseArg . "." . escapeshellarg($tableName);
75+
}
76+
$ignoreTablesArg = (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : '';
77+
78+
$singleTransaction = ($this->singleTransaction) ? "--single-transaction" : "";
79+
$skipLockTable = ($this->skipLockTables) ? "--skip-lock-tables" : "";
80+
$quick = ($this->quick) ? "--quick" : "";
81+
$createTables = (!$this->createTables) ? '--no-create-info' : '';
82+
$skipComments = ($this->skipComments) ? '--skip-comments' : '';
83+
$socket = ($this->socket !== '') ? "--socket={$this->socket}" : '';
84+
$defaultCharacterSet = ($this->defaultCharacterSet !== '') ? '--default-character-set=' . $this->defaultCharacterSet : '';
85+
86+
$authenticate = "--defaults-extra-file=" . $credentialFile;
87+
88+
$dumpCommand = sprintf(
89+
'%smysqldump %s %s %s %s %s %s %s %s %s %s %s',
90+
$this->dumpCommandPath,
91+
$authenticate,
92+
$databaseArg,
93+
$socket,
94+
$skipComments,
95+
$createTables,
96+
$singleTransaction,
97+
$skipLockTable,
98+
$quick,
99+
$defaultCharacterSet,
100+
$includeTablesArg,
101+
$ignoreTablesArg
102+
);
103+
104+
if ($this->isCompress) {
105+
106+
return "{$dumpCommand} | {$this->compressBinaryPath}{$this->compressCommand} > {$destinationPath}{$this->compressExtension}";
107+
}
108+
109+
return "{$dumpCommand} > {$destinationPath}";
110+
}
111+
112+
protected function prepareRestoreCommand(string $credentialFile, string $filePath): string
113+
{
114+
$database = escapeshellarg($this->dbName);
115+
$authenticate = "--defaults-extra-file=" . $credentialFile;
116+
117+
$restoreCommand = sprintf("%smysql %s %s",
118+
$this->dumpCommandPath,
119+
$authenticate,
120+
$database
121+
);
122+
123+
if ($this->isCompress) {
124+
125+
return "{$this->compressBinaryPath}{$this->compressCommand} < {$filePath} | {$restoreCommand}";
126+
}
127+
128+
return "{$restoreCommand} < {$filePath}";
129+
}
130+
131+
protected function runCommand($filePath, $action)
132+
{
133+
try {
134+
135+
$credentials = $this->getCredentials();
136+
$credentialFile = tempnam(sys_get_temp_dir(), 'mysqlpass');
137+
$handler = fopen($credentialFile, 'r+');
138+
fwrite($handler, $credentials);
139+
140+
if ($action == 'dump') {
141+
$command = $this->prepareDumpCommand($credentialFile, $filePath);
142+
}
143+
144+
if ($action == 'restore') {
145+
$command = $this->prepareRestoreCommand($credentialFile, $filePath);
146+
}
147+
148+
$process = $this->prepareProcessCommand($command);
149+
$process->mustRun();
150+
151+
fclose($handler);
152+
unlink($credentialFile);
153+
154+
} catch (ProcessFailedException $e) {
155+
throw new \Exception($e->getMessage());
156+
157+
}
158+
}
159+
160+
protected function getCredentials()
161+
{
162+
$contents = [
163+
'[client]',
164+
"user = '{$this->username}'",
165+
"password = '{$this->password}'",
166+
"host = '{$this->host}'",
167+
"port = '{$this->port}'",
168+
];
169+
return implode(PHP_EOL, $contents);
170+
}
171+
}

0 commit comments

Comments
 (0)