Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- ##MySQL Backups with Laravel -->
- <!--
- ##Introduction
- In this tutorial I am going to give you simple steps to take MySQL Database backup from terminal/command line using mysqldump.
- I am guessing you’re using Linux probably ubuntu operating system, it can be on server or local development environment.
- ##Requirment
- -laravel
- -MySQL Databases
- ##Installation
- ####Install composer dependecies
- ```
- composer install
- ```
- ##Steps
- -First we have to create new command using artisan by running the following in the command line.
- ```
- php artisan make:command `Name of command file`
- ```
- 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`
- Let’s take a look at the full command; then we explain how it does work:
- ``` -->
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Symfony\Component\Process\Process;
- use Symfony\Component\Process\Exception\ProcessFailedException;
- use Mail;
- use DB;
- class BackupDatabase extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'db:backup';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Backup th database';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- protected $content;
- public function handle()
- {
- try {
- $line=\readline('Enter prefix to backup: ');
- $Databases=DB::select('show databases');
- foreach($Databases as $database){
- $dbName=$database->Database;
- if((preg_match_all("/$line(.*)/",$dbName))){
- \array_push($DBName_Backup,$dbName);
- $process=new Process(sprintf(
- 'mysqldump -u%s -p%s %s > %s',
- \env('DB_USERNAME'),
- \env('DB_PASSWORD'),
- $dbName,
- storage_path('backups/backup'."-".$dbName.\date('Y-M-D')."-".time().'.sql')
- ));
- $process->mustRun();
- }
- }
- $content=array( 'massage'=>" The following Databases has been backup successfully",'database'=>$DBName_Backup);
- // Mail::send('view page' ,data,function($massage){}
- Mail::send('mail.sendMail',$content,function($massage){
- $massage->to('emailExample@gmail.com',"name ")
- ->subject('Database Backup');
- $massage->from(emailExample@gmail.com',"name ");
- });
- } catch (ProcessFailedException $exception) {
- $content= array('massage'=>'The backup process has been failed.');
- Mail::send('mail.sendMail',$content,function($massage){
- $massage->to('emailExample@gmail.com',"name username")
- ->subject('Database Backup');
- $ $massage->from(emailExample@gmail.com',"name ");
- });
- }
- }
- }
- // mail.sendMail
- // here where display the masaage content
- <html>
- <body>
- <div>
- <h1>{{$massage}}</h1>
- @foreach ($database as $db_name)
- <h1>{{$db_name}}</h1>
- @endforeach
- </div>
- </body>
- </html>
- // ```
- // -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.
- // -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` .
- // -The `handle` method will call when our command is executed ,here we place our command logic in this method.
- // -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.
- // -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.
- // ```
- // `mysqldump -u[user] -p[pass] [db] > [file_path]`.
- // ```
- // -`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
- // -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.
- // -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.
- // ##Run the code
- // -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.
- // ```
- // protected \$commands = [
- // \App\Console\Commands\DBBackup::class,
- // ];
- // ```
- // 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