Passed
Push — dev5 ( 4f6f89...5d8b6d )
by Ron
08:48
created

backupRun   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 126
ccs 3
cts 60
cp 0.05
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A backupFiles() 0 11 1
A handle() 0 25 3
A backupDatabase() 0 16 1
A cleanup() 0 5 1
A createBackupArchive() 0 16 1
A closeBackupArchive() 0 4 1
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 Backup';
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
        Storage::disk('backup')->put('.ignore', '');
50
        $this->line('Creating Tech Bench Backup...');
51
        $this->bar = $this->output->createProgressBar(12);
52
53
        $this->createBackupArchive();
54
55
        if(!$this->option('databaseOnly'))
56
        {
57
            $this->line('');
58
            $this->line('Backing up files');
59
            $this->line('This may take some time');
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->line('');
71
        $this->info('Backup Completed');
72
    }
73
74
75
    protected function createBackupArchive()
76
    {
77
        $backupName = 'TB_Backup_'.Carbon::now()->format('Ymd_his').'.zip';
78
79
        //  Create a text file holding the current system version
80
        $version = new \PragmaRX\Version\Package\Version();
81
        File::put(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'version.txt', $version->version_only());
82
        $this->bar->advance();
83
84
        //  Create the archive and place the version file in it
85
        $this->zip = Zip::create(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.$backupName);
86
        $this->bar->advance();
87
        $this->zip->add(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'version.txt');
88
        $this->bar->advance();
89
        $this->zip->add(base_path().DIRECTORY_SEPARATOR.'.env');
90
        $this->bar->advance();
91
    }
92
93
    protected function closeBackupArchive()
94
    {
95
        $this->zip->close();
96
        $this->bar->advance();
97
    }
98
99
    //  Backup stored files
100
    protected function backupFiles()
101
    {
102
        //  All uploaded files
103
        $this->zip->add(config('filesystems.disks.local.root'));
104
        $this->bar->advance();
105
        //  All publicly available files
106
        $this->zip->add(config('filesystems.disks.public.root'));
107
        $this->bar->advance();
108
        //  All system logs
109
        $this->zip->add(config('filesystems.disks.logs.root'));
110
        $this->bar->advance();
111
    }
112
113
    //  Create a MYSQLDUMP of the database
114
    protected function backupDatabase()
115
    {
116
        $processStr = 'mysqldump '.
117
            config('database.connections.mysql.database').' -u '.
118
            config('database.connections.mysql.user').' -p'.
119
            config('database.connections.mysql.password');
120
        // $process = new Process(
121
        // /** @scrutinizer ignore-type */
122
        // 'mysqldump tb-dev5-data -u root');
123
        $process = new Process($processStr);
0 ignored issues
show
Bug introduced by
$processStr 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

123
        $process = new Process(/** @scrutinizer ignore-type */ $processStr);
Loading history...
124
        $process->run();
125
126
        File::put(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'database.sql', $process->getOutput());
127
        $this->bar->advance();
128
        $this->zip->add(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'database.sql');
129
        $this->bar->advance();
130
    }
131
132
    //  Remove any temporary files
133
    protected function cleanup()
134
    {
135
        $this->bar->advance();
136
        Storage::disk('backup')->delete('version.txt');
137
        Storage::disk('backup')->delete('database.sql');
138
    }
139
}
140