[Q] Export SQL database into a file

Discussion in 'MySQL' started by Eng_A_Moktar, Nov 15, 2009.

  1. #1
    I wanna export a database into an .sql file or gz file
    I wanna do it with php
    something like that which happen when you are in phpmyadmin
    and do export, you will prompt for download ...
    Thanks ...
     
    Eng_A_Moktar, Nov 15, 2009 IP
  2. iama_gamer

    iama_gamer Active Member

    Messages:
    404
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    try this
    not all hosts allow it but if system() operation is allowed this is an easy way.
    config, opendb need to contain the variables to connect to the db with the variables used below

    <?php
    include 'config.php';
    include 'opendb.php';

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

    include 'closedb.php';
    ?>
     
    iama_gamer, Dec 8, 2009 IP