Backup a mysql database and download as a file

How to backup the mysql database and download it as a .sql file by using PHP Codes


A very simple solution would be something like (first example):

Naturally this will only make a Data dump of the table.

What you could do is use this code:

What this code does is actually gets a description of the table (i.e its structure), creates all the tables and pushes data. pretty much like any other tool does.

Then its just a matter of saving it from string to a file (file_put_contents() for instance or something similar, depending on your preference and need)

mysqldump -u username -p password database > file

Alternatively, phpMyAdmin can do this too with the Export tool.

Use phpmyadmin


You can use shell_exec to execute this command

mysqldump -u username -p password database > file

This will generate a dump file,and then redirect user to this generated file.

Do you have phpmyadmin? If so you can export it from there by clicking "Export" at the top (on the selected table/db).

I know its a bit late but hope someone else will find this.

//php file - html code:
    require_once 'connect.php'; //holds database variables with connect details
    require_once 'Admin_DatabaseFiles_Backup.php'; //Include the admin logout script

<form action="" method="post" class="form form">
    <!--<input type="hidden" name="backup" value="1" />-->
    <div class="float_left w200">
            <label class="title">Backup database</label>
            <span class="left">
              <input type="checkbox" name="db" value="1" checked="checked"/>
            <label class="title">Backup files</label>
            <span class="left">
                <input type="checkbox" name="files" value="1" checked="checked" />
    <p class="float_left">
        <input type="submit" name="submit" value="Backup" class="button" />

//php file Admin_DatabaseFiles_Backup.php:

if ($_POST['submit']=="Backup"){

    if ($_POST['db'] == "1"){

        $directory = "DatabaseFileBackups/";
        $dateAndTime = "".date('d-m-Y-H-i-s');
        $fileName = "".$dbname.$dateAndTime.".sql";
        $backupFile = "mysqldump --user=$dbuser --password='$dbpass' --host=$dbhost $dbname > ".$directory.$fileName;


        if($output == ''){ 
            echo = '<br />Failed To Backup Database!';
            echo = '<br />Database Backup Was Successful!';

    if ($_POST['files'] == "1"){

            echo 'Seleceted files';



If you have phpMyAdmin you can do it at the Export menu.

If you look for a command-line tool take a look at mysqldump.

Need Your Help

Displaying Blogs according to publish time

php jquery feeds

I have a doubt am displaying posts of blogs[more than 1] and now i want to display blogs according to publish date mean new post 1st next 2nd and so on...

Is it possible to alter database using EntityFramework- Database First workflow

sql entity-framework entity-framework-5 ef-database-first database-first

is it possible to change database tables (e.g adding a field in a particular table) in an application using Entity framework.??