1) Use the make:command command to create a new command. Simply pass in the command name, like so: php artisan make:command DBBackup |
2)To do this, update the $signature property of the command, like this: protected $signature = 'db:backup'; |
3)To do this, update the $description property to match this: protected $description = 'Backup the database'; |
5)add below namespace if not exit use Symfony\Component\Process\Process; use Symfony\Component\Process\Exception\ProcessFailedException; |
6)app/Console/Commands/DBBackup.php DB command full example <?php namespace App\Console\Commands; use Illuminate\Console\Command; use Symfony\Component\Process\Process; use Symfony\Component\Process\Exception\ProcessFailedException; class DBBackup 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 the database'; protected $process; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); $fileName='sample_'.date("Y-m-d").'.sql'; $this->process = new Process(sprintf( 'mysqldump -u%s -p%s %s %s %s %s %s %s %s %s %s > %s', config('database.connections.mysql.username'), config('database.connections.mysql.password'), config('database.connections.mysql.database'), '--single-transaction', '--quick', '|', 'ionice', '-c2', '-n', '7', 'tee', storage_path('backups/'.$fileName) )); } /** * Execute the console command. * * @return mixed */ public function handle() { try { $this->process->setTimeout(null); $this->process->setIdleTimeout(null); $this->process->mustRun(); $this->info('The backup has been proceed successfully.'); } catch (ProcessFailedException $exception) { $this->error('The backup process has been failed.'); } } } --Description $this->process = new Process(sprintf( 'mysqldump -u%s -p%s %s %s %s %s %s %s %s %s %s > %s', config('database.connections.mysql.username'), config('database.connections.mysql.password'), config('database.connections.mysql.database'), '--single-transaction', '--quick', '|', 'ionice', '-c2', '-n', '7', 'tee', storage_path('backups/'.$fileName) )); |
mysqldump Command:
mysqldump is a command-line utility for backing up MySQL or MariaDB databases.
The basic structure is mysqldump -u<username> -p<password> <database> [options] > <output file>.
Command Components:
-u%s: Specifies the MySQL username. The %s is a placeholder that will be replaced by the actual
username obtained from the Laravel configuration (config('database.connections.mysql.username')).
-p%s: Specifies the MySQL password. Similar to the username, it uses the placeholder %s and
retrieves the password from the Laravel configuration (config('database.connections.mysql.password')).
%s: Specifies the name of the database to be backed up. It is replaced by the actual database name
obtained from Laravel configuration (config('database.connections.mysql.database')).
--single-transaction: Ensures that the backup is performed as a single transaction, which can be useful
for preventing inconsistencies in the backup data.
--quick: This option is used to retrieve rows one at a time from the server rather than retrieving the
entire result set at once. This can be useful for large tables.
|: This pipe (|) symbol is used for piping the output of the mysqldump command into the next command
(ionice and tee).
ionice -c2 -n 7: Sets the I/O scheduling class and priority for the tee command. It uses the ionice utility
to set the I/O priority to best effort (-c2) with a priority level of 7 (-n 7).
tee: The tee command is used to redirect the output of the mysqldump command to a file and also to
the standard output (console). It is used to create a backup file at the specified storage path.
storage_path('backups/'.$fileName): Specifies the path where the backup file will be stored. The
filename is constructed with a prefix ('eliteopinio_') and the current date (date("Y-m-d")).
In summary, this line of code constructs a Process object that represents the execution of a
mysqldump command with various options, and it pipes the output through ionic and tee to create a backup file at a specified storage path.
public function handle() { try { $this->process->setTimeout(null); $this->process->setIdleTimeout(null); $this->process->mustRun(); $this->info('The backup has been proceeded successfully.'); } catch (ProcessFailedException $exception) { $this->error('The backup process has failed.'); } } |
--description
- The handle method is called when the command is executed.
- The Process object is configured to have no timeout.
- The mustRun method is used to execute the process. If it fails, a ProcessFailedException is
caught, and an error message is displayed. Otherwise, a success message is shown.
7)fire command in your terminal : php artisan db:backup and you can check path: storage/backups/ |
This is manually process using command
8)you can set using cron job daily base backup below command Command : 17 20 * * * php /var/www/html/sample/artisan db:backup >> /dev/null 2>&1 |
/var/www/html/sample/artisan : This is your project path you have to set
17 20 * * * : This is cron time which time you need to backup
No comments:
Post a Comment
Thank You For Comment