1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Spatie\Backup\Events\BackupHasFailed; |
7
|
|
|
use Spatie\Backup\Exceptions\InvalidCommand; |
8
|
|
|
use Spatie\Backup\Tasks\Backup\BackupJobFactory; |
9
|
|
|
|
10
|
|
|
class BackupCommand extends BaseCommand |
11
|
|
|
{ |
12
|
|
|
/** @var string */ |
13
|
|
|
protected $signature = 'backup:run {--filename=} {--only-db} {--db-name=*} {--only-files} {--only-to-disk=} {--disable-notifications}'; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $description = 'Run the backup.'; |
17
|
|
|
|
18
|
|
|
public function handle() |
19
|
|
|
{ |
20
|
|
|
consoleOutput()->comment('Starting backup...'); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$disableNotifications = $this->option('disable-notifications'); |
23
|
|
|
|
24
|
|
|
try { |
25
|
|
|
$this->guardAgainstInvalidOptions(); |
26
|
|
|
|
27
|
|
|
$backupJob = BackupJobFactory::createFromArray(config('backup')); |
28
|
|
|
|
29
|
|
|
if ($this->option('only-db')) { |
30
|
|
|
$backupJob->dontBackupFilesystem(); |
31
|
|
|
} |
32
|
|
|
if ($this->option('db-name')) { |
33
|
|
|
$backupJob->onlyDbName($this->option('db-name')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($this->option('only-files')) { |
37
|
|
|
$backupJob->dontBackupDatabases(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->option('only-to-disk')) { |
41
|
|
|
$backupJob->onlyBackupTo($this->option('only-to-disk')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($this->option('filename')) { |
45
|
|
|
$backupJob->setFilename($this->option('filename')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($disableNotifications) { |
49
|
|
|
$backupJob->disableNotifications(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$backupJob->run(); |
53
|
|
|
|
54
|
|
|
consoleOutput()->comment('Backup completed!'); |
|
|
|
|
55
|
|
|
} catch (Exception $exception) { |
56
|
|
|
consoleOutput()->error("Backup failed because: {$exception->getMessage()}."); |
|
|
|
|
57
|
|
|
|
58
|
|
|
if (! $disableNotifications) { |
59
|
|
|
event(new BackupHasFailed($exception)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return 1; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function guardAgainstInvalidOptions() |
67
|
|
|
{ |
68
|
|
|
if ($this->option('only-db') && $this->option('only-files')) { |
69
|
|
|
throw InvalidCommand::create('Cannot use `only-db` and `only-files` together'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: