1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Commands; |
4
|
|
|
|
5
|
|
|
use CodexShaper\DBM\Facades\Driver; |
6
|
|
|
use CodexShaper\Dumper\Contracts\Dumper; |
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
|
|
|
|
51
|
|
|
return 'composer'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getFileName($table, $database) |
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 $dumper, array $data) |
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.'.$data['driver'].'.binary_path', ''); |
78
|
|
|
$hostname = config('database.connections.'.$data['driver'].'.host', '127.0.0.1'); |
79
|
|
|
$port = config('database.connections.'.$data['driver'].'.port', '3306'); |
80
|
|
|
$database = config('database.connections.'.$data['driver'].'.database', 'dbm'); |
81
|
|
|
$username = config('database.connections.'.$data['driver'].'.username', 'root'); |
82
|
|
|
$password = config('database.connections.'.$data['driver'].'.password', ''); |
83
|
|
|
|
84
|
|
|
$dumper->setHost($hostname) |
85
|
|
|
->setPort($port) |
86
|
|
|
->setDbName($database) |
87
|
|
|
->setUserName($username) |
88
|
|
|
->setPassword($password); |
89
|
|
|
|
90
|
|
|
switch ($data['driver']) { |
91
|
|
|
case 'mysql': |
92
|
|
|
case 'pgsql': |
93
|
|
|
if (! empty($data['table'])) { |
94
|
|
|
$dumper->setTables($data['table']); |
95
|
|
|
} |
96
|
|
|
break; |
97
|
|
|
case 'mongodb': |
98
|
|
|
$dsn = config('dbm.backup.mongodb.dsn', ''); |
99
|
|
|
if (! empty($dsn) && method_exists($dumper, 'setUri')) { |
100
|
|
|
$dumper->setUri($dsn); |
101
|
|
|
} |
102
|
|
|
break; |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($isCompress) { |
107
|
|
|
$dumper->setCompressBinaryPath($compressBinaryPath); |
108
|
|
|
$dumper->setCompressCommand($compressCommand); |
109
|
|
|
$dumper->setCompressExtension($compressExtension); |
110
|
|
|
} |
111
|
|
|
if ($isDebug) { |
112
|
|
|
$dumper->enableDebug(); |
113
|
|
|
} |
114
|
|
|
$dumper->setCommandBinaryPath($dumpBinaryPath) |
115
|
|
|
->setDestinationPath($data['filePath']) |
116
|
|
|
->dump(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Execute the console command. |
121
|
|
|
* |
122
|
|
|
* @param \Illuminate\Filesystem\Filesystem $filesystem |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function handle(Filesystem $filesystem, Dumper $dumper) |
127
|
|
|
{ |
128
|
|
|
$this->info('Start Database Backup'); |
129
|
|
|
|
130
|
|
|
$driver = dbm_driver(); |
131
|
|
|
$database = config('database.connections.'.$driver.'.database', 'dbm'); |
132
|
|
|
$table = ($this->option('table') != null) ? $this->option('table') : ''; |
133
|
|
|
|
134
|
|
|
try { |
135
|
|
|
$directory = (config('dbm.backup.dir', 'backups') != '') |
136
|
|
|
? DIRECTORY_SEPARATOR.config('dbm.backup.dir', 'backups') |
137
|
|
|
: ''; |
138
|
|
|
$directoryPath = storage_path('app').$directory.DIRECTORY_SEPARATOR.$driver; |
139
|
|
|
$filePath = $directoryPath.DIRECTORY_SEPARATOR.$this->getFileName($table, $database); |
140
|
|
|
|
141
|
|
|
if (! File::isDirectory($directoryPath)) { |
142
|
|
|
File::makeDirectory($directoryPath, 0777, true, true); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->backup($dumper, [ |
146
|
|
|
'filePath' => $filePath, |
147
|
|
|
'driver' => $driver, |
148
|
|
|
'table' => $table, |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$this->info('Backup completed'); |
152
|
|
|
} catch (\Exception $e) { |
153
|
|
|
throw new \Exception($e->getMessage(), 1); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|