From 47493b1a2651e2d5c148bd58a3954eeac971aa2e Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Sun, 18 Sep 2022 17:20:51 +0800 Subject: [PATCH] backup add transfer option --- app/Console/Commands/BackupDatabase.php | 6 ++- app/Console/Commands/BackupWeb.php | 7 ++-- app/Console/Commands/Test.php | 5 +-- app/Repositories/ToolRepository.php | 52 ++++++++++++++++--------- include/constants.php | 2 +- 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/app/Console/Commands/BackupDatabase.php b/app/Console/Commands/BackupDatabase.php index 9b8825fa..3c3bb059 100644 --- a/app/Console/Commands/BackupDatabase.php +++ b/app/Console/Commands/BackupDatabase.php @@ -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); diff --git a/app/Console/Commands/BackupWeb.php b/app/Console/Commands/BackupWeb.php index fd09dbeb..097f469f 100644 --- a/app/Console/Commands/BackupWeb.php +++ b/app/Console/Commands/BackupWeb.php @@ -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); diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 52e44fa3..b84d6d05 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -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); } diff --git a/app/Repositories/ToolRepository.php b/app/Repositories/ToolRepository.php index eaf51a18..a63182ea 100644 --- a/app/Repositories/ToolRepository.php +++ b/app/Repositories/ToolRepository.php @@ -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 diff --git a/include/constants.php b/include/constants.php index 42baac68..77346b6d 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@