Files
nexusphp/app/Repositories/ToolRepository.php

83 lines
3.2 KiB
PHP
Raw Normal View History

2021-05-02 17:24:05 +08:00
<?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
);
2021-05-05 22:28:19 +08:00
$result = exec($command, $output, $result_code);
2021-05-02 17:24:05 +08:00
do_log(sprintf(
2021-05-05 22:28:19 +08:00
"command: %s, output: %s, result_code: %s, result: %s, filename: %s",
$command, json_encode($output), $result_code, $result, $filename
2021-05-02 17:24:05 +08:00
));
2021-05-05 22:28:19 +08:00
return compact('result_code', 'filename');
2021-05-02 17:24:05 +08:00
}
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,
);
2021-05-05 22:28:19 +08:00
$result = exec($command, $output, $result_code);
2021-05-02 17:24:05 +08:00
do_log(sprintf(
2021-05-05 22:28:19 +08:00
"command: %s, output: %s, result_code: %s, result: %s, filename: %s",
$command, json_encode($output), $result_code, $result, $filename
2021-05-02 17:24:05 +08:00
));
2021-05-05 22:28:19 +08:00
return compact('result_code', 'filename');
2021-05-02 17:24:05 +08:00
}
public function backupAll()
{
$backupWeb = $this->backupWebRoot();
2021-05-05 22:28:19 +08:00
if ($backupWeb['result_code'] != 0) {
2021-05-02 17:24:05 +08:00
throw new \RuntimeException("backup web fail: " . json_encode($backupWeb));
}
$backupDatabase = $this->backupDatabase();
2021-05-05 22:28:19 +08:00
if ($backupDatabase['result_code'] != 0) {
2021-05-02 17:24:05 +08:00
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'])
);
2021-05-05 22:28:19 +08:00
$result = exec($command, $output, $result_code);
2021-05-02 17:24:05 +08:00
do_log(sprintf(
2021-05-05 22:28:19 +08:00
"command: %s, output: %s, result_code: %s, result: %s, filename: %s",
$command, json_encode($output), $result_code, $result, $filename
2021-05-02 17:24:05 +08:00
));
2021-05-05 22:28:19 +08:00
return compact('result_code', 'filename');
2021-05-02 17:24:05 +08:00
}
}