Codice PHP:
	
 class Backup
    {
        var $newline;
        var $struct_only = false;
        var $output = true;
        var $compress = true;
        var $filename = "";
        function Backup( $struct_only = false, $output = true, $compress = true, $filename = "" )
        {
            $this->output = $output;
            $this->struct_only = $struct_only;
            $this->compress = $compress;
            $this->filename = $filename;
            $this->newline = $this->_define_newline();
        }
        /**
         * Generate the DB Dump.
         * @access private
         */
        function _backup()
        {
            global $dbcore ;
            $now = gmdate( 'D, d M Y H:i:s' ) . ' GMT';
            $newfile  = "";
            $newfile .= "#------------------------------------------" . $this->newline;
            $newfile .= "# Database Backup Class by Iván Melgrati" . $this->newline;
            $newfile .= "# Database: $this->dbname" . $this->newline;
            $newfile .= "# Date: $now" . $this->newline;
            $newfile .= "#------------------------------------------" . $this->newline . $this->newline;
            
            if ( !$dbcore->connect() ) // If no connection can be obtained, return empty string
            {
                return "Error. Can´t connect to Database: $this->dbname";
            }
            
            $result = $dbcore->query( "show tables from `". DATABASE_NAME ."`" );
            while ( list($table) = $dbcore->fetch_row($result) )
            {
                $i=0;
                $newfile .= $this->_get_def( $table );
                $newfile .= "$this->newline";
                if ( !$this->struct_only ) // If table data also has to be written, get table contents
                  $newfile .= $this->_get_content( $table );
                $i++;
            }
            $this->_out( $newfile );
        }
        function _out( $dump )
        {
            if ( $this->filename )
            {
                $fptr = fopen( $this->filename, "wb" );
                if ( $fptr )
                {
                    if ( $this->compress )
                    {
                        $gzbackupData = "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr( gzcompress($dump, 9), 0, -4 ) . pack( 'V', crc32($dump) ) . pack( 'V', strlen($dump) );
                        fwrite( $fptr, $gzbackupData );
                    }
                    else  fwrite( $fptr, $dump );
                    fclose( $fptr );
                }
            }
            else
            {
                if ( ($this->compress) and ($this->output) )
                {
                    $gzbackupData = "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr( gzcompress($dump, 9), 0, -4 ) . pack( 'V', crc32($dump) ) . pack( 'V', strlen($dump) );
                    echo $gzbackupData;
                }
                else  echo $dump;
            }
        }
        function _get_def( $tablename )
        {
        global $dbcore;
            $def = "";
            $def .= "#------------------------------------------" . $this->newline;
            $def .= "# Table definition for $tablename" . $this->newline;
            $def .= "#------------------------------------------" . $this->newline;
            $def .= "DROP TABLE IF EXISTS $tablename;" . $this->newline . $this->newline;
            $def .= "CREATE TABLE $tablename (" . $this->newline;
            $result = $dbcore->query( "SHOW FIELDS FROM `$tablename`" ) or die( "Table $tablename not existing in database" );
            while ( $row = $dbcore->fetch_array($result) )
            {
                // Updated after Carlos Carrasco's suggestion. Thanks!
                $def .= " `$row[Field]` $row[Type]"; // Sorround field names with quotes
                if ( $row["Null"] != "YES" ) $def .= " NOT NULL";
                if ( $row["Default"] != "" )
                {
                    if ( $row["Default"] == "CURRENT_TIMESTAMP" )
                    {
                        $def .= " default CURRENT_TIMESTAMP";
                    }
                    else
                    {
                        $def .= " default $row[Default]";
                    }
                }
                if ( $row['Extra'] != "" ) $def .= " $row[Extra]";
                $def .= ",$this->newline";
            }
            $def = preg_replace( "/,$this->newline$/", "", $def );
            $result = $dbcore->query( "SHOW KEYS FROM `$tablename`" );
            while ( $row = $dbcore->fetch_array($result) )
            {
                $kname = $row['Key_name'];
                if ( ($kname != "PRIMARY") && ($row['Non_unique'] == 0) ) $kname = "UNIQUE|$kname";
                if ( !isset($index[$kname]) ) $index[$kname] = array();
                $index[$kname][] = $row['Column_name'];
            }
            while ( list($x, $columns) = @each($index) )
            {
                $def .= ",$this->newline";
                if ( $x == "PRIMARY" ) $def .= "   PRIMARY KEY (" . implode( $columns, ", " ) . ")";
                else
                    if ( substr($x, 0, 6) == "UNIQUE" ) $def .= "   UNIQUE " . substr( $x, 7 ) . " (" . implode( $columns, ", " ) . ")";
                    else  $def .= "   KEY $x (" . implode( $columns, ", " ) . ")";
            }
            $def .= "$this->newline);";
            return ( stripslashes($def) );
        }
        function _get_content( $tablename )
        {
            $content = "";
            $result = @mysql_query( "SELECT * FROM $tablename" );
            if ( @mysql_num_rows($result) > 0 )
            {
                $content .= "#------------------------------------------" . $this->newline;
                $content .= "# Data inserts for $tablename" . $this->newline;
                $content .= "#------------------------------------------" . $this->newline;
            }
            while ( $row = @mysql_fetch_row($result) )
            {
                $insert = "INSERT INTO $tablename VALUES (";
                for ( $j = 0; $j < @mysql_num_fields($result); $j++ )
                {
                    if ( !isset($row[$j]) ) $insert .= "NULL,";
                    else
                        if ( $row[$j] != "" ) $insert .= "'" . addslashes( $row[$j] ) . "',";
                        else  $insert .= "'',";
                }
                $insert = preg_replace( "/,$/", "", $insert );
                $insert .= ");$this->newline";
                $content .= $insert;
            }
            return $content . $this->newline;
        }
        function _define_newline()
        {
            $unewline = "\r\n";
            if ( strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'win') )
            {
                $unewline = "\r\n";
            }
            else
                if ( strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'mac') )
                {
                    $unewline = "\r";
                }
                else
                {
                    $unewline = "\n";
                }
                return $unewline;
        }
        function _get_browser_type()
        {
            $USER_BROWSER_AGENT = "";
            if ( preg_match('#OPERA(/| )([0-9].[0-9]{1,2})/#', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
            {
                $USER_BROWSER_AGENT = 'OPERA';
            }
            else
                if ( preg_match('#MSIE ([0-9].[0-9]{1,2})#', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                {
                    $USER_BROWSER_AGENT = 'IE';
                }
                else
                    if ( preg_match('#OMNIWEB/([0-9].[0-9]{1,2})#', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                    {
                        $USER_BROWSER_AGENT = 'OMNIWEB';
                    }
                    else
                        if ( preg_match('#MOZILLA/([0-9].[0-9]{1,2})#', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                        {
                            $USER_BROWSER_AGENT = 'MOZILLA';
                        }
                        else
                            if ( preg_match('#KONQUEROR/([0-9].[0-9]{1,2})#', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
                            {
                                $USER_BROWSER_AGENT = 'KONQUEROR';
                            }else{
                                $USER_BROWSER_AGENT = 'OTHER';
                            }
                            return $USER_BROWSER_AGENT;
        }
        function _get_mime_type()
        {
            $USER_BROWSER_AGENT = $this->_get_browser_type();
            $mime_type = ( $USER_BROWSER_AGENT == 'IE' || $USER_BROWSER_AGENT == 'OPERA' ) ? 'application/octetstream' : 'application/octet-stream';
            return $mime_type;
        }
        function perform_backup()
        {
            $now = gmdate( 'D, d M Y H:i:s' ) . ' GMT';
            if ( $this->compress ){
                $filename = $this->dbname . ".sql";
                $ext = "gz";
            }else{
                $filename = $this->dbname;
                $ext = "sql";
            }
            $USER_BROWSER_AGENT = $this->_get_browser_type();
            if( $this->filename ){
                $this->_backup();
            }
            else
                if ( $this->output == true ){
                    header( 'Content-Type: ' . $this->_get_mime_type() );
                    header( 'Expires: ' . $now );
                    if ( $USER_BROWSER_AGENT == 'IE' ){
                        header( 'Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"' );
                        header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
                        header( 'Pragma: public' );
                    }else{
                        header( 'Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"' );
                        header( 'Pragma: no-cache' );
                    }
                    $this->_backup();
                }else{
                    echo "<html><body><pre style='text-align:left'>";
                    echo htmlspecialchars($this->_backup());
                    echo "</PRE></BODY></HTML>";
                }
        }
    }