0

I'm trying to take a backup of my database using mysqldump. When I use php artisan serve command it works perfectly on my local machine. but when I access it through URL is generates an empty file.

public function getBackup()
    {
        $dbhost = env('DB_HOST');
        $dbuser = env('DB_USERNAME');
        $dbpass = env('DB_PASSWORD');
        $dbname = env('DB_DATABASE');

        $dbfilename = 'easytrax_db_' . date("Y_m_d_his") . '.sql.gz';
        $backupfile = base_path("db") . '/' . $dbfilename;

        exec("mysqldump --max_allowed_packet=4000M --skip-lock-tables -h$dbhost -u$dbuser -p$dbpass $dbname | gzip > $backupfile");

        return response()->download(base_path("db/{$dbfilename}"));
    }

currently using wamp. mysql version: 8.0.21 php version: 7.2.33

1 Answers1

0

Check your script step by step where this goes wrong:

  1. Do you have write permissions on the filesystem?
  2. Does the database login work?
  3. Is the dump created at all?
  4. Is the file path correct?
  5. Is the gzipping working? What if you skip it for now?
  6. Is a proper download link generated for the file?
  7. Are the env variables populated as they should be?
PtrTon
  • 3,705
  • 2
  • 14
  • 24
  • [This post](https://stackoverflow.com/questions/57309869/how-to-generate-a-table-backup-programatically-with-laravel/57310013#57310013) I wrote a while back might also give you some inspiration on the topic – PtrTon Mar 04 '21 at 12:48