|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Zip; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Nwidart\Modules\Facades\Module; |
|
8
|
|
|
use PragmaRX\Version\Package\Version; |
|
9
|
|
|
|
|
10
|
|
|
use Illuminate\Bus\Queueable; |
|
11
|
|
|
use Illuminate\Support\Facades\Log; |
|
12
|
|
|
use Illuminate\Support\Facades\Storage; |
|
13
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
14
|
|
|
use Illuminate\Queue\SerializesModels; |
|
15
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
16
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
17
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
18
|
|
|
|
|
19
|
|
|
class ApplicationBackupJob implements ShouldQueue |
|
20
|
|
|
{ |
|
21
|
|
|
use Queueable; |
|
22
|
|
|
use Dispatchable; |
|
23
|
|
|
use SerializesModels; |
|
24
|
|
|
use InteractsWithQueue; |
|
25
|
|
|
|
|
26
|
|
|
protected $files; |
|
27
|
|
|
protected $archive; |
|
28
|
|
|
protected $database; |
|
29
|
|
|
protected $diskLocal; |
|
30
|
|
|
protected $backupName; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create a new job instance |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct($database = true, $files = true) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->database = $database; |
|
38
|
|
|
$this->files = $files; |
|
39
|
|
|
$this->backupName = 'TB_Backup_'.Carbon::now()->format('Ymd_his').'.zip'; |
|
40
|
|
|
$this->diskLocal = config('filesystems.disks.backups.root').DIRECTORY_SEPARATOR; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Execute the job |
|
45
|
|
|
*/ |
|
46
|
|
|
public function handle() |
|
47
|
|
|
{ |
|
48
|
|
|
Log::notice('Starting Application Backup', [ |
|
49
|
|
|
'Backup Database' => $this->database, |
|
50
|
|
|
'Backup Files' => $this->files |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$this->openArchive(); |
|
54
|
|
|
$this->backupFiles(); |
|
55
|
|
|
$this->backupDatabase(); |
|
56
|
|
|
$this->closeArchive(); |
|
57
|
|
|
$this->cleanup(); |
|
58
|
|
|
|
|
59
|
|
|
Log::notice('Backup Completed Successfully - file located at '.$this->diskLocal.$this->backupName); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create and open up the Archive Zip file |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function openArchive() |
|
66
|
|
|
{ |
|
67
|
|
|
// Create a file that holds the current version of the Tech Bench application |
|
68
|
|
|
Storage::disk('backups')->put('version.txt', (new Version)->version_only()); |
|
69
|
|
|
// Create a file that holds all enabled modules |
|
70
|
|
|
Storage::disk('backups')->put('modules.txt', Module::all()); |
|
71
|
|
|
|
|
72
|
|
|
// Create the backup Zip file |
|
73
|
|
|
$this->archive = Zip::create($this->diskLocal.$this->backupName); |
|
74
|
|
|
// Add the version and module files |
|
75
|
|
|
$this->archive->add($this->diskLocal.'version.txt'); |
|
76
|
|
|
$this->archive->add($this->diskLocal.'modules.txt'); |
|
77
|
|
|
// Add the .env file that holds local global config |
|
78
|
|
|
$this->archive->add(base_path().DIRECTORY_SEPARATOR.'.env'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Close and finish the Archive Zip File |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function closeArchive() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->archive->close(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Backup all of the Application Disks |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function backupFiles() |
|
93
|
|
|
{ |
|
94
|
|
|
if(!$this->files) |
|
95
|
|
|
{ |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// All uploaded files |
|
100
|
|
|
$this->archive->add(config('filesystems.disks.local.root')); |
|
101
|
|
|
// All public uploaded files (images for Tech Tips and Logos) |
|
102
|
|
|
$this->archive->add(config('filesystems.disks.public.root')); |
|
103
|
|
|
// All application logs |
|
104
|
|
|
$this->archive->add(config('filesystems.disks.logs.root')); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Backup the MySQL Database |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function backupDatabase() |
|
111
|
|
|
{ |
|
112
|
|
|
if(!$this->database) |
|
113
|
|
|
{ |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
Artisan::call('db:masked-dump', ['output' => $this->diskLocal.'backup.sql']); |
|
118
|
|
|
$this->archive->add($this->diskLocal.'backup.sql'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Remove any temporary files |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function cleanup() |
|
125
|
|
|
{ |
|
126
|
|
|
Storage::disk('backups')->delete('version.txt'); |
|
127
|
|
|
Storage::disk('backups')->delete('backup.sql'); |
|
128
|
|
|
Storage::disk('backups')->delete('modules.txt'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|