不能执行我的cron工作

时间:2018-08-24 11:22:15

标签: laravel-5 cron

我已经完成了备份数据库的任务。

当我运行php artisan backup:database时, 我有一个错误提示,表明"App\Console\Commands\Storage"丢失了。

我在YouTube上关注了this个视频。这部影片距今已有3年了。

有人可以帮助我解决此错误。还是备份数据库还有其他选择吗?

下面是我的代码:

<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;

class DatabaseBackup extends Command
{    
    protected $signature = 'backup:database';    
    protected $description = 'This is to backup database';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $date = Carbon::now()->format('d/m/Y');
        $user = env('DB_USERNAME');
        $password = env('DB_PASSWORD');
        $database = env('DB_DATABASE');
        $command = "mysqldump --user={$user} -p{$password} {$database} > {$date}.sql";
        $process = new Process($command);
        $process->start();
        while ($process->isRunning()) {
           $public = Storage::disk('public');
           $public->put('users/'.$date.".sql", file_get_contents("{$date}.sql"));
        }       
    }
}

1 个答案:

答案 0 :(得分:0)

只需添加

use Storage;

,并且还将日期格式更改为d-m-Y。现在可以使用!

namespace App\Console\Commands;

use Carbon\Carbon;
use Storage;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;

class DatabaseBackup extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'backup:database';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This is to backup database';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $date = Carbon::now()->format('d-m-Y');
        $user = env('DB_USERNAME');
        $password = env('DB_PASSWORD');
        $database = env('DB_DATABASE');
        $command = "mysqldump --user={$user} -p{$password} {$database} > {$date}.sql";
        $process = new Process($command);
        $process->start();
        while ($process->isRunning()) {
           $public = Storage::disk('public');
           $public->put('users/'.$date.".sql", file_get_contents("{$date}.sql"));
        }

    }
}