-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from AndresC1/feature/backup
feat: Integracion de backup para la BD
- Loading branch information
Showing
8 changed files
with
234 additions
and
69 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\Backup\ImportBackupRequest; | ||
use App\Models\Backup; | ||
use Illuminate\Http\Request; | ||
|
||
class BackupController extends Controller | ||
{ | ||
function exportBackup() | ||
{ | ||
$host = env('DB_HOST'); | ||
$port = env('DB_PORT'); | ||
$database = env('DB_DATABASE'); | ||
$user = env('DB_USERNAME'); | ||
$password = env('DB_PASSWORD'); | ||
|
||
$backupName = 'backup_' . now('America/Managua')->format('Y-m-d_His') . '.sql'; | ||
$backupPath = storage_path('app/backup/' . $backupName); | ||
|
||
$command = "mysqldump -h {$host} -P {$port} -u {$user} -p{$password} {$database} > {$backupPath}"; | ||
exec($command); | ||
|
||
$fileBackup = Backup::create([ | ||
'name' => $backupName, | ||
'path' => $backupPath, | ||
'disk' => 'local', | ||
'size' => round(filesize($backupPath) / 1024 / 1024, 2)."MB", | ||
'type' => 'sql', | ||
'created_at' => now('America/Managua')->format('Y-m-d H:i:s'), | ||
'updated_at' => now('America/Managua')->format('Y-m-d H:i:s') | ||
]); | ||
exec($command); | ||
|
||
|
||
return response()->json([ | ||
'backup' => $fileBackup, | ||
'message' => 'Backup created successfully', | ||
'status' => 200 | ||
], 200); | ||
} | ||
|
||
function downloadBackup(ImportBackupRequest $request) | ||
{ | ||
$request->validated(); | ||
$backup = Backup::find($request->backup_id); | ||
|
||
if($backup->deleted_at != null){ | ||
return response()->json([ | ||
'message' => 'Backup not found', | ||
'status' => 400 | ||
], 400); | ||
} | ||
|
||
return response()->download($backup->path); | ||
} | ||
|
||
function listBackups() | ||
{ | ||
$backups = Backup::Where('deleted_at', null)->get(); | ||
|
||
return response()->json([ | ||
'backups' => $backups, | ||
'message' => 'Backups obtained successfully', | ||
'status' => 200 | ||
], 200); | ||
} | ||
|
||
function deleteBackup(ImportBackupRequest $request) | ||
{ | ||
$request->validated(); | ||
$backup = Backup::find($request->backup_id); | ||
|
||
if($backup->deleted_at == null){ | ||
$backup->deleted_at = now('America/Managua')->format('Y-m-d H:i:s'); | ||
$backup->deleted_by = auth()->user()->id; | ||
$backup->save(); | ||
} | ||
|
||
return response()->json([ | ||
'message' => 'Backup deleted successfully', | ||
'status' => 200 | ||
], 200); | ||
} | ||
|
||
function restoreBackup(ImportBackupRequest $request) | ||
{ | ||
$request->validated(); | ||
$backup = Backup::find($request->backup_id); | ||
|
||
if($backup->deleted_at != null){ | ||
return response()->json([ | ||
'message' => 'Backup not found', | ||
'status' => 400 | ||
], 400); | ||
} | ||
|
||
$host = env('DB_HOST'); | ||
$port = env('DB_PORT'); | ||
$database = env('DB_DATABASE'); | ||
$user = env('DB_USERNAME'); | ||
$password = env('DB_PASSWORD'); | ||
|
||
$command = "mysql -h {$host} -P {$port} -u {$user} -p{$password} {$database} < {$backup->path}"; | ||
|
||
exec($command); | ||
|
||
return response()->json([ | ||
'message' => 'Backup restored successfully', | ||
'status' => 200 | ||
], 200); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Backup; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class ImportBackupRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"backup_id" => ["required", "exists:backups,id"], | ||
]; | ||
} | ||
|
||
/** | ||
* Get the error messages for the defined validation rules. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
|
||
public function messages(): array | ||
{ | ||
return [ | ||
"backup_id.required" => "Backup ID is required", | ||
"backup_id.exists" => "Backup not found", | ||
]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Backup extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'name', | ||
'path', | ||
'disk', | ||
'size', | ||
'type', | ||
'deleted_at', | ||
'deleted_by', | ||
'created_at', | ||
'updated_at' | ||
]; | ||
|
||
public function deletedBy(){ | ||
return $this->belongsTo(User::class, 'deleted_by'); | ||
} | ||
} |
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
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
34 changes: 34 additions & 0 deletions
34
database/migrations/2024_07_18_004646_create_backups_table.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('backups', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->string('path'); | ||
$table->string('disk'); | ||
$table->string('size'); | ||
$table->string('type'); | ||
$table->timestamp('deleted_at')->nullable(); | ||
$table->foreignId('deleted_by')->nullable()->constrained('users')->cascadeOnDelete()->cascadeOnUpdate(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('backups'); | ||
} | ||
}; |
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