Laravel 5.6 - 备份mysql数据库到S3?

问题描述 投票:1回答:1

我想备份整个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文件,它这样表示,而不是实际的数据库/表文件中是:

enter image description here

任何想法如何有.SQL转储实际工作和备份与所有的表,而不是使用文件一起真实的数据库?

php mysql laravel laravel-5 laravel-5.6
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.