Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Mail;
  7. use Illuminate\Support\Facades\Storage;
  8. class DailyBackupAndSyncCommand extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'tonic:db-backup';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Data backup for Tonic Navigator';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $filename = "backup-" . Carbon::now()->format('Y-m-d_H-i-s') . ".sql";
  39. $filePath = storage_path() . "/backup/" . $filename;
  40. $devDatabase = "db1";
  41. $this->destroyCreateAndDump($filePath, $devDatabase);
  42. $this->info('ALL IS GOOD');
  43. }
  44. private function destroyCreateAndDump($filePath, $database)
  45. {
  46. /**
  47. * If the DB is remote this will be useful
  48. * "pg_dump --username=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " --dbname=" . env('DB_DATABASE') . "
  49. */
  50. $command = "pg_dump " . env('DB_DATABASE') . " > " . $filePath;
  51. exec($command);
  52. $this->info('DATA DUMP FROM ' . env('DB_DATABASE') . ' IS PROCESSED AND SAVED TO `'. $filePath . '`');
  53. DB::connection()->statement("select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='".$database."' AND state='idle';");
  54. DB::connection()->statement('DROP DATABASE '.$database);
  55. DB::connection()->statement('CREATE DATABASE '.$database);
  56. $dumpDev = "psql ".$database." < " . $filePath;
  57. exec($dumpDev);
  58. $this->info('DATABASE RESTORED `'.$database.'` FROM `'. $filePath . '`');
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement