BackupToCloud.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Http\File;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Storage;
  6. use Spatie\Backup\BackupDestination\BackupDestination;
  7. class BackupToCloud extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'backup:cloud';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Send backups to cloud storage';
  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. $localDisk = Storage::disk('local');
  38. $cloudDisk = Storage::disk('backup');
  39. $backupDestination = new BackupDestination($localDisk, '', 'local');
  40. if(
  41. empty(config('filesystems.disks.backup.key')) ||
  42. empty(config('filesystems.disks.backup.secret')) ||
  43. empty(config('filesystems.disks.backup.endpoint')) ||
  44. empty(config('filesystems.disks.backup.region')) ||
  45. empty(config('filesystems.disks.backup.bucket'))
  46. ) {
  47. $this->error('Backup disk not configured.');
  48. $this->error('See https://docs.pixelfed.org/technical-documentation/env.html#filesystem for more information.');
  49. return Command::FAILURE;
  50. }
  51. $newest = $backupDestination->newestBackup();
  52. $name = $newest->path();
  53. $parts = explode('/', $name);
  54. $fileName = array_pop($parts);
  55. $storagePath = 'backups';
  56. $path = storage_path('app/'. $name);
  57. $file = $cloudDisk->putFileAs($storagePath, new File($path), $fileName, 'private');
  58. $this->info("Backup file successfully saved!");
  59. $url = $cloudDisk->url($file);
  60. $this->table(
  61. ['Name', 'URL'],
  62. [
  63. [$fileName, $url]
  64. ],
  65. );
  66. return Command::SUCCESS;
  67. }
  68. }