Passed
Push — master ( fabd45...a8694e )
by Ron
03:26 queued 11s
created

backupRun::createBackupArchive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        $process = new Process(/** @scrutinizer ignore-type */ 'mysqldump tb-dev5-data -u root');
Loading history...
116
        $process->run();
117
118
        File::put(config('filesystems.disks.backup.root') . DIRECTORY_SEPARATOR . 'database.sql', $process->getOutput());
119
        $this->bar->advance();
120
        $this->zip->add(config('filesystems.disks.backup.root') . DIRECTORY_SEPARATOR . 'database.sql');
121
        $this->bar->advance();
122
    }
123
124
    //  Remove any temporary files
125
    protected function cleanup()
126
    {
127
        $this->bar->advance();
128
        Storage::disk('backup')->delete('version.txt');
129
        Storage::disk('backup')->delete('database.sql');
130
    }
131
}
132