|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\Log; |
|
7
|
|
|
use Illuminate\Support\Facades\File; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\Component\Process\Process; |
|
11
|
|
|
|
|
12
|
|
|
use Zip; |
|
13
|
|
|
use Carbon\Carbon; |
|
14
|
|
|
|
|
15
|
|
|
class backupRun extends Command |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The name and signature of the console command. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $signature = 'tb-backup:run |
|
23
|
|
|
{--databaseOnly : Only backup configuration database} |
|
24
|
|
|
{--filesOnly : Only backup uploaded files}'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The console command description. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $description = 'Creates a Tech Bench Backup'; |
|
32
|
|
|
|
|
33
|
|
|
protected $archive, $bar; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a new command instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
2 |
|
parent::__construct(); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Execute the console command. |
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
public function handle() |
|
51
|
|
|
{ |
|
52
|
|
|
Storage::disk('backup')->put('.ignore', ''); |
|
53
|
|
|
$this->info('Creating Tech Bench Backup...'); |
|
54
|
|
|
$this->line('Please note, for large applications this may take some time'); |
|
55
|
|
|
$this->line('and appear that the script has locked up'); |
|
56
|
|
|
$this->bar = $this->output->createProgressBar(12); |
|
57
|
|
|
Log::notice('System Backup script has begun'); |
|
58
|
|
|
|
|
59
|
|
|
$this->createBackupArchive(); |
|
60
|
|
|
|
|
61
|
|
|
if(!$this->option('databaseOnly')) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->backupFiles(); |
|
64
|
|
|
} |
|
65
|
|
|
if(!$this->option('filesOnly')) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->backupDatabase(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->closeBackupArchive(); |
|
71
|
|
|
$this->cleanup(); |
|
72
|
|
|
$this->bar->finish(); |
|
73
|
|
|
$this->line(''); |
|
74
|
|
|
$this->info('Backup Completed'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
protected function createBackupArchive() |
|
79
|
|
|
{ |
|
80
|
|
|
$backupName = 'TB_Backup_'.Carbon::now()->format('Ymd_his').'.zip'; |
|
81
|
|
|
Log::info('Backup archive name - '.$backupName.' created'); |
|
82
|
|
|
|
|
83
|
|
|
// Create a text file holding the current system version |
|
84
|
|
|
$version = new \PragmaRX\Version\Package\Version(); |
|
85
|
|
|
File::put(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'version.txt', $version->version_only()); |
|
86
|
|
|
$this->bar->advance(); |
|
87
|
|
|
|
|
88
|
|
|
// Create the archive and place the version file in it |
|
89
|
|
|
$this->zip = Zip::create(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.$backupName); |
|
90
|
|
|
$this->bar->advance(); |
|
91
|
|
|
$this->zip->add(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'version.txt'); |
|
92
|
|
|
$this->bar->advance(); |
|
93
|
|
|
$this->zip->add(base_path().DIRECTORY_SEPARATOR.'.env'); |
|
94
|
|
|
$this->bar->advance(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function closeBackupArchive() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->zip->close(); |
|
100
|
|
|
$this->bar->advance(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Backup stored files |
|
104
|
|
|
protected function backupFiles() |
|
105
|
|
|
{ |
|
106
|
|
|
// All uploaded files |
|
107
|
|
|
$this->zip->add(config('filesystems.disks.local.root')); |
|
108
|
|
|
$this->bar->advance(); |
|
109
|
|
|
// All publicly available files |
|
110
|
|
|
$this->zip->add(config('filesystems.disks.public.root')); |
|
111
|
|
|
$this->bar->advance(); |
|
112
|
|
|
// All system logs |
|
113
|
|
|
$this->zip->add(config('filesystems.disks.logs.root')); |
|
114
|
|
|
$this->bar->advance(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Create a MYSQLDUMP of the database |
|
118
|
|
|
protected function backupDatabase() |
|
119
|
|
|
{ |
|
120
|
|
|
$processStr = 'mysqldump '. |
|
121
|
|
|
config('database.connections.mysql.database').' -u '. |
|
122
|
|
|
config('database.connections.mysql.username').' -p'. |
|
123
|
|
|
config('database.connections.mysql.password'); |
|
124
|
|
|
|
|
125
|
|
|
$process = new Process(/** @scrutinizer ignore-type */$processStr); |
|
126
|
|
|
$process->run(); |
|
127
|
|
|
|
|
128
|
|
|
File::put(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'database.sql', $process->getOutput()); |
|
129
|
|
|
$this->bar->advance(); |
|
130
|
|
|
$this->zip->add(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'database.sql'); |
|
131
|
|
|
$this->bar->advance(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// Remove any temporary files |
|
135
|
|
|
protected function cleanup() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->bar->advance(); |
|
138
|
|
|
Storage::disk('backup')->delete('version.txt'); |
|
139
|
|
|
Storage::disk('backup')->delete('database.sql'); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|