mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-03 14:10:57 +08:00
backup add transfer option
This commit is contained in:
@@ -12,7 +12,7 @@ class BackupDatabase extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'backup:database';
|
||||
protected $signature = 'backup:database {--transfer=}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
@@ -39,7 +39,9 @@ class BackupDatabase extends Command
|
||||
public function handle()
|
||||
{
|
||||
$rep = new ToolRepository();
|
||||
$result = $rep->backupDatabase();
|
||||
$transfer = $this->option('transfer');
|
||||
$this->info("transfer: $transfer");
|
||||
$result = $rep->backupDatabase($transfer);
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
@@ -12,7 +12,7 @@ class BackupWeb extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'backup:web {--method=}';
|
||||
protected $signature = 'backup:web {--method=} {--transfer=}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
@@ -39,9 +39,10 @@ class BackupWeb extends Command
|
||||
public function handle()
|
||||
{
|
||||
$method = $this->option('method');
|
||||
$this->info("method: $method");
|
||||
$transfer = $this->option('transfer');
|
||||
$this->info("method: $method, transfer: $transfer");
|
||||
$rep = new ToolRepository();
|
||||
$result = $rep->backupWeb($method);
|
||||
$result = $rep->backupWeb($method, $transfer);
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
@@ -88,9 +88,8 @@ class Test extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$rep = new PluginRepository();
|
||||
// $rep->installCronjob();
|
||||
$r = $rep->getInstalledVersion('xiaomlove/nexusphp-post-like');
|
||||
$rep = new ToolRepository();
|
||||
$r = $rep->transfer('C:\Users\CHENYU~1\AppData\Local\Temp/nexusphp.v1.5.beta5.20120707.web.20220918.053953.zip', 0);
|
||||
dd($r);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ class ToolRepository extends BaseRepository
|
||||
{
|
||||
const BACKUP_EXCLUDES = ['vendor', 'node_modules', '.git', '.idea', '.settings', '.DS_Store', '.github'];
|
||||
|
||||
public function backupWeb($method = null): array
|
||||
public function backupWeb($method = null, $transfer = false): array
|
||||
{
|
||||
$webRoot = base_path();
|
||||
$dirName = basename($webRoot);
|
||||
@@ -76,10 +76,13 @@ class ToolRepository extends BaseRepository
|
||||
$result_code = 0;
|
||||
do_log("No tar command, use zip.");
|
||||
}
|
||||
return compact('result_code', 'filename');
|
||||
if (!$transfer) {
|
||||
return compact('result_code', 'filename');
|
||||
}
|
||||
return $this->transfer($filename, $result_code);
|
||||
}
|
||||
|
||||
public function backupDatabase(): array
|
||||
public function backupDatabase($transfer = false): array
|
||||
{
|
||||
$connectionName = config('database.default');
|
||||
$config = config("database.connections.$connectionName");
|
||||
@@ -93,10 +96,13 @@ class ToolRepository extends BaseRepository
|
||||
"command: %s, output: %s, result_code: %s, result: %s, filename: %s",
|
||||
$command, json_encode($output), $result_code, $result, $filename
|
||||
));
|
||||
return compact('result_code', 'filename');
|
||||
if (!$transfer) {
|
||||
return compact('result_code', 'filename');
|
||||
}
|
||||
return $this->transfer($filename, $result_code);
|
||||
}
|
||||
|
||||
public function backupAll($method = null): array
|
||||
public function backupAll($method = null, $transfer = false): array
|
||||
{
|
||||
$backupWeb = $this->backupWeb($method);
|
||||
if ($backupWeb['result_code'] != 0) {
|
||||
@@ -134,8 +140,10 @@ class ToolRepository extends BaseRepository
|
||||
$result_code = 0;
|
||||
do_log("No tar command, use zip.");
|
||||
}
|
||||
return compact('result_code', 'filename');
|
||||
|
||||
if (!$transfer) {
|
||||
return compact('result_code', 'filename');
|
||||
}
|
||||
return $this->transfer($filename, $result_code);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,27 +186,33 @@ class ToolRepository extends BaseRepository
|
||||
}
|
||||
$backupResult = $this->backupAll();
|
||||
do_log("Backup all result: " . json_encode($backupResult));
|
||||
if ($backupResult['result_code'] != 0) {
|
||||
throw new \RuntimeException("Backup all fail.");
|
||||
}
|
||||
$filename = $backupResult['filename'];
|
||||
$transferResult = $this->transfer($backupResult['filename'], $backupResult['result_code'], $setting);
|
||||
$backupResult['transfer_result'] = $transferResult;
|
||||
do_log("[BACKUP_ALL_DONE]: " . json_encode($backupResult));
|
||||
return $backupResult;
|
||||
}
|
||||
|
||||
public function transfer($filename, $result_code, $setting = null): array
|
||||
{
|
||||
if ($result_code != 0) {
|
||||
throw new \RuntimeException("file: $filename backup fail!");
|
||||
}
|
||||
$result = compact('filename', 'result_code');
|
||||
if (empty($setting)) {
|
||||
$setting = Setting::get('backup');
|
||||
}
|
||||
$saveResult = $this->saveToGoogleDrive($setting, $filename);
|
||||
do_log("[BACKUP_GOOGLE_DRIVE]: $saveResult");
|
||||
$backupResult['google_drive'] = $saveResult;
|
||||
$result['google_drive'] = $saveResult;
|
||||
|
||||
$saveResult = $this->saveToFtp($setting, $filename);
|
||||
do_log("[BACKUP_FTP]: $saveResult");
|
||||
$backupResult['ftp'] = $saveResult;
|
||||
$result['ftp'] = $saveResult;
|
||||
|
||||
$saveResult = $this->saveToSftp($setting, $filename);
|
||||
do_log("[BACKUP_SFTP]: $saveResult");
|
||||
$backupResult['sftp'] = $saveResult;
|
||||
|
||||
do_log("[BACKUP_ALL_DONE]: " . json_encode($backupResult));
|
||||
|
||||
return $backupResult;
|
||||
|
||||
$result['sftp'] = $saveResult;
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function saveToGoogleDrive(array $setting, $filename): bool|string
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.7.27');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-09-17');
|
||||
defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-09-18');
|
||||
defined('IN_TRACKER') || define('IN_TRACKER', false);
|
||||
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
|
||||
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
|
||||
|
||||
Reference in New Issue
Block a user