Passed
Push — dev5 ( da16e0...6692a0 )
by Ron
08:20
created

backupRun::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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