Passed
Push — dev5 ( 570ed9...d30fce )
by Ron
08:57
created

backupRestore   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 3.13%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 82
c 1
b 0
f 0
dl 0
loc 179
ccs 3
cts 96
cp 0.0313
rs 10
wmc 19

10 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanup() 0 3 1
A prepareBackup() 0 14 1
A handle() 0 39 5
A loadDatabase() 0 5 1
A checkVersion() 0 14 2
A replaceFiles() 0 19 1
A wipeDatabase() 0 7 1
A copyAllFiles() 0 8 2
A __construct() 0 3 1
A clearAllFiles() 0 14 4
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\File;
9
use Illuminate\Support\Facades\Storage;
10
11
use Zip;
12
13
class backupRestore extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'tb-backup:restore {filename} {--confirmed}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Restore application from a previous backup';
28
    protected $archive, $bar, $baseName;
29
30
    /**
31
     * Create a new command instance.
32
     *
33
     * @return void
34
     */
35 2
    public function __construct()
36
    {
37 2
        parent::__construct();
38 2
    }
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return mixed
44
     */
45
    public function handle()
46
    {
47
        if(!Storage::disk('backup')->exists(
48
        /** @scrutinizer ignore-type */
49
        $this->argument('filename')))
50
        {
51
            $this->error('The backup filename you entered does not exist.');
52
            $this->error('Exiting                                        ');
53
            return 1;
54
        }
55
56
        if(!$this->option('confirmed'))
57
        {
58
            $this->question('____________________________________________________________');
59
            $this->question('|                  IMPORTANT NOTE:                         |');
60
            $this->question('|   ALL DATA WILL BE ERASED AND REPLACED WITH THE BACKUP   |');
61
            $this->question('|__________________________________________________________|');
62
        }
63
64
        if($this->option('confirmed') || $this->confirm('Are You Sure?'))
65
        {
66
            Log::critical('Restoring Backup from filename '.$this->archive);
67
            $this->line('Restoring Backup.  Please Wait');
68
            $this->line('This could take some time');
69
            $this->bar = $this->output->createProgressBar(10);
70
            $this->call('down');
71
            $this->prepareBackup();
72
            $this->checkVersion();
73
            $this->replaceFiles();
74
            $this->wipeDatabase();
75
            $this->loadDatabase();
76
            $this->cleanup();
77
            $this->bar->finish();
78
            $this->line('');
79
            $this->call('up');
80
            $this->line('Restore Completed');
81
        }
82
83
        return 1;
84
    }
85
86
    //  Open the zip and prepare it for restore
87
    protected function prepareBackup()
88
    {
89
        $this->bar->advance();
90
        $fileParts = pathinfo(
91
            /** @scrutinizer ignore-type */
92
            $this->argument('filename'));
93
94
        $this->baseName = $fileParts['filename'];
95
        $this->archive = Zip::open(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.
96
            /** @scrutinizer ignore-type */
97
            $this->argument('filename'));
98
99
        $this->archive->extract(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.$this->baseName);
100
        $this->bar->advance();
101
    }
102
103
    protected function cleanup()
104
    {
105
        Storage::disk('backup')->deleteDirectory($this->baseName);
106
    }
107
108
    //  Check to verify that the version matches
109
    protected function checkVersion()
110
    {
111
        $backupVer = Storage::disk('backup')->get($this->baseName.DIRECTORY_SEPARATOR.'version.txt');
112
        $appVer = new \PragmaRX\Version\Package\Version();
113
        $this->bar->advance();
114
115
        if($backupVer !== $appVer->version_only())
116
        {
117
            $this->error('Unable to Restore, you are running '.$appVer->version());
118
            $this->error('This backup is for version '.$backupVer);
119
            $this->error('Please load the proper version before loading this backup');
120
121
            $this->cleanup();
122
            return 1;
123
        }
124
    }
125
126
    protected function clearAllFiles($disk)
127
    {
128
        $files = Storage::disk($disk)->files();
129
        foreach($files as $file)
130
        {
131
            if($file != '.gitignore')
132
            {
133
                Storage::disk($disk)->delete($file);
134
            }
135
        }
136
        $folders = Storage::disk($disk)->directories();
137
        foreach($folders as $folder)
138
        {
139
            Storage::disk($disk)->deleteDirectory($folder);
140
        }
141
    }
142
143
    protected function copyAllFiles($backupFolder, $disk)
144
    {
145
        $files = Storage::disk('backup')->allFiles($this->baseName.DIRECTORY_SEPARATOR.$backupFolder);
146
147
        foreach($files as $file)
148
        {
149
            $newFile = str_replace($this->baseName.'/'.$backupFolder.'/', '', $file);
150
            Storage::disk($disk)->put($newFile, Storage::disk('backup')->get($file));
151
        }
152
    }
153
154
    //  Replace the system files
155
    protected function replaceFiles()
156
    {
157
        $this->bar->advance();
158
        //  Start with the .env file
159
        $env = Storage::disk('backup')->get($this->baseName.DIRECTORY_SEPARATOR.'.env');
160
        File::put(base_path().DIRECTORY_SEPARATOR.'.env', $env);
161
        $this->bar->advance();
162
        //  Replace the log files
163
        $this->clearAllFiles('logs');
164
        $this->copyAllFiles('logs', 'logs');
165
        $this->bar->advance();
166
        //  Replace the public files
167
        $this->clearAllFiles('public');
168
        $this->copyAllFiles('public', 'public');
169
        $this->bar->advance();
170
        //  Replace all upladed files
171
        $this->clearAllFiles('local');
172
        $this->copyAllFiles('files', 'local');
173
        $this->bar->advance();
174
    }
175
176
    //  Clear out current database
177
    protected function wipeDatabase()
178
    {
179
        DB::connection(DB::getDefaultConnection())
180
            ->getSchemaBuilder()
181
            ->dropAllTables();
182
        DB::reconnect();
183
        $this->bar->advance();
184
    }
185
186
    //  Load the backed up database
187
    protected function loadDatabase()
188
    {
189
        $dbContents = Storage::disk('backup')->get($this->baseName.DIRECTORY_SEPARATOR.'database.sql');
190
        DB::unprepared($dbContents);
191
        $this->bar->advance();
192
    }
193
}
194