mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 11:27:24 +08:00
add backup and admin dist
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ToolRepository extends BaseRepository
|
||||
{
|
||||
public function getSystemInfo()
|
||||
{
|
||||
$systemInfo = [
|
||||
'nexus_version' => config('app.nexus_version'),
|
||||
'laravel_version' => \Illuminate\Foundation\Application::VERSION,
|
||||
'php_version' => PHP_VERSION,
|
||||
'mysql_version' => DB::select(DB::raw('select version() as info'))[0]->info,
|
||||
'os' => PHP_OS,
|
||||
'server_software' => $_SERVER['SERVER_SOFTWARE'],
|
||||
|
||||
];
|
||||
|
||||
return $systemInfo;
|
||||
}
|
||||
|
||||
public function backupWebRoot()
|
||||
{
|
||||
$webRoot = base_path();
|
||||
$dirName = basename($webRoot);
|
||||
$filename = sprintf('%s/%s.%s.tar.gz', sys_get_temp_dir(), $dirName, date('Ymd.His'));
|
||||
$command = sprintf(
|
||||
'tar --exclude=vendor --exclude=.git -czf %s -C %s %s',
|
||||
$filename, dirname($webRoot), $dirName
|
||||
);
|
||||
$result = exec($command, $output, $resultCode);
|
||||
do_log(sprintf(
|
||||
"command: %s, output: %s, resultCode: %s, result: %s, filename: %s",
|
||||
$command, json_encode($output), $resultCode, $result, $filename
|
||||
));
|
||||
return compact('result', 'filename');
|
||||
}
|
||||
|
||||
public function backupDatabase()
|
||||
{
|
||||
$connectionName = config('database.default');
|
||||
$config = config("database.connections.$connectionName");
|
||||
$filename = sprintf('%s/%s.database.%s.sql', sys_get_temp_dir(), basename(base_path()), date('Ymd.His'));
|
||||
$command = sprintf(
|
||||
'mysqldump --user=%s --password=%s --port=%s --single-transaction --databases %s >> %s',
|
||||
$config['username'], $config['password'], $config['port'], $config['database'], $filename,
|
||||
);
|
||||
$result = exec($command, $output, $resultCode);
|
||||
do_log(sprintf(
|
||||
"command: %s, output: %s, resultCode: %s, result: %s, filename: %s",
|
||||
$command, json_encode($output), $resultCode, $result, $filename
|
||||
));
|
||||
return compact('result', 'filename');
|
||||
}
|
||||
|
||||
public function backupAll()
|
||||
{
|
||||
$backupWeb = $this->backupWebRoot();
|
||||
if ($backupWeb['result'] != 0) {
|
||||
throw new \RuntimeException("backup web fail: " . json_encode($backupWeb));
|
||||
}
|
||||
$backupDatabase = $this->backupDatabase();
|
||||
if ($backupDatabase['result'] != 0) {
|
||||
throw new \RuntimeException("backup database fail: " . json_encode($backupDatabase));
|
||||
}
|
||||
$filename = sprintf('%s/%s.%s.tar.gz', sys_get_temp_dir(), basename(base_path()), date('Ymd.His'));
|
||||
$command = sprintf(
|
||||
'tar -czf %s -C %s %s -C %s %s',
|
||||
$filename,
|
||||
dirname($backupWeb['filename']), basename($backupWeb['filename']),
|
||||
dirname($backupDatabase['filename']), basename($backupDatabase['filename'])
|
||||
);
|
||||
$result = exec($command, $output, $resultCode);
|
||||
do_log(sprintf(
|
||||
"command: %s, output: %s, resultCode: %s, result: %s, filename: %s",
|
||||
$command, json_encode($output), $resultCode, $result, $filename
|
||||
));
|
||||
return compact('result', 'filename');
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user