Guest User

Untitled

a guest
Feb 4th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. <!-- ##MySQL Backups with Laravel -->
  2. <!--
  3. ##Introduction
  4. In this tutorial I am going to give you simple steps to take MySQL Database backup from terminal/command line using mysqldump.
  5. I am guessing you’re using Linux probably ubuntu operating system, it can be on server or local development environment.
  6.  
  7. ##Requirment
  8. -laravel
  9. -MySQL Databases
  10.  
  11. ##Installation
  12. ####Install composer dependecies
  13.  
  14. ```
  15. composer install
  16. ```
  17.  
  18. ##Steps
  19. -First we have to create new command using artisan by running the following in the command line.
  20.  
  21. ```
  22. php artisan make:command `Name of command file`
  23. ```
  24.  
  25. After you create your console command, laravel automatically registers it for you in the `app/console/commands` in our Example we named the command file `DBBackup`
  26. Let’s take a look at the full command; then we explain how it does work:
  27.  
  28. ``` -->
  29. <?php
  30.  
  31. namespace App\Console\Commands;
  32.  
  33.  
  34. use Illuminate\Console\Command;
  35. use Symfony\Component\Process\Process;
  36. use Symfony\Component\Process\Exception\ProcessFailedException;
  37. use Mail;
  38. use DB;
  39. class BackupDatabase extends Command
  40. {
  41. /**
  42. * The name and signature of the console command.
  43. *
  44. * @var string
  45. */
  46. protected $signature = 'db:backup';
  47.  
  48. /**
  49. * The console command description.
  50. *
  51. * @var string
  52. */
  53. protected $description = 'Backup th database';
  54.  
  55. /**
  56. * Create a new command instance.
  57. *
  58. * @return void
  59. */
  60.  
  61. public function __construct()
  62. {
  63. parent::__construct();
  64.  
  65.  
  66. }
  67.  
  68. /**
  69. * Execute the console command.
  70. *
  71. * @return mixed
  72. */
  73. protected $content;
  74. public function handle()
  75. {
  76.  
  77.  
  78. try {
  79. $line=\readline('Enter prefix to backup: ');
  80. $Databases=DB::select('show databases');
  81. foreach($Databases as $database){
  82. $dbName=$database->Database;
  83. if((preg_match_all("/$line(.*)/",$dbName))){
  84. \array_push($DBName_Backup,$dbName);
  85. $process=new Process(sprintf(
  86. 'mysqldump -u%s -p%s %s > %s',
  87. \env('DB_USERNAME'),
  88. \env('DB_PASSWORD'),
  89. $dbName,
  90. storage_path('backups/backup'."-".$dbName.\date('Y-M-D')."-".time().'.sql')
  91.  
  92. ));
  93.  
  94. $process->mustRun();
  95. }
  96. }
  97.  
  98.  
  99.  
  100. $content=array( 'massage'=>" The following Databases has been backup successfully",'database'=>$DBName_Backup);
  101. // Mail::send('view page' ,data,function($massage){}
  102. Mail::send('mail.sendMail',$content,function($massage){
  103. $massage->to('emailExample@gmail.com',"name ")
  104. ->subject('Database Backup');
  105. $massage->from(emailExample@gmail.com',"name ");
  106. });
  107. } catch (ProcessFailedException $exception) {
  108.  
  109. $content= array('massage'=>'The backup process has been failed.');
  110. Mail::send('mail.sendMail',$content,function($massage){
  111. $massage->to('emailExample@gmail.com',"name username")
  112. ->subject('Database Backup');
  113. $ $massage->from(emailExample@gmail.com',"name ");
  114. });
  115.  
  116.  
  117. }
  118.  
  119. }
  120.  
  121.  
  122. }
  123.  
  124. // mail.sendMail
  125. // here where display the masaage content
  126. <html>
  127. <body>
  128. <div>
  129. <h1>{{$massage}}</h1>
  130. @foreach ($database as $db_name)
  131. <h1>{{$db_name}}</h1>
  132. @endforeach
  133.  
  134. </div>
  135. </body>
  136. </html>
  137.  
  138. // ```
  139.  
  140. // -In the constructor, we initialize a new `Symfony\Component\Process\Process`instance.The reason we use Symfony’s Process here,because it offers many nice features,For example,if the process fails, we want to throw an exception that we can handle how we want.
  141. // -After generating command, you should fill in the `signature`and `description` properties of the class, wich will display in your command on the `php artisan list` .
  142.  
  143. // -The `handle` method will call when our command is executed ,here we place our command logic in this method.
  144. // -In the handle method, we have elementary `try-catch block`.First,we call `readline()`method, this method reads a single line from the user and return a string from user,we use it here to specify which databases we want to backups it.
  145. // -Then we use `DB::select(show databases)`to get all the databases exist in local environment or server,then we loop through the databases if the name is match it will backup all the databases start with the name we specify.
  146.  
  147. // ```
  148. // `mysqldump -u[user] -p[pass] [db] > [file_path]`.
  149. // ```
  150.  
  151. // -`mysqldump` in this command we can quickly dump and save database to an sql file,as you can see in the above example we give the user ,the password,and the DB,then give the file path where we want to save the data
  152.  
  153. // -Then we call`mustRun()` method to execute the code, we use `mustRun()` method because it will throw a `ProcessFailedException` if the process couldn't be executed successfully.
  154. // -If the Database backup success or failed the user will receive email message, content the status of backups and the names of database try to bakups it.
  155.  
  156. // ##Run the code
  157. // -last step befor running the code we have to add this command to Kernal file go to `app\console\Kernel` and add your command path to the kernal file.
  158.  
  159. // ```
  160. // protected \$commands = [
  161. // \App\Console\Commands\DBBackup::class,
  162. // ];
  163. // ```
  164.  
  165. // So now if we run the `php artisan db:backup`command it will ask you to enter the name of database you want to backups,after you ente the name it will save the backup in `storage\backups`.
Add Comment
Please, Sign In to add comment