BackupDb.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Storage;
  5. use Symfony\Component\Process\Process;
  6. class BackupDb extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'tty:backupDb {--I|import= : 导入数据库}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '备份数据库';
  20. protected $process;
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $import = $this->option('import');
  38. //设置超时时间为0,表示一直执行。当php在safe mode模式下无效,此时可能会导致导入超时,此时需要分段导入
  39. set_time_limit(0);
  40. if ($import) {
  41. $this->info('正在执行恢复操作');
  42. $this->importData($import);
  43. } else {
  44. $this->info('正在执行备份操作');
  45. $this->exportData();
  46. }
  47. return 0;
  48. }
  49. private function exportData()
  50. {
  51. $DB_HOST = getenv('DB_HOST');
  52. $DB_DATABASE = getenv('DB_DATABASE');
  53. $DB_USERNAME = getenv('DB_USERNAME');
  54. $DB_PASSWORD = getenv('DB_PASSWORD');
  55. if (!Storage::exists('db_backup')) {
  56. Storage::makeDirectory('db_backup');
  57. }
  58. $dumpFilename = storage_path('app/db_backup/' . date("Y-m-d_H-i-s") . ".sql");
  59. $command = "mysqldump --host=" . $DB_HOST . " --user=" . $DB_USERNAME . " --password=" . $DB_PASSWORD . " --databases " . $DB_DATABASE . " > " . $dumpFilename;
  60. $process = Process::fromShellCommandline($command, null, null, null, null);
  61. $process->start();
  62. $process->wait();
  63. $this->info('备份成功!');
  64. // 保留最近的30天数据
  65. $fileArr = Storage::allFiles('db_backup');
  66. sort($fileArr);
  67. $limit = 30;
  68. if (count($fileArr) > $limit) {
  69. for ($i = 0; $i < count($fileArr) - $limit; $i++) {
  70. Storage::delete($fileArr[$i]);
  71. $this->info('删除数据库备份文件:' . $fileArr[$i]);
  72. }
  73. }
  74. }
  75. /**
  76. * 导入数据
  77. */
  78. private function importData($file_name)
  79. {
  80. $file_name = storage_path('app/db_backup/' . $file_name); //要导入的SQL文件名
  81. $file_name = str_replace('\\', '/', $file_name);
  82. $DB_HOST = getenv('DB_HOST');
  83. $DB_DATABASE = getenv('DB_DATABASE');
  84. $DB_USERNAME = getenv('DB_USERNAME');
  85. $DB_PASSWORD = getenv('DB_PASSWORD');
  86. $command = "mysql --host=$DB_HOST --user=$DB_USERNAME " . "--password=" . $DB_PASSWORD . " " . $DB_DATABASE . " < " . $file_name;
  87. Process::fromShellCommandline($command)->run(function ($type, $buffer) {
  88. if (Process::ERR === $type) {
  89. $this->info('failed - 数据库恢复失败!');
  90. } else {
  91. $this->info('数据库恢复成功!');
  92. }
  93. });
  94. }
  95. }