1
|
|
|
<?php |
2
|
|
|
namespace CodexShaper\DBM\Commands; |
3
|
|
|
|
4
|
|
|
use CodexShaper\DBM\Facades\Driver; |
5
|
|
|
use CodexShaper\Dumper\Contracts\Dumper; |
6
|
|
|
use DBM; |
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
9
|
|
|
use Illuminate\Support\Facades\File; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
|
12
|
|
|
class DatabaseBackup extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The console command name. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'dbm:backup |
20
|
|
|
{--t|table=}'; |
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Database Backup'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get Option |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
protected function getOptions() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production', null], |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the composer command for the environment. |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
protected function findComposer() |
46
|
|
|
{ |
47
|
|
|
if (file_exists(getcwd() . '/composer.phar')) { |
48
|
|
|
return '"' . PHP_BINARY . '" ' . getcwd() . '/composer.phar'; |
49
|
|
|
} |
50
|
|
|
return 'composer'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getFileName($table, $database) |
54
|
|
|
{ |
55
|
|
|
|
56
|
|
|
$prefix = (strlen($table) > 0) |
57
|
|
|
? 'table_' . strtolower(str_replace('-', '_', $table)) . "_" |
58
|
|
|
: 'database_' . strtolower(str_replace('-', '_', $database)) . "_"; |
59
|
|
|
|
60
|
|
|
$extension = Driver::isMongoDB() ? '' : '.sql'; |
61
|
|
|
$fileName = $prefix . 'backup_' . date('G_a_m_d_y_h_i_s') . $extension; |
62
|
|
|
|
63
|
|
|
if (Driver::isSqlite()) { |
64
|
|
|
$fileName = 'backup_' . date('G_a_m_d_y_h_i_s') . $extension; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $fileName; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function backup($dumper, $filePath) |
71
|
|
|
{ |
72
|
|
|
$isCompress = config('dbm.backup.compress', false); |
73
|
|
|
$isDebug = config('dbm.backup.debug', false); |
74
|
|
|
$compressBinaryPath = config('dbm.backup.compress_binary_path', ""); |
75
|
|
|
$compressCommand = config('dbm.backup.compress_command', "gzip"); |
76
|
|
|
$compressExtension = config('dbm.backup.compress_extension', ".gz"); |
77
|
|
|
$dumpBinaryPath = config('dbm.backup.' . $driver . '.binary_path', ''); |
|
|
|
|
78
|
|
|
|
79
|
|
|
switch ($driver) { |
80
|
|
|
case 'mysql': |
81
|
|
|
case 'pgsql': |
82
|
|
|
if (!empty($table)) { |
|
|
|
|
83
|
|
|
$dumper->setTables($table); |
84
|
|
|
} |
85
|
|
|
break; |
86
|
|
|
case 'mongodb': |
87
|
|
|
$dsn = config('dbm.backup.mongodb.dsn', ''); |
88
|
|
|
if (!empty($dsn) && method_exists($dumper, 'setUri')) { |
89
|
|
|
$dumper->setUri($dsn); |
90
|
|
|
} |
91
|
|
|
break; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($isCompress) { |
96
|
|
|
$dumper->setCompressBinaryPath($compressBinaryPath); |
97
|
|
|
$dumper->setCompressCommand($compressCommand); |
98
|
|
|
$dumper->setCompressExtension($compressExtension); |
99
|
|
|
} |
100
|
|
|
if ($isDebug) { |
101
|
|
|
$dumper->enableDebug(); |
102
|
|
|
} |
103
|
|
|
$dumper->setCommandBinaryPath($dumpBinaryPath) |
104
|
|
|
->setDestinationPath($filePath) |
105
|
|
|
->dump(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Execute the console command. |
110
|
|
|
* |
111
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function handle(Filesystem $filesystem, Dumper $dumper) |
116
|
|
|
{ |
117
|
|
|
$this->info('Start Database Backup'); |
118
|
|
|
|
119
|
|
|
$driver = dbm_driver(); |
120
|
|
|
|
121
|
|
|
// $hostname = config('database.connections.' . $driver . '.host', '127.0.0.1'); |
122
|
|
|
// $port = config('database.connections.' . $driver . '.port', '3306'); |
123
|
|
|
$database = config('database.connections.' . $driver . '.database', 'dbm'); |
124
|
|
|
$table = ($this->option('table') != null) ? $this->option('table') : ''; |
125
|
|
|
// $username = config('database.connections.' . $driver . '.username', 'root'); |
126
|
|
|
// $password = config('database.connections.' . $driver . '.password', ''); |
127
|
|
|
|
128
|
|
|
try |
129
|
|
|
{ |
130
|
|
|
$directory = (config('dbm.backup.dir', 'backups') != '') |
131
|
|
|
? DIRECTORY_SEPARATOR . config('dbm.backup.dir', 'backups') |
132
|
|
|
: ''; |
133
|
|
|
$directoryPath = DBM::getPathPrefix() . $directory . DIRECTORY_SEPARATOR . $driver; |
134
|
|
|
$filePath = $directoryPath . DIRECTORY_SEPARATOR . $this->getFileName($table, $database); |
135
|
|
|
|
136
|
|
|
if (!File::isDirectory($directoryPath)) { |
137
|
|
|
|
138
|
|
|
File::makeDirectory($directoryPath, 0777, true, true); |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->backup($dumper, $filePath); |
143
|
|
|
|
144
|
|
|
$this->info("Backup completed"); |
145
|
|
|
|
146
|
|
|
} catch (\Exception $e) { |
147
|
|
|
|
148
|
|
|
throw new \Exception($e->getMessage(), 1); |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths