J
J
Jesse Pinkman2021-11-17 12:28:29
PHP
Jesse Pinkman, 2021-11-17 12:28:29

How to backup mysql database using php?

Hey!
I need to backup/dump a database using php.
I tried this solution but none of them helped:

//разумеется тут данные свои, имя пользователя, пароль и название бд
exec("mysqldump --user={$user} --password={$pass} --host={$host} {$database} --result-file={$dir} 2>&1", $output);


$backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
   $command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass ". "test_db | gzip > $backup_file";
   
   system($command);


exec('mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql');


It creates an empty .sql file

Using
SERVER - localhost(xampp)
PHP - php7.4
MYSQL - 10.4.19-MariaDB

Maybe someone has already done this please share the solution

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-11-17
@jessepinkman010101

For you should specify the path to the executable file:

$path = 'C:\path\to\mysqldump\';
exec("{$path}mysqldump --user={$user} --password={$pass} --host={$host} {$database} --result-file={$dir} 2>&1", $output);

G
grek_cheburek, 2021-11-17
@grek_cheburek

And if the author's path changes, then what should he do? At one time, for myself, I wrote a script that made a backup of a database, or a table, or the entire server. I spent two days on this, but it was worth it. So I recommend that you write the same script for yourself and then everything will be ok. The logic is simple. You need to create some functions. One will prepare the table, the other will prepare the data from the table, and the third will collect it all into one file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question