1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finder\Console\Commands; |
4
|
|
|
|
5
|
|
|
use File; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class DatabaseBackupCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = 'sitec:db:backup'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Backup and restore database.'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Execute the console command. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function fire() |
32
|
|
|
{ |
33
|
|
|
// Create destination dir if it does not exist |
34
|
|
|
$this->destination = base_path('database/backups'); |
|
|
|
|
35
|
|
|
if(! File::exists($this->destination)) { |
36
|
|
|
File::makeDirectory($this->destination); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Show list |
40
|
|
|
if($this->option('list')) { |
41
|
|
|
return $this->showListOfBackups(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Get database connection to use |
45
|
|
|
if(! $this->getConnection($this->option('connection'))) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Either restore ... |
50
|
|
|
if($this->option('restore')) { |
51
|
|
|
return ($this->restore($this->option('restore'), $this->option('force'))) ? $this->info('OK') : $this->error('Unable to restore'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// ... or backup |
55
|
|
|
return ($this->backup()) ? $this->info('OK') : $this->error('Unable to backup'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Show a list of exisisting backups. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
protected function showListOfBackups() |
64
|
|
|
{ |
65
|
|
|
if(! $files = File::files($this->destination)) { |
66
|
|
|
return $this->error('No backup files found'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach($files as $key => $file) |
70
|
|
|
{ |
71
|
|
|
$date = Carbon::createFromTimeStamp(File::lastModified($file)); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$files[$key] = [ |
74
|
|
|
basename($file), |
75
|
|
|
$date->toDateTimeString(), |
76
|
|
|
$date->diffForHumans(), |
77
|
|
|
$this->bytesToHuman(File::size($file)), |
|
|
|
|
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->table(['File', 'Date', 'Age', 'Size'], $files); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the database connection. |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
* @return array|string |
89
|
|
|
*/ |
90
|
|
|
protected function getConnection($name) |
91
|
|
|
{ |
92
|
|
|
// Make sure connection exists |
93
|
|
|
if(! $connection = \Illuminate\Support\Facades\Config::get("database.connections.$name")) { |
94
|
|
|
$this->error("Unknown connection '$name'"); |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Make sure the connection is MySQL |
100
|
|
|
if($connection['driver'] !== 'mysql') { |
101
|
|
|
$this->error("Unsupported connection type '{$connection['driver']}'. Only 'mysql' connections are supported"); |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->connection = $connection; |
|
|
|
|
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create a backup. |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected function backup() |
117
|
|
|
{ |
118
|
|
|
$date = Carbon::now()->toDateTimeString(); |
119
|
|
|
$file = $this->destination . DIRECTORY_SEPARATOR . $date . ' ' . $this->connection['database'] . '.sql'; |
120
|
|
|
|
121
|
|
|
// Build backup command |
122
|
|
|
$command = sprintf( |
123
|
|
|
'mysqldump --host=%s --user=%s --password=%s %s > %s', |
124
|
|
|
escapeshellarg($this->connection['host']), |
125
|
|
|
escapeshellarg($this->connection['username']), |
126
|
|
|
escapeshellarg($this->connection['password']), |
127
|
|
|
escapeshellarg($this->connection['database']), |
128
|
|
|
escapeshellarg(str_replace([' ', ':'], ['_', '.'], $file)) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
// Exec backup command |
132
|
|
|
exec($command, $output, $returnValue); |
133
|
|
|
|
134
|
|
|
return ($returnValue === 0); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Restore a backup. |
139
|
|
|
* |
140
|
|
|
* @param string $file File to restore |
141
|
|
|
* @param bool $force Do not prompt for confirmation |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
protected function restore($file, $force = false) |
145
|
|
|
{ |
146
|
|
|
// Check if file exist |
147
|
|
|
$file = $this->destination . DIRECTORY_SEPARATOR . $file; |
148
|
|
|
if(! File::exists($file)) { |
149
|
|
|
$this->error("File not found '$file'"); |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Promt for confirmation |
155
|
|
|
if(! $force and ! $this->confirm('This could delete existing data. Do you wish to continue?', false)) { |
156
|
|
|
$this->comment('Cancelled by user'); |
157
|
|
|
|
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// Build restore command |
162
|
|
|
$command = sprintf( |
163
|
|
|
'mysql --host=%s --user=%s --password=%s %s < %s', |
164
|
|
|
escapeshellarg($this->connection['host']), |
165
|
|
|
escapeshellarg($this->connection['username']), |
166
|
|
|
escapeshellarg($this->connection['password']), |
167
|
|
|
escapeshellarg($this->connection['database']), |
168
|
|
|
escapeshellarg($file) |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
// Exec restore command |
172
|
|
|
exec($command, $output, $returnValue); |
173
|
|
|
|
174
|
|
|
return ($returnValue === 0); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Convert bytes to human friendly unit. |
179
|
|
|
* |
180
|
|
|
* @param integer $bytes |
181
|
|
|
* @param integer $decimals |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function bytesToHuman($bytes, $decimals = 2) |
185
|
|
|
{ |
186
|
|
|
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); |
187
|
|
|
$factor = floor((strlen($bytes) - 1) / 3); |
188
|
|
|
|
189
|
|
|
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the console command options. |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
protected function getOptions() |
198
|
|
|
{ |
199
|
|
|
return [ |
200
|
|
|
['list', 'l', InputOption::VALUE_NONE, 'List saved backups'], |
201
|
|
|
['restore', 'r', InputOption::VALUE_REQUIRED, 'Restore a backup'], |
202
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'Do not prompt for confimation when restoring a backup'], |
203
|
|
|
['connection', 'c', InputOption::VALUE_REQUIRED, 'Database connection name', \Illuminate\Support\Facades\Config::get('database.default')], |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: