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

Laravel 5.6 - Backup mySql database to S3?

我正在尝试将整个 mysql 数据库备份到 S3,如下所示:

<?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");
            }
        }
    }

但是当 运行 php artisan backup:database 然后查看存储桶 .sql 文件,并在本地下载 .sql 文件时,它显示如下而不是实际 database/tables 在文件中:

知道如何让 .sql 转储实际工作并备份真实数据库及其所有表而不是使用文件吗?

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

或结束命令行参数的字符,如空格。

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

如果不引用密码,很可能会生成一个 t运行cated 命令。

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

引用该选项文件
$command = "mysqldump --defaults-file=mysql-dump.cfg {$database} > {$date}.sql";

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

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

这也很好,可以避免让任何可以 运行 ps.

的人清楚地看到您的密码