Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Storage;
  8.  
  9. class SnapshotCommand extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'app:snapshot {action}';
  17.  
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Create or load a production snapshot';
  24.  
  25. /**
  26. * Execute the console command.
  27. *
  28. * @return mixed
  29. */
  30. public function handle()
  31. {
  32. switch ($this->argument('action')) {
  33. case 'create':
  34. if (!app()->environment('production')) {
  35. $this->error('"create" should only be run in the production environment');
  36.  
  37. return;
  38. }
  39.  
  40. $this->createSnapshot();
  41.  
  42. break;
  43. case 'load':
  44. if (!app()->environment('local')) {
  45. $this->error('"create" should only be run in the local environment');
  46.  
  47. return;
  48. }
  49.  
  50. $this->loadSnapshot();
  51.  
  52. break;
  53. default:
  54. $this->error('{action} must be either "create" or "load"');
  55. }
  56. }
  57.  
  58. protected function createSnapshot()
  59. {
  60. $this->runCommandWithCredentials(
  61. 'mysqldump --defaults-extra-file="{credentials_file}" --single-transaction {database} '
  62. . '> /app/storage/app/snapshot-data.sql; gzip -f /app/storage/app/snapshot-data.sql'
  63. );
  64.  
  65. Storage::cloud()->put('db-snapshots/snapshot-data.sql.gz', fopen('/app/storage/app/snapshot-data.sql.gz', 'r+'));
  66.  
  67. unlink('/app/storage/app/snapshot-data.sql.gz');
  68. }
  69.  
  70. protected function loadSnapshot()
  71. {
  72. DB::getSchemaBuilder()->dropAllTables();
  73.  
  74. Storage::disk()->put(
  75. 'snapshot-data.sql.gz',
  76. Storage::cloud()->getDriver()->readStream('db-snapshots/snapshot-data.sql.gz')
  77. );
  78.  
  79. $this->runCommandWithCredentials(
  80. 'zcat /app/storage/app/snapshot-data.sql.gz'
  81. . ' | mysql --defaults-extra-file="{credentials_file}" {database}'
  82. );
  83.  
  84. unlink('/app/storage/app/snapshot-data.sql.gz');
  85. }
  86.  
  87. protected function runCommandWithCredentials($command)
  88. {
  89. $dbConfig = config('database.connections.' . config('database.default'));
  90.  
  91. $disk = Storage::disk('local');
  92.  
  93. $disk->put('mysql-credentials.txt', implode(PHP_EOL, [
  94. '[client]',
  95. "user = '{$dbConfig['username']}'",
  96. "password = '{$dbConfig['password']}'",
  97. "host = '{$dbConfig['host']}'",
  98. "port = '{$dbConfig['port']}'",
  99. ]));
  100.  
  101. $command = str_replace(
  102. ['{credentials_file}', '{database}'],
  103. [$disk->path('mysql-credentials.txt'), $dbConfig['database']],
  104. $command
  105. );
  106.  
  107. exec($command);
  108.  
  109. $disk->delete('mysql-credentials.txt');
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement