Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/BackupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ class BackupManager
protected $fBackupName;
protected $dBackupName;
protected $fileVerifyName = 'backup-verify';

/**
* BackupManager constructor.
*/
public function __construct()
{
$this->disk = config('backupmanager.backups.disk');
$this->backupPath = config('backupmanager.backups.backup_path') . DIRECTORY_SEPARATOR;
$this->backupSuffix = strtolower(config('backupmanager.backups.backup_file_date_suffix'));
$this->backupSuffix = strtolower(date('M-d-Y-h:i:sa'));
$this->fBackupName = "f_$this->backupSuffix.tar";
$this->dBackupName = "d_$this->backupSuffix.gz";

Expand All @@ -51,7 +50,8 @@ public function getBackups()
'size_raw' => $file['size'],
'size' => $this->formatSizeUnits($file['size']),
'type' => $file['basename'][0] === 'd' ? 'Database' : 'Files',
'date' => date('M d Y', $this->getFileTimeStamp($file))
'date' => date('M d Y', $this->getFileTimeStamp($file)),
'time' => date('h:i:sa', $this->getFileTimeStamp($file))
];
}

Expand Down Expand Up @@ -188,13 +188,14 @@ protected function backupDatabase()

$connection = [
'host' => config('database.connections.mysql.host'),
'port' => config('database.connections.mysql.port'),
'database' => config('database.connections.mysql.database'),
'username' => config('database.connections.mysql.username'),
'password' => config('database.connections.mysql.password'),
];

$tableOptions = '';
$connectionOptions = "--user={$connection['username']} --password=\"{$connection['password']}\" --host={$connection['host']} {$connection['database']} ";
$connectionOptions = "--user={$connection['username']} --password=\"{$connection['password']}\" --host={$connection['host']} --port={$connection['port']} {$connection['database']} ";

// https://mariadb.com/kb/en/library/mysqldump/
$options = [
Expand Down Expand Up @@ -276,6 +277,7 @@ protected function restoreDatabase($file)

$connection = [
'host' => config('database.connections.mysql.host'),
'port' => config('database.connections.mysql.port'),
'database' => config('database.connections.mysql.database'),
'username' => config('database.connections.mysql.username'),
'password' => config('database.connections.mysql.password'),
Expand All @@ -287,7 +289,7 @@ protected function restoreDatabase($file)
$connectionOptions .= " -p\"{$connection['password']}\" ";
}

$connectionOptions .= " -h {$connection['host']} {$connection['database']} ";
$connectionOptions .= " -h {$connection['host']} --port={$connection['port']} {$connection['database']} ";

//$command = "$cd gunzip < $this->fBackupName | mysql $connectionOptions";
$command = 'cd ' . str_replace('\\', '/',
Expand Down Expand Up @@ -396,4 +398,12 @@ protected function formatSizeUnits($size)

return number_format($size / (1024 ** $power), 2, '.', ',') . ' ' . $units[$power];
}
/**
* Set backup name
*/
public function setBackupName( $name )
{
$this->fBackupName = "f_$name.tar";
$this->dBackupName = "d_$name.gz";
}
}
2 changes: 1 addition & 1 deletion src/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
'backup_path' => 'backups',

// backup files name suffix
'backup_file_date_suffix' => date('M-d-Y'),
'backup_file_date_suffix' => date('M-d-Y-h:m:s'),

// define number of days old backup files will be deleted before new backup
'delete_old_backup_days' => 10
Expand Down
13 changes: 10 additions & 3 deletions src/Http/Controllers/BackupManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Sarfraznawaz2005\BackupManager\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Log;
use Sarfraznawaz2005\BackupManager\Facades\BackupManager;
use Session;
use Storage;
use Illuminate\Routing\Controller as BaseController;
use Sarfraznawaz2005\BackupManager\Facades\BackupManager;
use Sarfraznawaz2005\BackupManager\Http\Requests\CreateBackupRequest;


class BackupManagerController extends BaseController
{
Expand All @@ -26,10 +28,14 @@ public function index()
return view('backupmanager::index', compact('title', 'backups'));
}

public function createBackup()
public function createBackup(CreateBackupRequest $request)
{
$messages = [];

// Set name
if ($request->backupName != '')
BackupManager::setBackupName($request->backupName);

// create backups
$result = BackupManager::createBackup();

Expand Down Expand Up @@ -183,4 +189,5 @@ public function download($file)

return response()->download($file);
}

}
31 changes: 31 additions & 0 deletions src/Http/Requests/CreateBackupRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Sarfraznawaz2005\BackupManager\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateBackupRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
// Regex: no space
return [
'backupName' => ['nullable' ,'string', 'max: 25', 'alpha_dash']
];
}
}
3 changes: 2 additions & 1 deletion src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Route::group(
[
'namespace' => 'Sarfraznawaz2005\BackupManager\Http\Controllers',
'prefix' => config('backupmanager.route', 'backupmanager')
'prefix' => config('backupmanager.route', 'backupmanager'),
'middleware' => 'web'
],
function () {
// list backups
Expand Down
3 changes: 3 additions & 0 deletions src/Views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@section('header')
<form action="{{route('backupmanager_create')}}" method="post" id="frmNew">
{{ csrf_field() }}
<input type="text" name="backupName">
<button type="submit" class="btn btn-warning btn-sm"><i class="fa fa-plus"></i> Create New Backup</button>
</form>
@endsection
Expand All @@ -20,6 +21,7 @@
<th style="text-align: center;" width="1">#</th>
<th>Name</th>
<th>Date</th>
<th>Time</th>
<th>Size</th>
<th style="text-align: center;">Health</th>
<th style="text-align: center;">Type</th>
Expand All @@ -34,6 +36,7 @@
<td style="text-align: center;">{{++$index}}</td>
<td>{{$backup['name']}}</td>
<td class="date">{{$backup['date']}}</td>
<td>{{$backup['time']}}</td>
<td>{{$backup['size']}}</td>
<td style="text-align: center;">
<?php
Expand Down
6 changes: 5 additions & 1 deletion src/Views/layout/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@
</p>
@endforeach
@endif

@error('backupName')
<span class="alert alert-danger">
{{ $message }}
</span>
@enderror
@yield('content')
</div>

Expand Down