BackupLogic.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. namespace App\Module\Cleanup\Logics;
  3. use App\Module\Cleanup\Models\CleanupBackup;
  4. use App\Module\Cleanup\Models\CleanupBackupFile;
  5. use App\Module\Cleanup\Models\CleanupPlan;
  6. use App\Module\Cleanup\Models\CleanupTask;
  7. use App\Module\Cleanup\Enums\BACKUP_TYPE;
  8. use App\Module\Cleanup\Enums\BACKUP_STATUS;
  9. use App\Module\Cleanup\Enums\COMPRESSION_TYPE;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Facades\Storage;
  13. use Illuminate\Support\Str;
  14. /**
  15. * 备份管理逻辑类
  16. *
  17. * 负责数据备份和恢复功能
  18. */
  19. class BackupLogic
  20. {
  21. /**
  22. * 为计划创建数据备份
  23. *
  24. * @param int $planId 计划ID
  25. * @param array $backupOptions 备份选项
  26. * @return array 备份结果
  27. */
  28. public static function createPlanBackup(int $planId, array $backupOptions = []): array
  29. {
  30. try {
  31. $plan = CleanupPlan::with('contents')->findOrFail($planId);
  32. // 验证备份选项
  33. $validatedOptions = static::validateBackupOptions($backupOptions);
  34. // 创建备份记录
  35. $backup = CleanupBackup::create([
  36. 'backup_name' => $validatedOptions['backup_name'] ?? "计划备份 - {$plan->plan_name}",
  37. 'plan_id' => $planId,
  38. 'backup_type' => $validatedOptions['backup_type'],
  39. 'compression_type' => $validatedOptions['compression_type'],
  40. 'status' => BACKUP_STATUS::PENDING->value,
  41. 'file_count' => 0,
  42. 'backup_size' => 0,
  43. 'created_by' => $validatedOptions['created_by'] ?? 0,
  44. ]);
  45. // 执行备份
  46. $result = static::performBackup($backup, $plan->contents->where('backup_enabled', true));
  47. return $result;
  48. } catch (\Exception $e) {
  49. Log::error('创建计划备份失败', [
  50. 'plan_id' => $planId,
  51. 'backup_options' => $backupOptions,
  52. 'error' => $e->getMessage(),
  53. 'trace' => $e->getTraceAsString()
  54. ]);
  55. return [
  56. 'success' => false,
  57. 'message' => '创建计划备份失败: ' . $e->getMessage(),
  58. 'data' => null
  59. ];
  60. }
  61. }
  62. /**
  63. * 为任务创建数据备份
  64. *
  65. * @param int $taskId 任务ID
  66. * @param array $backupOptions 备份选项
  67. * @return array 备份结果
  68. */
  69. public static function createTaskBackup(int $taskId, array $backupOptions = []): array
  70. {
  71. try {
  72. $task = CleanupTask::with('plan.contents')->findOrFail($taskId);
  73. if (!$task->plan) {
  74. throw new \Exception('任务关联的计划不存在');
  75. }
  76. // 验证备份选项
  77. $validatedOptions = static::validateBackupOptions($backupOptions);
  78. // 创建备份记录
  79. $backup = CleanupBackup::create([
  80. 'backup_name' => $validatedOptions['backup_name'] ?? "任务备份 - {$task->task_name}",
  81. 'task_id' => $taskId,
  82. 'plan_id' => $task->plan_id,
  83. 'backup_type' => $validatedOptions['backup_type'],
  84. 'compression_type' => $validatedOptions['compression_type'],
  85. 'status' => BACKUP_STATUS::PENDING->value,
  86. 'file_count' => 0,
  87. 'backup_size' => 0,
  88. 'created_by' => $validatedOptions['created_by'] ?? 0,
  89. ]);
  90. // 更新任务的备份ID
  91. $task->update(['backup_id' => $backup->id]);
  92. // 执行备份
  93. $result = static::performBackup($backup, $task->plan->contents->where('backup_enabled', true));
  94. return $result;
  95. } catch (\Exception $e) {
  96. Log::error('创建任务备份失败', [
  97. 'task_id' => $taskId,
  98. 'backup_options' => $backupOptions,
  99. 'error' => $e->getMessage(),
  100. 'trace' => $e->getTraceAsString()
  101. ]);
  102. return [
  103. 'success' => false,
  104. 'message' => '创建任务备份失败: ' . $e->getMessage(),
  105. 'data' => null
  106. ];
  107. }
  108. }
  109. /**
  110. * 执行备份操作
  111. *
  112. * @param CleanupBackup $backup 备份记录
  113. * @param \Illuminate\Support\Collection $contents 内容集合
  114. * @return array 备份结果
  115. */
  116. private static function performBackup(CleanupBackup $backup, $contents): array
  117. {
  118. $startTime = microtime(true);
  119. $totalSize = 0;
  120. $fileCount = 0;
  121. $errors = [];
  122. try {
  123. // 更新备份状态为进行中
  124. $backup->update([
  125. 'status' => BACKUP_STATUS::RUNNING->value,
  126. 'started_at' => now(),
  127. ]);
  128. $backupType = BACKUP_TYPE::from($backup->backup_type);
  129. $compressionType = COMPRESSION_TYPE::from($backup->compression_type);
  130. foreach ($contents as $content) {
  131. try {
  132. $result = static::backupTable($backup, $content->table_name, $backupType, $compressionType);
  133. if ($result['success']) {
  134. $totalSize += $result['file_size'];
  135. $fileCount++;
  136. } else {
  137. $errors[] = "表 {$content->table_name}: " . $result['message'];
  138. }
  139. } catch (\Exception $e) {
  140. $errors[] = "表 {$content->table_name}: " . $e->getMessage();
  141. Log::error("备份表失败", [
  142. 'backup_id' => $backup->id,
  143. 'table_name' => $content->table_name,
  144. 'error' => $e->getMessage()
  145. ]);
  146. }
  147. }
  148. $executionTime = round(microtime(true) - $startTime, 3);
  149. // 更新备份完成状态
  150. $finalStatus = empty($errors) ? BACKUP_STATUS::COMPLETED : BACKUP_STATUS::FAILED;
  151. $backup->update([
  152. 'status' => $finalStatus->value,
  153. 'file_count' => $fileCount,
  154. 'backup_size' => $totalSize,
  155. 'execution_time' => $executionTime,
  156. 'completed_at' => now(),
  157. 'error_message' => empty($errors) ? null : implode('; ', $errors),
  158. ]);
  159. return [
  160. 'success' => $finalStatus === BACKUP_STATUS::COMPLETED,
  161. 'message' => $finalStatus === BACKUP_STATUS::COMPLETED ? '备份创建成功' : '备份创建完成,但有错误',
  162. 'data' => [
  163. 'backup_id' => $backup->id,
  164. 'backup_name' => $backup->backup_name,
  165. 'file_count' => $fileCount,
  166. 'backup_size' => $totalSize,
  167. 'execution_time' => $executionTime,
  168. 'errors' => $errors,
  169. ]
  170. ];
  171. } catch (\Exception $e) {
  172. // 更新备份失败状态
  173. $backup->update([
  174. 'status' => BACKUP_STATUS::FAILED->value,
  175. 'execution_time' => round(microtime(true) - $startTime, 3),
  176. 'completed_at' => now(),
  177. 'error_message' => $e->getMessage(),
  178. ]);
  179. throw $e;
  180. }
  181. }
  182. /**
  183. * 备份单个表
  184. *
  185. * @param CleanupBackup $backup 备份记录
  186. * @param string $tableName 表名
  187. * @param BACKUP_TYPE $backupType 备份类型
  188. * @param COMPRESSION_TYPE $compressionType 压缩类型
  189. * @return array 备份结果
  190. */
  191. private static function backupTable(CleanupBackup $backup, string $tableName, BACKUP_TYPE $backupType, COMPRESSION_TYPE $compressionType): array
  192. {
  193. try {
  194. // 生成备份文件名
  195. $fileName = static::generateBackupFileName($backup, $tableName, $backupType);
  196. $filePath = "cleanup/backups/{$backup->id}/{$fileName}";
  197. // 根据备份类型执行备份
  198. $content = match ($backupType) {
  199. BACKUP_TYPE::SQL => static::exportTableToSQL($tableName),
  200. BACKUP_TYPE::JSON => static::exportTableToJSON($tableName),
  201. BACKUP_TYPE::CSV => static::exportTableToCSV($tableName),
  202. };
  203. // 压缩内容(如果需要)
  204. if ($compressionType !== COMPRESSION_TYPE::NONE) {
  205. $content = static::compressContent($content, $compressionType);
  206. $fileName .= static::getCompressionExtension($compressionType);
  207. $filePath .= static::getCompressionExtension($compressionType);
  208. }
  209. // 保存文件
  210. Storage::disk('local')->put($filePath, $content);
  211. $fileSize = Storage::disk('local')->size($filePath);
  212. // 计算文件哈希
  213. $fileHash = hash('sha256', $content);
  214. // 记录备份文件
  215. CleanupBackupFile::create([
  216. 'backup_id' => $backup->id,
  217. 'table_name' => $tableName,
  218. 'file_name' => $fileName,
  219. 'file_path' => $filePath,
  220. 'file_size' => $fileSize,
  221. 'file_hash' => $fileHash,
  222. 'backup_type' => $backupType->value,
  223. 'compression_type' => $compressionType->value,
  224. ]);
  225. return [
  226. 'success' => true,
  227. 'message' => "表 {$tableName} 备份成功",
  228. 'file_size' => $fileSize,
  229. 'file_path' => $filePath,
  230. ];
  231. } catch (\Exception $e) {
  232. return [
  233. 'success' => false,
  234. 'message' => $e->getMessage(),
  235. 'file_size' => 0,
  236. ];
  237. }
  238. }
  239. /**
  240. * 导出表为SQL格式
  241. *
  242. * @param string $tableName 表名
  243. * @return string SQL内容
  244. */
  245. private static function exportTableToSQL(string $tableName): string
  246. {
  247. $sql = "-- 表 {$tableName} 的数据备份\n";
  248. $sql .= "-- 备份时间: " . now()->toDateTimeString() . "\n\n";
  249. // 获取表结构
  250. $createTable = DB::select("SHOW CREATE TABLE `{$tableName}`")[0];
  251. $sql .= $createTable->{'Create Table'} . ";\n\n";
  252. // 获取表数据
  253. $records = DB::table($tableName)->get();
  254. if ($records->isNotEmpty()) {
  255. $sql .= "-- 数据插入\n";
  256. $sql .= "INSERT INTO `{$tableName}` VALUES\n";
  257. $values = [];
  258. foreach ($records as $record) {
  259. $recordArray = (array) $record;
  260. $escapedValues = array_map(function ($value) {
  261. return $value === null ? 'NULL' : "'" . addslashes($value) . "'";
  262. }, $recordArray);
  263. $values[] = '(' . implode(', ', $escapedValues) . ')';
  264. }
  265. $sql .= implode(",\n", $values) . ";\n";
  266. }
  267. return $sql;
  268. }
  269. /**
  270. * 导出表为JSON格式
  271. *
  272. * @param string $tableName 表名
  273. * @return string JSON内容
  274. */
  275. private static function exportTableToJSON(string $tableName): string
  276. {
  277. $data = [
  278. 'table_name' => $tableName,
  279. 'backup_time' => now()->toDateTimeString(),
  280. 'records' => DB::table($tableName)->get()->toArray(),
  281. ];
  282. return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  283. }
  284. /**
  285. * 导出表为CSV格式
  286. *
  287. * @param string $tableName 表名
  288. * @return string CSV内容
  289. */
  290. private static function exportTableToCSV(string $tableName): string
  291. {
  292. $records = DB::table($tableName)->get();
  293. if ($records->isEmpty()) {
  294. return '';
  295. }
  296. $csv = '';
  297. $headers = array_keys((array) $records->first());
  298. $csv .= implode(',', $headers) . "\n";
  299. foreach ($records as $record) {
  300. $recordArray = (array) $record;
  301. $escapedValues = array_map(function ($value) {
  302. return '"' . str_replace('"', '""', $value ?? '') . '"';
  303. }, $recordArray);
  304. $csv .= implode(',', $escapedValues) . "\n";
  305. }
  306. return $csv;
  307. }
  308. /**
  309. * 压缩内容
  310. *
  311. * @param string $content 原始内容
  312. * @param COMPRESSION_TYPE $compressionType 压缩类型
  313. * @return string 压缩后的内容
  314. */
  315. private static function compressContent(string $content, COMPRESSION_TYPE $compressionType): string
  316. {
  317. return match ($compressionType) {
  318. COMPRESSION_TYPE::GZIP => gzencode($content),
  319. COMPRESSION_TYPE::ZIP => static::createZipContent($content),
  320. default => $content,
  321. };
  322. }
  323. /**
  324. * 创建ZIP内容
  325. *
  326. * @param string $content 原始内容
  327. * @return string ZIP内容
  328. */
  329. private static function createZipContent(string $content): string
  330. {
  331. $zip = new \ZipArchive();
  332. $tempFile = tempnam(sys_get_temp_dir(), 'backup_');
  333. if ($zip->open($tempFile, \ZipArchive::CREATE) === TRUE) {
  334. $zip->addFromString('data.sql', $content);
  335. $zip->close();
  336. $zipContent = file_get_contents($tempFile);
  337. unlink($tempFile);
  338. return $zipContent;
  339. }
  340. throw new \Exception('创建ZIP文件失败');
  341. }
  342. /**
  343. * 获取压缩扩展名
  344. *
  345. * @param COMPRESSION_TYPE $compressionType 压缩类型
  346. * @return string 扩展名
  347. */
  348. private static function getCompressionExtension(COMPRESSION_TYPE $compressionType): string
  349. {
  350. return match ($compressionType) {
  351. COMPRESSION_TYPE::GZIP => '.gz',
  352. COMPRESSION_TYPE::ZIP => '.zip',
  353. default => '',
  354. };
  355. }
  356. /**
  357. * 生成备份文件名
  358. *
  359. * @param CleanupBackup $backup 备份记录
  360. * @param string $tableName 表名
  361. * @param BACKUP_TYPE $backupType 备份类型
  362. * @return string 文件名
  363. */
  364. private static function generateBackupFileName(CleanupBackup $backup, string $tableName, BACKUP_TYPE $backupType): string
  365. {
  366. $timestamp = now()->format('Y-m-d_H-i-s');
  367. $extension = match ($backupType) {
  368. BACKUP_TYPE::SQL => '.sql',
  369. BACKUP_TYPE::JSON => '.json',
  370. BACKUP_TYPE::CSV => '.csv',
  371. };
  372. return "{$tableName}_{$timestamp}{$extension}";
  373. }
  374. /**
  375. * 验证备份选项
  376. *
  377. * @param array $backupOptions 备份选项
  378. * @return array 验证后的选项
  379. */
  380. private static function validateBackupOptions(array $backupOptions): array
  381. {
  382. return [
  383. 'backup_name' => $backupOptions['backup_name'] ?? null,
  384. 'backup_type' => $backupOptions['backup_type'] ?? BACKUP_TYPE::SQL->value,
  385. 'compression_type' => $backupOptions['compression_type'] ?? COMPRESSION_TYPE::GZIP->value,
  386. 'created_by' => $backupOptions['created_by'] ?? 0,
  387. ];
  388. }
  389. /**
  390. * 清理过期备份
  391. *
  392. * @param int $retentionDays 保留天数
  393. * @return array 清理结果
  394. */
  395. public static function cleanExpiredBackups(int $retentionDays = 30): array
  396. {
  397. try {
  398. $expiredDate = now()->subDays($retentionDays);
  399. // 获取过期的备份
  400. $expiredBackups = CleanupBackup::where('created_at', '<', $expiredDate)
  401. ->where('status', BACKUP_STATUS::COMPLETED->value)
  402. ->get();
  403. $deletedCount = 0;
  404. $freedSpace = 0;
  405. $errors = [];
  406. foreach ($expiredBackups as $backup) {
  407. try {
  408. $result = static::deleteBackup($backup->id);
  409. if ($result['success']) {
  410. $deletedCount++;
  411. $freedSpace += $backup->backup_size;
  412. } else {
  413. $errors[] = "备份 {$backup->id}: " . $result['message'];
  414. }
  415. } catch (\Exception $e) {
  416. $errors[] = "备份 {$backup->id}: " . $e->getMessage();
  417. }
  418. }
  419. return [
  420. 'success' => true,
  421. 'message' => "清理完成,删除 {$deletedCount} 个过期备份",
  422. 'data' => [
  423. 'deleted_count' => $deletedCount,
  424. 'total_expired' => $expiredBackups->count(),
  425. 'freed_space' => $freedSpace,
  426. 'errors' => $errors,
  427. ]
  428. ];
  429. } catch (\Exception $e) {
  430. Log::error('清理过期备份失败', [
  431. 'retention_days' => $retentionDays,
  432. 'error' => $e->getMessage()
  433. ]);
  434. return [
  435. 'success' => false,
  436. 'message' => '清理过期备份失败: ' . $e->getMessage(),
  437. 'data' => null
  438. ];
  439. }
  440. }
  441. /**
  442. * 删除备份
  443. *
  444. * @param int $backupId 备份ID
  445. * @return array 删除结果
  446. */
  447. public static function deleteBackup(int $backupId): array
  448. {
  449. try {
  450. DB::beginTransaction();
  451. $backup = CleanupBackup::with('files')->findOrFail($backupId);
  452. // 删除备份文件
  453. foreach ($backup->files as $file) {
  454. try {
  455. if (Storage::disk('local')->exists($file->file_path)) {
  456. Storage::disk('local')->delete($file->file_path);
  457. }
  458. } catch (\Exception $e) {
  459. Log::warning('删除备份文件失败', [
  460. 'file_path' => $file->file_path,
  461. 'error' => $e->getMessage()
  462. ]);
  463. }
  464. }
  465. // 删除备份文件记录
  466. $backup->files()->delete();
  467. // 删除备份记录
  468. $backup->delete();
  469. DB::commit();
  470. return [
  471. 'success' => true,
  472. 'message' => '备份删除成功',
  473. 'data' => [
  474. 'backup_id' => $backupId,
  475. 'deleted_files' => $backup->files->count(),
  476. ]
  477. ];
  478. } catch (\Exception $e) {
  479. DB::rollBack();
  480. Log::error('删除备份失败', [
  481. 'backup_id' => $backupId,
  482. 'error' => $e->getMessage()
  483. ]);
  484. return [
  485. 'success' => false,
  486. 'message' => '删除备份失败: ' . $e->getMessage(),
  487. 'data' => null
  488. ];
  489. }
  490. }
  491. /**
  492. * 获取备份详情
  493. *
  494. * @param int $backupId 备份ID
  495. * @return array 备份详情
  496. */
  497. public static function getBackupDetails(int $backupId): array
  498. {
  499. try {
  500. $backup = CleanupBackup::with(['files', 'plan', 'task'])->findOrFail($backupId);
  501. return [
  502. 'success' => true,
  503. 'data' => [
  504. 'backup' => [
  505. 'id' => $backup->id,
  506. 'backup_name' => $backup->backup_name,
  507. 'backup_type' => $backup->backup_type,
  508. 'backup_type_name' => BACKUP_TYPE::from($backup->backup_type)->getDescription(),
  509. 'compression_type' => $backup->compression_type,
  510. 'compression_type_name' => COMPRESSION_TYPE::from($backup->compression_type)->getDescription(),
  511. 'status' => $backup->status,
  512. 'status_name' => BACKUP_STATUS::from($backup->status)->getDescription(),
  513. 'file_count' => $backup->file_count,
  514. 'backup_size' => $backup->backup_size,
  515. 'execution_time' => $backup->execution_time,
  516. 'started_at' => $backup->started_at,
  517. 'completed_at' => $backup->completed_at,
  518. 'error_message' => $backup->error_message,
  519. 'created_at' => $backup->created_at,
  520. ],
  521. 'files' => $backup->files->map(function ($file) {
  522. return [
  523. 'id' => $file->id,
  524. 'table_name' => $file->table_name,
  525. 'file_name' => $file->file_name,
  526. 'file_size' => $file->file_size,
  527. 'file_hash' => $file->file_hash,
  528. 'backup_type' => $file->backup_type,
  529. 'compression_type' => $file->compression_type,
  530. ];
  531. }),
  532. 'plan' => $backup->plan ? [
  533. 'id' => $backup->plan->id,
  534. 'plan_name' => $backup->plan->plan_name,
  535. ] : null,
  536. 'task' => $backup->task ? [
  537. 'id' => $backup->task->id,
  538. 'task_name' => $backup->task->task_name,
  539. ] : null,
  540. ]
  541. ];
  542. } catch (\Exception $e) {
  543. Log::error('获取备份详情失败', [
  544. 'backup_id' => $backupId,
  545. 'error' => $e->getMessage()
  546. ]);
  547. return [
  548. 'success' => false,
  549. 'message' => '获取备份详情失败: ' . $e->getMessage(),
  550. 'data' => null
  551. ];
  552. }
  553. }
  554. /**
  555. * 验证备份完整性
  556. *
  557. * @param int $backupId 备份ID
  558. * @return array 验证结果
  559. */
  560. public static function verifyBackupIntegrity(int $backupId): array
  561. {
  562. try {
  563. $backup = CleanupBackup::with('files')->findOrFail($backupId);
  564. $verifiedFiles = 0;
  565. $corruptedFiles = 0;
  566. $missingFiles = 0;
  567. $errors = [];
  568. foreach ($backup->files as $file) {
  569. try {
  570. if (!Storage::disk('local')->exists($file->file_path)) {
  571. $missingFiles++;
  572. $errors[] = "文件缺失: {$file->file_name}";
  573. continue;
  574. }
  575. $content = Storage::disk('local')->get($file->file_path);
  576. $currentHash = hash('sha256', $content);
  577. if ($currentHash === $file->file_hash) {
  578. $verifiedFiles++;
  579. } else {
  580. $corruptedFiles++;
  581. $errors[] = "文件损坏: {$file->file_name}";
  582. }
  583. } catch (\Exception $e) {
  584. $errors[] = "验证失败: {$file->file_name} - " . $e->getMessage();
  585. }
  586. }
  587. $isIntact = $corruptedFiles === 0 && $missingFiles === 0;
  588. return [
  589. 'success' => true,
  590. 'data' => [
  591. 'backup_id' => $backupId,
  592. 'is_intact' => $isIntact,
  593. 'total_files' => $backup->files->count(),
  594. 'verified_files' => $verifiedFiles,
  595. 'corrupted_files' => $corruptedFiles,
  596. 'missing_files' => $missingFiles,
  597. 'errors' => $errors,
  598. ]
  599. ];
  600. } catch (\Exception $e) {
  601. Log::error('验证备份完整性失败', [
  602. 'backup_id' => $backupId,
  603. 'error' => $e->getMessage()
  604. ]);
  605. return [
  606. 'success' => false,
  607. 'message' => '验证备份完整性失败: ' . $e->getMessage(),
  608. 'data' => null
  609. ];
  610. }
  611. }
  612. }