|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Bus\Queueable; |
|
7
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique; |
|
8
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
9
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
10
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
11
|
|
|
use Illuminate\Queue\SerializesModels; |
|
12
|
|
|
use Illuminate\Support\Facades\File; |
|
13
|
|
|
use Illuminate\Support\Facades\Log; |
|
14
|
|
|
use Illuminate\Support\Facades\Storage; |
|
15
|
|
|
use PragmaRX\Version\Package\Version; |
|
16
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
|
17
|
|
|
use Zip; |
|
18
|
|
|
use Symfony\Component\Process\Process; |
|
19
|
|
|
|
|
20
|
|
|
class ApplicationBackupJob implements ShouldQueue |
|
21
|
|
|
{ |
|
22
|
|
|
use Queueable; |
|
23
|
|
|
use Dispatchable; |
|
24
|
|
|
use SerializesModels; |
|
25
|
|
|
use InteractsWithQueue; |
|
26
|
|
|
|
|
27
|
|
|
protected $files; |
|
28
|
|
|
protected $archive; |
|
29
|
|
|
protected $database; |
|
30
|
|
|
protected $diskLocal; |
|
31
|
|
|
protected $backupName; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a new job instance |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($database = true, $files = true) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->database = $database; |
|
39
|
|
|
$this->files = $files; |
|
40
|
|
|
$this->backupName = 'TB_Backup_'.Carbon::now()->format('Ymd_his').'.zip'; |
|
41
|
|
|
$this->diskLocal = config('filesystems.disks.backups.root').DIRECTORY_SEPARATOR; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Execute the job |
|
46
|
|
|
*/ |
|
47
|
|
|
public function handle() |
|
48
|
|
|
{ |
|
49
|
|
|
Log::notice('Starting Application Backup', [ |
|
50
|
|
|
'Backup Database' => $this->database, |
|
51
|
|
|
'Backup Files' => $this->files |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
// $this->openArchive(); |
|
55
|
|
|
// $this->backupFiles(); |
|
56
|
|
|
$this->backupDatabase(); |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
|
|
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
|
|
|
|
|
70
|
|
|
// Create the backup Zip file |
|
71
|
|
|
$this->archive = Zip::create($this->diskLocal.$this->backupName); |
|
72
|
|
|
// Add the version file |
|
73
|
|
|
$this->archive->add($this->diskLocal.'version.txt'); |
|
74
|
|
|
// Add the .env file that holds local global config |
|
75
|
|
|
$this->archive->add(base_path().DIRECTORY_SEPARATOR.'.env'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Backup all of the Application Disks |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function backupFiles() |
|
82
|
|
|
{ |
|
83
|
|
|
if(!$this->files) |
|
84
|
|
|
{ |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// All uploaded files |
|
89
|
|
|
$this->archive->add(config('filesystems.disks.local.root')); |
|
90
|
|
|
// All public uploaded files (images for Tech Tips and Logos) |
|
91
|
|
|
$this->archive->add(config('filesystems.disks.public.root')); |
|
92
|
|
|
// All application logs |
|
93
|
|
|
$this->archive->add(config('filesystems.disks.logs.root')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Backup the MySQL Database |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function backupDatabase() |
|
100
|
|
|
{ |
|
101
|
|
|
if(!$this->database) |
|
102
|
|
|
{ |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$processStr = ['mysqldump '. |
|
107
|
|
|
config('database.connections.mysql.database').' -u '. |
|
108
|
|
|
config('database.connections.mysql.username').' -p'. |
|
109
|
|
|
config('database.connections.mysql.password')]; |
|
110
|
|
|
|
|
111
|
|
|
$process = new Process($processStr); |
|
112
|
|
|
|
|
113
|
|
|
try{ |
|
114
|
|
|
|
|
115
|
|
|
$process->run(); |
|
116
|
|
|
} |
|
117
|
|
|
catch(ProcessFailedException $e) |
|
118
|
|
|
{ |
|
119
|
|
|
Log::critical('process failed'); |
|
120
|
|
|
report($e); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$output = $process->getOutput(); |
|
124
|
|
|
Log::critical($output); |
|
125
|
|
|
|
|
126
|
|
|
Storage::disk('backups')->put('database.sql', $output); |
|
127
|
|
|
// $this->archive->add($this->diskLocal.'database.sql'); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|