PHP Database Dump Script - are there any issues?

后端 未结 6 2007
梦谈多话
梦谈多话 2020-12-17 07:35

I found a PHP function to dump a mySQL database that someone else had written, and then cleaned it up and formatted it a bit. I wanted to know if I could get a critique of i

相关标签:
6条回答
  • 2020-12-17 07:42

    Character sets? Perhaps SET NAMES utf8 would be a good addition.

    Also, what happens if the database contains views?

    0 讨论(0)
  • 2020-12-17 07:53

    That script has serious, deal-breaking problems. It will not work for any but the most trivial database.

    • NULLs are not supported.
    • Character sets are not taken into account.
    • Table names are not delimited.
    • Only tables are supported -- not views, stored procedures, triggers, functions, etc.
    • addslashes() is not character-set safe.
    • mysql_query() pre-fetches all the rows from the table, so if you query a table with millions of rows, you will exceed your PHP memory limit. Use mysql_unbuffered_query() instead. On second thought, I see you collect all the output in $return so this is moot.
    • Your suppression of errors with the @ operator is bad practice. Check for errors and fail gracefully with an informative message.

    Your requirement not to use mysqldump is absurd.

    Why make so much work for yourself reinventing the wheel, and still get it so wrong? Just run mysqldump via shellexec().


    See also:

    • Why is my database backup script not working in php?
    0 讨论(0)
  • 2020-12-17 07:53

    Try mysql command or mysqldump command

    0 讨论(0)
  • 2020-12-17 07:54

    create backup DB :

    <?php
    
    $dbHost = 'DBHOST';
    $dbUser = 'DBUSER';
    $dbPassword = 'DBPASSWORD';
    $dbName = 'DBNAME';
    $tables = '*';
    
    backup_tables($dbHost,$dbUser,$dbPassword,$tables);
    
    /* backup the db OR just a table */
    function backup_tables($host,$user,$pass,$name,$tables = '*')
    {
    
        $db = new PDO("mysql:host=$host;dbname=$name;", $user, $pass);
    
    
        //get all of the tables
        if($tables == '*')
        {
            $tables = array();
    
            $result = $db->query('SHOW TABLES');
    
            $tables= $result->fetchAll(PDO::FETCH_COLUMN, 0);
    
        }
        else
        {
            $tables = is_array($tables) ? $tables : explode(',',$tables);
        }
    
        $return="";
    
        //cycle through
        foreach($tables as $table)
        {
    
    
            $return.= 'DROP TABLE  IF EXISTS '.$table.';';
            $result=$db->query('SHOW CREATE TABLE '.$table);
            $row2 = $result->fetch(PDO::FETCH_NUM);
            $return.= "\n\n".$row2[1].";\n\n";
    
            $result = $db->query('SELECT * FROM '.$table);
    
            foreach ($result->fetchAll(PDO::FETCH_ASSOC) as  $key=>$value) {
    
                // build query...
               $return .= "INSERT INTO $table (`".implode("`, `", array_keys($value))."`)
                VALUES ('".implode("', '", $value)."');\n\n";
    
            }
    
            $return.="\n\n\n";
        }
    
    
        //save file
        $handle = fopen('db-backup-'.date('Y-m-d--H-i-s').'-'.(md5(implode(',',$tables))).'.sql','w+');
        fwrite($handle,$return);
        fclose($handle);
    }
    
    0 讨论(0)
  • 2020-12-17 07:56

    This will not dump stored procedures, functions, views, triggers and so on.

    Edit: You can dump procedures etc. this way too. Just use i.e. SHOW PROCEDURE STATUS; to get the list of procedures and then SHOW CREATE PROCEDURE for each procedure. The same thing for functions, views, triggers...

    Don't forget SHOW CREATE DATABASE; either.

    0 讨论(0)
  • 2020-12-17 08:02

    In case this is a very huge database that needs to be dumped, make sure your server (and php max execution memory per script) has enough memory to keep the whole $return in memory, otherwise you better flush to a file once a while, or every line.

    0 讨论(0)
提交回复
热议问题