How do i backup a SQL database using PHP.
Is there a vendor agnostic way to do this that conforms to ANSI SQL?
If not maybe you can list how to do it for each of the database vendors?
Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. You have several options to fetch data from MySQL. The most frequently used option is to use function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both.
To back up a MySQL database, you can use either third-party tools or execute the mysqldump command from the command line. mysqldump is a command-line utility used to generate a MySQL logical database backup. It creates a single . sql file that contains a set of SQL statements.
Every database system comes with some program for dumping its contents.
pg_dump
mysqldump
You can simply call that program from PHP using system()
or shell_exec()
.
For example, if you use PostgreSQL with enabled Ident authentication and want to dump the Database test
directly as SQL text to the browser, it's as simple as:
<?php
header('Content-type: text/plain');
system('pg_dump test');
?>
When using MySQL with database user and password stored into ~/.my.cnf
, it is also very simple:
<?php
header('Content-type: text/plain');
system('mysqldump test');
?>
However, don't do this:
because transmitting a password as command line argument is very insecure.
<?php
header('Content-type: text/plain');
system('mysqldump -utestuser -ptestpassword test');
?>
I love phpmybackuppro
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With