|
| 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 | +} |
0 commit comments