Laravel 5.6-将mySql数据库备份到S3吗?

时间:2019-02-04 22:20:04

标签: php mysql laravel laravel-5 laravel-5.6

我试图像这样将整个mysql数据库备份到S3:

IPython

但是当运行<?php namespace App\Console\Commands; use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Support\Facades\Storage; 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 = 'Take a backup of the entire DB and upload to S3.'; /** * 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('Y-m-d_h-i'); $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()) { $s3 = Storage::disk('s3'); $s3->put('gallery-app-db/' . $date . ".sql", file_get_contents("{$date}.sql")); unlink("{$date}.sql"); } } } 然后查看存储桶.sql文件并在本地下载.sql文件时,它显示如下,而不是文件中的实际数据库/表:

enter image description here

有什么主意如何使.sql转储真正起作用并备份真实数据库及其所有表而不是使用文件?

1 个答案:

答案 0 :(得分:1)

假设您的密码包含一个结束Shell命令的字符,例如; & | && ||

或以命令行参数结尾的字符,例如空格。

或其他一些特殊字符,例如任何类型的引号,$!

如果不引用密码,很可能会生成一条截断的命令。

我建议将您的用户名和密码放在选项文件中,然后使用--defaults-file=...引用该选项文件

$command = "mysqldump --defaults-file=mysql-dump.cfg {$database} > {$date}.sql";

mysql-dump.cfg应该包含以下内容:

[client]
user = <your user>
password = <your password>

避免在任何可以运行ps的人那里输入密码时也很方便。

相关问题