不从控制器执行Laravel Backup命令

不从控制器执行Laravel Backup命令,laravel,backup,laravel-blade,Laravel,Backup,Laravel Blade,我创建了一个从cmd运行的备份命令,没有任何问题。下面是命令源代码: use Illuminate\Console\Command; class BackupDBCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'mysql:backup';

我创建了一个从cmd运行的备份命令,没有任何问题。下面是命令源代码:

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Backup the database';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $username = env('DB_USERNAME','root');
        $password = env('DB_PASSWORD','');
        $dbname = env('DB_DATABASE','dbname');
        $filename ='public/backups/'. $dbname.'-' . \Carbon\Carbon::now()->toDateString() . '.sql';

        exec('C:/xampp/mysql/bin/'.'mysqldump -u'.$username.' '.$dbname. ' > ' . $filename);
        $this->info('Your backup is being saved to the root directory ' . $filename);
    }
}
但是当我想从控制器执行这个命令时,它对我不起作用。日志文件和消息显示输出结果,显示“您的备份正在保存到根目录public/backups/dbname-2020-03-09.sql”。 控制器源代码:

public function create()
    {
        try {
            // start the backup process
            Artisan::call('mysql:backup');
            $output = Artisan::output();
            // log the results
            Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n" . $output);
            //dump($output);
            // return the results as a response to the ajax call
            //Alert::success('New backup created');
            return redirect()->back();
        } catch (Exception $e) {
            //Flash::error($e->getMessage());
            return redirect()->back()->with(["error"=>$e->getMessage()]);;
        }
    }

为什么不创建备份文件?

尝试使用laravel备份:而不是手动执行:

function backupdb($database = null){
    \Spatie\DbDumper\Databases\MySql::create()
        ->setDbName($database??env('DB_DATABASE', 'db'))
        //->setDumpBinaryPath('/opt/lampp/bin')
        ->setDumpBinaryPath('C:\xampp73\mysql\bin')
        ->setUserName(env('DB_USERNAME', 'username'))
        ->setPassword(env('DB_PASSWORD', 'pass'))
        ->dumpToFile('storage/app/backups/db-' . date('d-m-Y') . '.sql');
}