0

I am trying to write a little script to backup all my localhost databases at once in seperate files. Currently my script does the backup part but does the download in one file. (So i get something like 30MB information_schema_backup.sql ) What i want is db_name_backup.sql for each database. Here is my code.

<?

    function backup_dbs()
    {
        global $db_name;
        global $dosyaadi; //dosyaadi means filename.
        $return='';

        /*
            Backup Part Here;
            I can share if anyone needs.
            I will post this on my blog anyway.
        */

        //save file
        $dosyaadi=$db_name.'-backup-'.time().'.sql';
        header("Content-type: application/octet-stream");
        header("Content-disposition: attachment;filename=$dosyaadi");
        echo $return;
    }

        $db_host = 'localhost';
        $db_user = 'root';
        $db_pass = '';

        $db = mysql_connect($db_host, $db_user, $db_pass);

        mysql_query("SET NAMES 'utf8'", $db);
        mysql_query("SET CHARACTER SET utf8", $db); 
        mysql_query("SET COLLATION_CONNECTION = 'utf8_general_ci'", $db); 

        $res = mysql_query("SHOW DATABASES");

        while ($row = mysql_fetch_assoc($res)) {
            $db_name =  $row['Database'];
            mysql_select_db($db_name, $db);
            backup_dbs();
        }
    die();
?>

Thanks in advance.

4

0 回答 0