0

I have an export script for multiple tables into .sql file as a backup. The script works great but I want to save a copy of the backup on the server too! I can insert it as a record but I can't use the method move_uploaded_file as I used to for the photo upload.

Here is my script:

<?php 
    //ENTER THE RELEVANT INFO BELOW
    //http://stackoverflow.com/a/31531996/1620626
    $mysqlUserName      = "root";
    $mysqlPassword      = "xxx";
    $mysqlHostName      = "localhost";
    $DbName             = "xxx_db";
    $backup_name        ="mybackup.sql";
    $tables             = array("table1","table2");

   //or add 5th parameter(array) of specific tables:    array("mytable1","mytable2","mytable3") for multiple tables

    Export_Database($mysqlHostName,$mysqlUserName,$mysqlPassword,$DbName,  $tables=false, $backup_name=false );

    function Export_Database($host,$user,$pass,$name,  $tables=false, $backup_name=false )
    {
        $mysqli = new mysqli($host,$user,$pass,$name); 
        $mysqli->select_db($name); 
        $mysqli->query("SET NAMES 'utf8'");

        $queryTables    = $mysqli->query('SHOW TABLES'); 
        while($row = $queryTables->fetch_row()) 
        { 
            $target_tables[] = $row[0]; 
        }   
        if($tables !== false) 
        { 
            $target_tables = array_intersect( $target_tables, $tables); 
        }
        foreach($target_tables as $table)
        {
            $result         =   $mysqli->query('SELECT * FROM '.$table);  
            $fields_amount  =   $result->field_count;  
            $rows_num=$mysqli->affected_rows;     
            $res            =   $mysqli->query('SHOW CREATE TABLE '.$table); 
            $TableMLine     =   $res->fetch_row();
            $content        = (!isset($content) ?  '' : $content) . "\n\n".$TableMLine[1].";\n\n";

            for ($i = 0, $st_counter = 0; $i < $fields_amount;   $i++, $st_counter=0) 
            {
                while($row = $result->fetch_row())  
                { //when started (and every after 100 command cycle):
                    if ($st_counter%100 == 0 || $st_counter == 0 )  
                    {
                            $content .= "\nINSERT INTO ".$table." VALUES";
                    }
                    $content .= "\n(";
                    for($j=0; $j<$fields_amount; $j++)  
                    { 
                        $row[$j] = str_replace("\n","\\n", addslashes($row[$j]) ); 
                        if (isset($row[$j]))
                        {
                            $content .= '"'.$row[$j].'"' ; 
                        }
                        else 
                        {   
                            $content .= '""';
                        }     
                        if ($j<($fields_amount-1))
                        {
                                $content.= ',';
                        }      
                    }
                    $content .=")";
                    //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
                    if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) 
                    {   
                        $content .= ";";
                    } 
                    else 
                    {
                        $content .= ",";
                    } 
                    $st_counter=$st_counter+1;
                }
            } $content .="\n\n\n";
        }
        $backup_name = $backup_name ? $backup_name : $name."_(".date('H-i-s')."_".date('d-m-Y').").sql";
        //$backup_name = $backup_name ? $backup_name : $name.".sql";
        header('Content-Type: application/octet-stream');   
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-disposition: attachment; filename=\"".$backup_name."\"");  

        move_uploaded_file($backup_name, 'files/bak/'.$backup_name);
        echo $content; exit;
    }
?>

So, I want to save the exported file into files/bak as the user downloaded .sql file.

Wilf
  • 2,297
  • 5
  • 38
  • 82

2 Answers2

1

So this bit of code get the backup file downloaded to your browser and therefore your client machine, backup is safe and sound!

    header('Content-Type: application/octet-stream');   
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"".$backup_name."\"");  

    echo $content; exit;

But the move_uploaded_file(); is closely linked to Uploading file via the $_FILES mechanism, so dont use that.

All you need to do is write $content to a file, it could be done as simply as

file_put_contents('files/bak/'.$backup_name, $content);

Link to manual - file-put-contents

So the complete code for the tail of your script would be

file_put_contents('files/bak/'.$backup_name, $content);

header('Content-Type: application/octet-stream');   
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$backup_name."\"");  

echo $content; 
exit;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thank you, but the file wasn't save into the path as expect. How can I see the error? – Wilf Jan 08 '16 at 10:32
  • Refresh the page, I forgot to put the filepath on originally – RiggsFolly Jan 08 '16 at 10:32
  • Well now you are probably into a Unix file/directory Permissions issue. Make sure that the user account user by Apahe is allowed access to the `files/bak` directory. Can be called `www` but may be different on your host – RiggsFolly Jan 08 '16 at 10:34
0

Thanks for the help from @RiggsFolly. I come up with my own solutions :

$bakDir='files/bak/';
if (!is_dir($bakDir) or !is_writable($bakDir)) {
    // Error if directory doesn't exist or isn't writable.
    exit($bakDir." is unwritable");
} elseif (!empty($backup_name)) {
    // Error if the file exists and isn't writable.
    if(file_put_contents($bakDir.$backup_name, $content)){
        echo "<script>alert('Backup successfully');</script>";
    }else{
        echo "<script>alert('Backup failed');</script>";
    }
}
Wilf
  • 2,297
  • 5
  • 38
  • 82