-1

I'm using laravel and mysql as database, and I want to make a backup database in hosting with one click button in html, but I wonder what is the best query for them instead of query all data in all table with loop, then insert it one by one to host with loop. Thanks before

user5436320
  • 133
  • 2
  • 17

1 Answers1

3

Use mysqldump

$ mysqldump -h <host> -u <user> -p<password> <database_name> > backup.sql

This will create a sql file backup.sql with all your data.

Note the missing space beween -p and <password>.

It's okay to give the password interactively (in which case, you'll have to skip the <password> part), but since you mentioned laravel in the question, I am assuming you want to code this and want it to be non-interactive.

EDIT: Using code

Execute the command with exec()

$cmd = "mysqldump -h <host> -u <user> -p<password> <database_name> > backup.sql";
exec($cmd, $output, $return_value);
linuxartisan
  • 2,396
  • 3
  • 21
  • 40