mirror of
https://github.com/PhyreApps/PhyrePanel.git
synced 2024-11-25 00:50:32 +00:00
Update Backup.php
This commit is contained in:
parent
0e57e4c609
commit
716a9ba1f1
1 changed files with 63 additions and 24 deletions
|
@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Jackiedo\DotenvEditor\Facades\DotenvEditor;
|
||||||
|
|
||||||
class Backup extends Model
|
class Backup extends Model
|
||||||
{
|
{
|
||||||
|
@ -47,12 +48,14 @@ class Backup extends Model
|
||||||
});
|
});
|
||||||
|
|
||||||
static::deleting(function ($model) {
|
static::deleting(function ($model) {
|
||||||
ShellApi::safeDelete($model->path,[
|
if (is_file($model->filepath)) {
|
||||||
Storage::path('backups')
|
// ShellApi::safeDelete($model->path, [
|
||||||
]);
|
// Storage::path('backups')
|
||||||
if (Storage::disk('backups')->exists($model->filepath)) {
|
// ]);
|
||||||
Storage::disk('backups')->delete($model->filepath);
|
if (Storage::disk('backups')->exists($model->filepath)) {
|
||||||
}
|
Storage::disk('backups')->delete($model->filepath);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +99,7 @@ class Backup extends Model
|
||||||
shell_exec('cd '.$tempValidatePath.' && unzip -o '.Storage::disk('backups')->path($this->filepath));
|
shell_exec('cd '.$tempValidatePath.' && unzip -o '.Storage::disk('backups')->path($this->filepath));
|
||||||
|
|
||||||
$validateDatabaseFile = $tempValidatePath.'/database.sql';
|
$validateDatabaseFile = $tempValidatePath.'/database.sql';
|
||||||
$validateEnvFile = $tempValidatePath.'/env.json';
|
$validateEnvFile = $tempValidatePath.'/.env';
|
||||||
|
|
||||||
$errorsBag = [];
|
$errorsBag = [];
|
||||||
if (! file_exists($validateDatabaseFile)) {
|
if (! file_exists($validateDatabaseFile)) {
|
||||||
|
@ -105,19 +108,19 @@ class Backup extends Model
|
||||||
if (! file_exists($validateEnvFile)) {
|
if (! file_exists($validateEnvFile)) {
|
||||||
$errorsBag[] = 'Env file not found';
|
$errorsBag[] = 'Env file not found';
|
||||||
}
|
}
|
||||||
$getEnv = Dotenv::createArrayBacked(base_path())->load();
|
if (count($errorsBag) > 0) {
|
||||||
$getEnvFromBackup = json_decode(file_get_contents($validateEnvFile), true);
|
$this->status = 'failed';
|
||||||
if (empty($getEnvFromBackup)) {
|
$this->save();
|
||||||
$errorsBag[] = 'Env file is empty';
|
return [
|
||||||
} else {
|
'status' => 'failed',
|
||||||
foreach ($getEnv as $key => $value) {
|
'message' => 'Backup failed. Database or env file missing.',
|
||||||
if (! isset($getEnvFromBackup[$key])) {
|
'errors' => $errorsBag
|
||||||
$errorsBag[] = 'Env key '.$key.' not found';
|
];
|
||||||
}
|
}
|
||||||
if ($getEnvFromBackup[$key] != $value) {
|
$originalEnvContent = file_get_contents(base_path().'/.env');
|
||||||
$errorsBag[] = 'Env key '.$key.' value mismatch';
|
$backupEnvContent = file_get_contents($validateEnvFile);
|
||||||
}
|
if ($originalEnvContent != $backupEnvContent) {
|
||||||
}
|
$errorsBag[] = 'Env file content mismatch';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($errorsBag) > 0) {
|
if (count($errorsBag) > 0) {
|
||||||
|
@ -192,7 +195,7 @@ class Backup extends Model
|
||||||
mkdir($backupTempPath);
|
mkdir($backupTempPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
$backupFilename = 'phyre-panel-'.date('Ymd-His').'.zip';
|
$backupFilename = 'phyre-backup-'.date('Ymd-His').'.zip';
|
||||||
$backupFilePath = $storagePath.'/' . $backupFilename;
|
$backupFilePath = $storagePath.'/' . $backupFilename;
|
||||||
|
|
||||||
if ($this->backup_type == 'full') {
|
if ($this->backup_type == 'full') {
|
||||||
|
@ -210,12 +213,48 @@ class Backup extends Model
|
||||||
// Export Phyre Panel database
|
// Export Phyre Panel database
|
||||||
$shellFileContent .= 'mysqldump -u "'.env('MYSQl_ROOT_USERNAME').'" -p"'.env('MYSQL_ROOT_PASSWORD').'" "'.env('DB_DATABASE').'" > '.$databaseBackupPath . PHP_EOL;
|
$shellFileContent .= 'mysqldump -u "'.env('MYSQl_ROOT_USERNAME').'" -p"'.env('MYSQL_ROOT_PASSWORD').'" "'.env('DB_DATABASE').'" > '.$databaseBackupPath . PHP_EOL;
|
||||||
|
|
||||||
|
|
||||||
// Export Phyre Panel ENV
|
// Export Phyre Panel ENV
|
||||||
$getEnv = Dotenv::createArrayBacked(base_path())->load();
|
$getEnv = Dotenv::createArrayBacked(base_path())->load();
|
||||||
file_put_contents($backupTempPath.'/env.json', json_encode($getEnv, JSON_PRETTY_PRINT));
|
$backupStructure = [
|
||||||
|
'env'=>$getEnv,
|
||||||
|
];
|
||||||
|
file_put_contents($backupTempPath.'/backup.json', json_encode($backupStructure, JSON_PRETTY_PRINT));
|
||||||
|
$shellFileContent .= 'echo "Backup Phyre Panel ENV"'. PHP_EOL;
|
||||||
|
$shellFileContent .= 'cp '.base_path().'/.env '.$backupTempPath.'/.env'. PHP_EOL;
|
||||||
|
|
||||||
$shellFileContent .= 'cd '.$backupTempPath .' && zip -r '.$backupFilePath.' ./* '. PHP_EOL;
|
// Export Phyre Panel Hosting Subscription
|
||||||
|
$findHostingSubscription = HostingSubscription::all();
|
||||||
|
if ($findHostingSubscription->count() > 0) {
|
||||||
|
foreach ($findHostingSubscription as $hostingSubscription) {
|
||||||
|
$hostingSubscriptionPath = $backupTempPath .'/hosting_subscriptions/'.$hostingSubscription->system_username;
|
||||||
|
$shellFileContent .= PHP_EOL;
|
||||||
|
$shellFileContent .= 'echo "Backup up hosting subscription: ' . $hostingSubscription->system_username .'" '. PHP_EOL;
|
||||||
|
$shellFileContent .= 'mkdir -p '.$hostingSubscriptionPath.PHP_EOL;
|
||||||
|
$shellFileContent .= 'cp -r /home/'.$hostingSubscription->system_username.'/* .[^.]* ' . $hostingSubscriptionPath .PHP_EOL;
|
||||||
|
|
||||||
|
$shellFileContent .= 'mkdir -p '.$hostingSubscriptionPath.'/databases'.PHP_EOL;
|
||||||
|
|
||||||
|
$getDatabases = Database::where('hosting_subscription_id', $hostingSubscription->id)
|
||||||
|
->where(function ($query) {
|
||||||
|
$query->where('is_remote_database_server', '0')
|
||||||
|
->orWhereNull('is_remote_database_server');
|
||||||
|
})
|
||||||
|
->get();
|
||||||
|
|
||||||
|
if ($getDatabases->count() > 0) {
|
||||||
|
foreach ($getDatabases as $database) {
|
||||||
|
$databaseName = $database->database_name_prefix . $database->database_name;
|
||||||
|
$shellFileContent .= 'echo "Backup up database: ' . $databaseName . '" ' . PHP_EOL;
|
||||||
|
$databaseBackupPath = $hostingSubscriptionPath . '/databases/' . $databaseName . '.sql';
|
||||||
|
$shellFileContent .= 'mysqldump -u "' . env('MYSQl_ROOT_USERNAME') . '" -p"' . env('MYSQL_ROOT_PASSWORD') . '" "' . $databaseName . '" > ' . $databaseBackupPath . PHP_EOL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$shellFileContent .= PHP_EOL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$shellFileContent .= 'cd '.$backupTempPath .' && zip -r '.$backupFilePath.' ./* .[^.]* '. PHP_EOL;
|
||||||
|
|
||||||
$shellFileContent .= 'rm -rf '.$backupTempPath.PHP_EOL;
|
$shellFileContent .= 'rm -rf '.$backupTempPath.PHP_EOL;
|
||||||
$shellFileContent .= 'echo "Backup complete"' . PHP_EOL;
|
$shellFileContent .= 'echo "Backup complete"' . PHP_EOL;
|
||||||
|
|
Loading…
Reference in a new issue