简体   繁体   English

Laravel 数据库备份计划

[英]Laravel database backup schedule

I am trying to set up a scheduler for weekly database backup in laravel.我正在尝试为 laravel 中的每周数据库备份设置调度程序。 I have created a command and filled out some data like the command itself and description, and registered it in the console kernel as well.我创建了一个命令并填写了命令本身和描述等一些数据,并将其注册到控制台 kernel 中。 The issue is that the file never gets created and/or stored in storage.问题是该文件永远不会被创建和/或存储在存储中。

This is the part of the code where is the command:这是命令所在的代码部分:

public function handle()
{
    Log::info('Database backup completed.');

    $filename = 'mysite' . Carbon::now()->format('Y-m-d') . ".gz";
    $command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . "  | gzip > " . storage_path() . "/app/backup/" . $filename;
    $returnVar = NULL;
    $output = NULL;

    exec($command, $output, $returnVar);
}

This is the kernel part:这是 kernel 零件:

/**
 * The Artisan commands provided by your application.
 *
 * @var array
 */
protected $commands = [
    OtherCron::class,
    DatabaseBackupCron::class,
];

/**
 * Define the application's command schedule.
 *
 * @param Schedule $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
     $schedule->command('othercron')->dailyAt('00:00');
     $schedule->command('database-backup:cron')->everyFiveMinutes();
}

Note : I have used "->everyFiveMinutes()" just for testing purposes:)注意:我使用“->everyFiveMinutes()”只是为了测试目的:)

You could use a ready-made lib for that:您可以为此使用现成的库:
github.com/spresnac/laravel-artisan-database-helper github.com/spresnac/laravel-artisan-database-helper
and then just call the command in the scheduler as you like ;)然后根据需要在调度程序中调用命令;)

You can also set the full path to your mysqldump binary, if it's not in your path ;)如果它不在您的路径中,您还可以设置 mysqldump 二进制文件的完整路径;)

I tried your code, it is working for me, the only difference is in Kernel, I wrote like this.我试过你的代码,它对我有用,唯一的区别是 Kernel,我是这样写的。

    protected $commands = [
        'App\Console\Commands\DatabaseBackUp'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
      //some other commands
        $schedule->command('database:backup')->weekly();
    }

now the moment I run php artisan database:backup a file with the extension.zp created in the storage folder现在我运行php artisan database:backup在存储文件夹中创建的扩展名为.zp 的文件

在此处输入图像描述

php artisan make:command DatabaseBackUp

Use this command make databasebackup file.使用此命令制作数据库备份文件。

app/Console/Commands/DatabaseBackUp.php
<?php
  
namespace App\Console\Commands;
  
use Illuminate\Console\Command;
use Carbon\Carbon;
   
class DatabaseBackUp extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'database:backup';
  
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';
  
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
  
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $filename = "backup-" . Carbon::now()->format('Y-m-d') . ".gz";
  
        $command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . "  | gzip > " . storage_path() . "/app/backup/" . $filename;
  
        $returnVar = NULL;
        $output  = NULL;
  
        exec($command, $output, $returnVar);
    }
}

In this step, we need to create "backup" folder in your storage folder.在这一步中,我们需要在您的存储文件夹中创建“备份”文件夹。 you must have to create "backup" on following path:您必须在以下路径上创建“备份”:

storage/app/backup

Now, in this step, we need to schedule our created command.现在,在这一步中,我们需要安排我们创建的命令。 so let's update kernel file as like bellow:所以让我们更新内核文件,如下所示:

app/Console/Kernel.php
<?php
  
namespace App\Console;
  
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
  
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\DatabaseBackUp'
    ];
  
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('database:backup')->daily();
    }
  
    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
  
        require base_path('routes/console.php');
    }
}

you can check following command to getting database backup with this command:您可以使用以下命令检查以下命令以获取数据库备份:

php artisan database:backup

t will create one backup file on your backup folder. t 将在您的备份文件夹中创建一个备份文件。 you can check there.你可以在那里检查。

Now, we are ready to setup cron on our server.现在,我们准备在我们的服务器上设置 cron。

At last you can manage this command on scheduling task, you have to add a single entry to your server's crontab file:最后,您可以在调度任务上管理此命令,您必须在服务器的 crontab 文件中添加一个条目:

Run following command:运行以下命令:

crontab -e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM