1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Zip; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
use Illuminate\Support\Facades\File; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use Symfony\Component\Process\Process; |
12
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
13
|
|
|
|
14
|
|
|
class BackupApplication extends Command |
15
|
|
|
{ |
16
|
|
|
// Command Name |
17
|
|
|
protected $signature = 'backup:now {type=manual}'; |
18
|
|
|
|
19
|
|
|
// Command description |
20
|
|
|
protected $description = 'Create a full backup of the Tech Bench Application'; |
21
|
|
|
|
22
|
|
|
// Constructor |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Create backup of system |
29
|
|
|
public function handle() |
30
|
|
|
{ |
31
|
|
|
$backupType = $this->argument('type'); |
32
|
|
|
$backupDir = config('filesystems.disks.backup.root'); |
33
|
|
|
$backupTmp = config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'tmp'; |
34
|
|
|
$localDir = config('filesystems.disks.local.root'); |
35
|
|
|
$publicDir = config('filesystems.disks.public.root'); |
36
|
|
|
$backupBase = $backupType.'_backup-'.Carbon::now()->toDateString().'_'.Carbon::now()->hour.Carbon::now()->minute; |
37
|
|
|
|
38
|
|
|
// Determine if the backup file already exists |
39
|
|
|
$i = 0; |
40
|
|
|
do |
41
|
|
|
{ |
42
|
|
|
if($i) |
43
|
|
|
{ |
44
|
|
|
$backupName = $backupBase.'('.$i.')'; |
45
|
|
|
} |
46
|
|
|
else |
47
|
|
|
{ |
48
|
|
|
$backupName = $backupBase; |
49
|
|
|
} |
50
|
|
|
$i++; |
51
|
|
|
} while(Storage::disk('backup')->exists($backupName.'.zip')); |
52
|
|
|
|
53
|
|
|
// Write a file that shows the system version |
54
|
|
|
$version = new \PragmaRX\Version\Package\Version(); |
55
|
|
|
Storage::disk('backup')->put('tmp/version.txt', $version->compact()); |
56
|
|
|
|
57
|
|
|
// Create a dump file of the MySQL database |
58
|
|
|
$process = new Process(sprintf( |
|
|
|
|
59
|
|
|
'mysqldump -u%s -p%s %s > %s', |
60
|
|
|
config('database.connections.mysql.username'), |
61
|
|
|
config('database.connections.mysql.password'), |
62
|
|
|
config('database.connections.mysql.database'), |
63
|
|
|
$backupTmp.DIRECTORY_SEPARATOR.'db_dump.sql' |
64
|
|
|
)); |
65
|
|
|
$process->mustRun(); |
66
|
|
|
|
67
|
|
|
// Create a zip archive of the tmp folder |
68
|
|
|
$zip = Zip::create($backupDir.DIRECTORY_SEPARATOR.$backupName.'.zip'); |
69
|
|
|
$zip->add($backupTmp.DIRECTORY_SEPARATOR.'version.txt'); |
70
|
|
|
$zip->add($backupTmp.DIRECTORY_SEPARATOR.'db_dump.sql'); |
71
|
|
|
$zip->add(base_path('.env')); |
72
|
|
|
$zip->add($localDir); |
73
|
|
|
$zip->add($publicDir); |
74
|
|
|
$zip->add(storage_path('logs')); |
75
|
|
|
$zip->close(); |
76
|
|
|
|
77
|
|
|
// Clean up the tmp folder |
78
|
|
|
Storage::disk('backup')->deleteDirectory('tmp'); |
79
|
|
|
|
80
|
|
|
Log::notice('Backup file created successfully'); |
81
|
|
|
$this->line('Backup Successful'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|