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
|
|
|
/** |
71
|
|
|
* Execute the console command. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function handle(Filesystem $filesystem, Dumper $dumper) |
78
|
|
|
{ |
79
|
|
|
$this->info('Start Database Backup'); |
80
|
|
|
|
81
|
|
|
$driver = dbm_driver(); |
82
|
|
|
|
83
|
|
|
$hostname = config('database.connections.' . $driver . '.host', '127.0.0.1'); |
|
|
|
|
84
|
|
|
$port = config('database.connections.' . $driver . '.port', '3306'); |
|
|
|
|
85
|
|
|
$database = config('database.connections.' . $driver . '.database', 'dbm'); |
86
|
|
|
$table = ($this->option('table') != null) ? $this->option('table') : ''; |
87
|
|
|
$username = config('database.connections.' . $driver . '.username', 'root'); |
|
|
|
|
88
|
|
|
$password = config('database.connections.' . $driver . '.password', ''); |
|
|
|
|
89
|
|
|
|
90
|
|
|
try |
91
|
|
|
{ |
92
|
|
|
$directory = (config('dbm.backup.dir', 'backups') != '') |
93
|
|
|
? DIRECTORY_SEPARATOR . config('dbm.backup.dir', 'backups') |
94
|
|
|
: ''; |
95
|
|
|
$directoryPath = DBM::getPathPrefix() . $directory . DIRECTORY_SEPARATOR . $driver; |
96
|
|
|
$filePath = $directoryPath . DIRECTORY_SEPARATOR . $this->getFileName($table, $database); |
97
|
|
|
|
98
|
|
|
if (!File::isDirectory($directoryPath)) { |
99
|
|
|
|
100
|
|
|
File::makeDirectory($directoryPath, 0777, true, true); |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$isCompress = config('dbm.backup.compress', false); |
105
|
|
|
$isDebug = config('dbm.backup.debug', false); |
106
|
|
|
$compressBinaryPath = config('dbm.backup.compress_binary_path', ""); |
107
|
|
|
$compressCommand = config('dbm.backup.compress_command', "gzip"); |
108
|
|
|
$compressExtension = config('dbm.backup.compress_extension', ".gz"); |
109
|
|
|
$dumpBinaryPath = config('dbm.backup.' . $driver . '.binary_path', ''); |
110
|
|
|
|
111
|
|
|
switch ($driver) { |
112
|
|
|
case 'mysql': |
113
|
|
|
case 'pgsql': |
114
|
|
|
if (!empty($table)) { |
115
|
|
|
$dumper->setTables($table); |
116
|
|
|
} |
117
|
|
|
break; |
118
|
|
|
case 'mongodb': |
119
|
|
|
$dsn = config('dbm.backup.mongodb.dsn', ''); |
120
|
|
|
if (!empty($dsn)) { |
121
|
|
|
$dumper->setUri($dsn); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
break; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($isCompress) { |
128
|
|
|
$dumper->setCompressBinaryPath($compressBinaryPath); |
129
|
|
|
$dumper->setCompressCommand($compressCommand); |
130
|
|
|
$dumper->setCompressExtension($compressExtension); |
131
|
|
|
} |
132
|
|
|
if ($isDebug) { |
133
|
|
|
$dumper->enableDebug(); |
134
|
|
|
} |
135
|
|
|
$dumper->setCommandBinaryPath($dumpBinaryPath) |
136
|
|
|
->setDestinationPath($filePath) |
137
|
|
|
->dump(); |
138
|
|
|
|
139
|
|
|
$this->info("Backup completed"); |
140
|
|
|
|
141
|
|
|
} catch (\Exception $e) { |
142
|
|
|
|
143
|
|
|
throw new \Exception($e->getMessage(), 1); |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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