Skip to content

Commit 935a5a0

Browse files
committed
Added Create new Backup File and Lists Backup Files
Changed timezone to Asia/Makassar Added ManageBackupTest Added test method for create new backup file Added Backups route resource to web route file Added backup index view (inc. create new backup) Added backup nav menu and wrap content within container-fluid div Added backup lang file
1 parent 5231697 commit 935a5a0

File tree

9 files changed

+160
-22
lines changed

9 files changed

+160
-22
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use BackupManager\Filesystems\Destination;
6+
use BackupManager\Manager;
7+
use Illuminate\Http\Request;
8+
use League\Flysystem\FileExistsException;
9+
10+
class BackupsController extends Controller
11+
{
12+
public function index(Request $request)
13+
{
14+
if (!file_exists(storage_path('app/backup/db'))) {
15+
$backups = [];
16+
} else {
17+
$backups = \File::allFiles(storage_path('app/backup/db'));
18+
19+
// Sort files by modified time DESC
20+
usort($backups, function($a, $b) {
21+
return -1 * strcmp($a->getMTime(), $b->getMTime());
22+
});
23+
}
24+
25+
return view('backups.index',compact('backups'));
26+
}
27+
28+
public function store(Request $request)
29+
{
30+
$this->validate($request, [
31+
'file_name' => 'max:30|regex:/^[\w._-]+$/'
32+
]);
33+
34+
try {
35+
$manager = app()->make(Manager::class);
36+
$fileName = $request->get('file_name') ?: date('Y-m-d_Hi');
37+
38+
$manager->makeBackup()->run('mysql', [
39+
new Destination('local', 'backup/db/' . $fileName)
40+
], 'gzip');
41+
42+
// if ($fileName)
43+
// flash()->success('Backup berhasil dilakukan, nama File : ' . $fileName);
44+
45+
return redirect()->route('backups.index');
46+
} catch (FileExistsException $e) {
47+
// flash()->error('Database tidak dapat dibackup dengan Nama File yang sama.');
48+
return redirect()->route('backups.index');
49+
}
50+
}
51+
52+
}

config/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
|
6565
*/
6666

67-
'timezone' => 'UTC',
67+
'timezone' => 'Asia/Makassar',
6868

6969
/*
7070
|--------------------------------------------------------------------------

resources/lang/en/backup.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'index_title' => 'Database Backup Manager',
5+
'list' => 'Backup File List',
6+
'create' => 'Create Backup File',
7+
'file_name' => 'File Name',
8+
'file_size' => 'File Size',
9+
'created_at' => 'Created at',
10+
];
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@extends('layouts.app')
2+
3+
@section('title',trans('backup.index_title'))
4+
5+
@section('content')
6+
<h1 class="page-header">{{ trans('backup.index_title') }}</h1>
7+
<div class="row">
8+
<div class="col-md-8">
9+
<div class="panel panel-default">
10+
<div class="panel-heading"><h3 class="panel-title">{{ trans('backup.list') }}</h3></div>
11+
<table class="table table-condensed">
12+
<thead>
13+
<th>#</th>
14+
<th>{{ trans('backup.file_name') }}</th>
15+
<th>{{ trans('backup.file_size') }}</th>
16+
<th>{{ trans('backup.created_at') }}</th>
17+
</thead>
18+
<tbody>
19+
@forelse($backups as $key => $backup)
20+
<tr>
21+
<td>{{ $key + 1 }}</td>
22+
<td>{{ $backup->getFilename() }}</td>
23+
<td>{{ $backup->getSize() }} Bytes</td>
24+
<td>{{ date('Y-m-d H:i:s', $backup->getMTime()) }}</td>
25+
</tr>
26+
@empty
27+
<tr>
28+
<td colspan="3">{{ trans('backup.empty') }}</td>
29+
</tr>
30+
@endforelse
31+
</tbody>
32+
</table>
33+
</div>
34+
</div>
35+
<div class="col-md-4">
36+
<div class="panel panel-default">
37+
<div class="panel-body">
38+
<form action="{{ route('backups.store') }}" method="post">
39+
{{ csrf_field() }}
40+
<div class="form-group">
41+
<label for="file_name" class="control-label">{{ trans('backup.create') }}</label>
42+
<input type="text" name="file_name" class="form-control" placeholder="{{ date('Y-m-d_Hi') }}">
43+
</div>
44+
<div class="form-group">
45+
<input type="submit" value="{{ trans('backup.create') }}" class="btn btn-success">
46+
</div>
47+
</form>
48+
</div>
49+
</div>
50+
</div>
51+
</div>
52+
@endsection

resources/views/layouts/app.blade.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<li><a href="{{ url('/login') }}">Login</a></li>
5454
<li><a href="{{ url('/register') }}">Register</a></li>
5555
@else
56+
<li><a href="{{ route('backups.index') }}">{{ trans('backup.index_title') }}</a></li>
5657
<li class="dropdown">
5758
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
5859
{{ Auth::user()->name }} <span class="caret"></span>
@@ -77,8 +78,9 @@
7778
</div>
7879
</div>
7980
</nav>
80-
81-
@yield('content')
81+
<div class="container-fluid">
82+
@yield('content')
83+
</div>
8284
</div>
8385

8486
<!-- Scripts -->

routes/web.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
return view('welcome');
1616
});
1717

18+
Route::group(['middleware' => 'auth'], function() {
19+
Route::resource('backups','BackupsController');
20+
});
21+
1822
Auth::routes();
1923

2024
Route::get('/home', 'HomeController@index');

tests/ExampleTest.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/ManageBackupTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Illuminate\Foundation\Testing\WithoutMiddleware;
4+
use Illuminate\Foundation\Testing\DatabaseMigrations;
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
7+
class ManageBackupTest extends TestCase
8+
{
9+
use DatabaseTransactions;
10+
11+
/** @test */
12+
public function it_can_create_new_backup_file()
13+
{
14+
$this->signInAsAdmin();
15+
16+
$this->visit(route('backups.index'));
17+
$this->seePageIs(route('backups.index'));
18+
$this->type('new_backup.1231231231','file_name');
19+
$this->press(trans('backup.create'));
20+
21+
$this->seePageIs(route('backups.index'));
22+
23+
$this->assertTrue(file_exists(storage_path('app/backup/db') . '/new_backup.1231231231.gz'));
24+
unlink(storage_path('app/backup/db') . '/new_backup.1231231231.gz');
25+
$this->assertFalse(file_exists(storage_path('app/backup/db') . '/new_backup.1231231231.gz'));
26+
}
27+
}

tests/TestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use App\User;
4+
35
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
46
{
57
/**
@@ -22,4 +24,12 @@ public function createApplication()
2224

2325
return $app;
2426
}
27+
28+
protected function signInAsAdmin()
29+
{
30+
$user = factory(User::class)->create();
31+
$this->actingAs($user);
32+
33+
return $user;
34+
}
2535
}

0 commit comments

Comments
 (0)