You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.1 KiB
69 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Domains\System\Actions\Backup;
|
|
|
|
use App\Domains\System\Models\Backup;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Process;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use RuntimeException;
|
|
use ZipArchive;
|
|
|
|
class SystemRestore
|
|
{
|
|
public function __construct(protected SyncBackupCatalog $syncBackupCatalog) {}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function execute(Backup $backup): void
|
|
{
|
|
$backupPath = Storage::disk($backup->disk)->path($backup->path);
|
|
$restorePath = storage_path('app/restore-temp');
|
|
if (! file_exists($backupPath)) {
|
|
throw new Exception(__('domains/system/messages.backup.file_not_found'));
|
|
}
|
|
|
|
$zip = new ZipArchive;
|
|
if ($zip->open($backupPath) === true) {
|
|
$zip->extractTo($restorePath);
|
|
$zip->close();
|
|
|
|
$this->restoreDatabase($restorePath);
|
|
$this->restoreFiles($restorePath);
|
|
|
|
File::deleteDirectory($restorePath);
|
|
|
|
$this->syncBackupCatalog->execute();
|
|
}
|
|
}
|
|
|
|
private function restoreDatabase(string $tempPath): void
|
|
{
|
|
$dbDump = $tempPath.'/db-dumps/mysql-'.config('database.connections.mysql.database').'.sql';
|
|
if (file_exists($dbDump)) {
|
|
$restoreSql = Process::run([
|
|
'mysql',
|
|
'-u', config('database.connections.mysql.username'),
|
|
'-p'.config('database.connections.mysql.password'),
|
|
'-h'.config('database.connections.mysql.host'),
|
|
config('database.connections.mysql.database'),
|
|
'-e', "source {$dbDump}",
|
|
]);
|
|
|
|
if ($restoreSql->failed()) {
|
|
throw new RuntimeException(__('domains/system/messages.backup.restored_error').' '.$restoreSql->errorOutput());
|
|
}
|
|
}
|
|
}
|
|
|
|
private function restoreFiles(string $tempPath): void
|
|
{
|
|
$uploadsPath = $tempPath.'/storage';
|
|
|
|
if (file_exists($uploadsPath)) {
|
|
File::copyDirectory($uploadsPath, storage_path());
|
|
}
|
|
}
|
|
}
|
|
|