Last active
March 11, 2019 11:54
-
-
Save ziggybaba1/930740dbaeef928061877f427896652a to your computer and use it in GitHub Desktop.
Laravel Backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create a route | |
<? | |
Route::get('backup/create', 'BackupController@create'); | |
Route::get('backup/download/{file_name}', 'BackupController@download'); | |
Route::get('backup/delete/{file_name}', 'BackupController@delete'); | |
?> | |
<?php | |
//Create BackupController controller | |
///declare the following | |
use Artisan; | |
use Log; | |
use Storage; | |
//add the following method to BackupController class | |
public function create() | |
{ | |
try { | |
// start the backup process | |
Artisan::call('backup:run', [ '--only-db' => 1]); | |
$output = Artisan::output(); | |
// log the results | |
Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n" . $output); | |
// return the results as a response to the ajax call | |
return redirect()->back()->with(['message' => "Successful!!"]); | |
} catch (\Exception $e) { | |
\Flash::error($e->getMessage()); | |
return redirect()->back(); | |
} | |
} | |
/** | |
* Downloads a backup zip file. | |
* | |
* TODO: make it work no matter the flysystem driver (S3 Bucket, etc). | |
*/ | |
public function download($file_name) | |
{ | |
$file = config('laravel-backup.backup.name') . '/' . $file_name; | |
$disk = Storage::disk(config('laravel-backup.backup.destination.disks')[0]); | |
if ($disk->exists($file)) { | |
$fs = Storage::disk(config('laravel-backup.backup.destination.disks')[0])->getDriver(); | |
$stream = $fs->readStream($file); | |
return \Response::stream(function () use ($stream) { | |
fpassthru($stream); | |
}, 200, [ | |
"Content-Type" => $fs->getMimetype($file), | |
"Content-Length" => $fs->getSize($file), | |
"Content-disposition" => "attachment; filename=\"" . basename($file) . "\"", | |
]); | |
} else { | |
abort(404, "The backup file doesn't exist."); | |
} | |
} | |
/** | |
* Deletes a backup file. | |
*/ | |
public function delete($file_name) | |
{ | |
$disk = Storage::disk(config('laravel-backup.backup.destination.disks')[0]); | |
if ($disk->exists(config('laravel-backup.backup.name') . '/' . $file_name)) { | |
$disk->delete(config('laravel-backup.backup.name') . '/' . $file_name); | |
return redirect()->back(); | |
} else { | |
abort(404, "The backup file doesn't exist."); | |
} | |
} | |
?> | |
//Create a view blade to start backup process | |
//Add the following to route controller opening the databackup view | |
<? | |
$disk = Storage::disk(config('laravel-backup.backup.destination.disks')[0]); | |
$files = $disk->files(config('laravel-backup.backup.name')); | |
$backups = []; | |
// make an array of backup files, with their filesize and creation date | |
foreach ($files as $k => $f) { | |
// only take the zip files into account | |
if (substr($f, -4) == '.zip' && $disk->exists($f)) { | |
$backups[] = [ | |
'file_path' => $f, | |
'file_name' => str_replace(config('laravel-backup.backup.name') . '/', '', $f), | |
'file_size' => $disk->size($f), | |
'last_modified' => $disk->lastModified($f), | |
]; | |
} | |
} | |
// reverse the backups, so the newest one would be on top | |
$backups = array_reverse($backups); | |
return view('page.backup',compact('backups')); | |
?> | |
//end | |
//create helper class for humanFilesize | |
//create this document link app/Helpers/Helper.php | |
//add the following code | |
//start code | |
<?php | |
/** | |
* change plain number to formatted currency | |
* | |
* @param $number | |
* @param $currency | |
*/ | |
function humanFilesize($size, $precision = 2) { | |
$units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB'); | |
$step = 1024; | |
$i = 0; | |
while (($size / $step) > 0.9) { | |
$size = $size / $step; | |
$i++; | |
} | |
return round($size, $precision).$units[$i]; | |
} | |
//end code | |
?> | |
//add the following to "autoload" composer.json | |
"autoload": { | |
"files": [ | |
"app/Helpers/Helper.php" | |
], | |
} | |
//Ensure to dump the autoloader by using command | |
composer dump-autoload | |
//Add the following to backup.blade.php file | |
<table id="example22" class="display nowrap table table-bordered" cellspacing="0" width="100%"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>name</th> | |
<th>size</th> | |
<th>date</th> | |
<th></th> | |
</tr> | |
</thead> | |
<tfoot> | |
<tr> | |
<th>#</th> | |
<th>name</th> | |
<th>size</th> | |
<th>date</th> | |
<th></th> </tr> | |
</tfoot> | |
<tbody> | |
@forelse($backups as $backup) | |
<tr> | |
<td>{{$loop->iteration}}</td> | |
<td>{{ $backup['file_name'] }}</td> | |
<td>{{ humanFilesize($backup['file_size']) }}</td> | |
<td> | |
@php | |
$data=[]; | |
$data=explode("-",$backup['file_name']); | |
@endphp | |
{{\Carbon\Carbon::parse($data[0].'-'.$data[1].'-'.$data[2].' '.str_replace('.zip','',$data[3]))->toDayDateTimeString()}} | |
</td> | |
<td class="text-right"> | |
<a class="btn btn-default" | |
href="{{ url('backup/download/'.$backup['file_name']) }}"><i | |
class="fa fa-cloud-download"></i> @lang('admin.download')</a> | |
<a class="btn btn-danger" data-button-type="delete" | |
href="{{ url('backup/delete/'.$backup['file_name']) }}"><i class="fa fa-trash-o"></i> | |
@lang('admin.delete')</a> | |
</td> | |
</tr> | |
@empty | |
<tr> | |
<td colspan="6"></td> | |
</tr> | |
@endforelse | |
</tbody> | |
</table> | |
//end of all code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment