Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Old 09-28-2005, 07:24 AM SQL backup code
Harvey's Avatar
Super Spam Talker

Posts: 901
Name: Harvey C
Location: Brighton, UK
Trades: 0
I'm transferring my databases to my new server (or trying!).

I have been given the following code to use:

mysqldump -uuser -ppass --compatible=mysql40 db > db.sql

However, I can't get it to work. Could somebody please insert examples into this so I can see how to do it?

Thanks
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Harvey is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-28-2005, 07:55 PM
AxE
AxE's Avatar
Skilled Talker

Posts: 62
Trades: 0
heres a code that will loop through your database and write a .sql file into where the script is with the contents. May take a while for larger databases though -.-

PHP Code:
    $q = @mysql_query("SHOW tables");
    $sql = "";
    while($qa = @mysql_fetch_array($q)){
        $ai = false;
        $pri = "";
        $p = "";
        $qa = $qa['0'];
        $sql .= "

-- 
-- Table structure for table `".$qa."`
-- 

CREATE TABLE `".$qa."` (";
        $d = @mysql_query("SHOW COLUMNS FROM `".$qa."`");
        while($s = @mysql_fetch_array($d)){
            if($s['NULL']==""){
                $s['2'] = "NOT NULL";
            }else{
                $s['2'] = "NULL";
            }
            $s['1'] = str_replace(" ","",$s['1']);
            if(strtolower($s['5']) != "auto_increment" && $s['1'] != 'text'){$s['4'] = " default '".$s['4']."'";}
            $s['5'] = str_replace(" ","",$s['5']);
            if(!empty($s['5'])){ $s['5'] = " ".$s['5'];}
            $l = 2;
            $sql .= "
    `".$s['0']."` ".$s['1']." ".$s['2'].$s['4'].strtolower($s['5']).","; 
            if($s['3']=="PRI"){
                $pri = "
    PRIMARY KEY (`".$s['0']."`)";
                $p = $s['0'];
            }
            if(strstr($s['5'],"auto_increment")){
                $ai = true;
            }
        }
        if(!empty($pri)){
        $sql .= $pri;
        }else{
            $sql = substr($sql,0,strlen($sql)-1);
        }
        $sql .="
) TYPE=MyISAM";
        if($ai == true){
            $j = "SELECT `id`
            FROM `".$qa."`
            ORDER BY `".$p."` DESC
            LIMIT 0,1";
            $j = @mysql_fetch_array(@mysql_query($j));
            $sql.= " AUTO_INCREMENT=".($j[$p]+1)."";
        }
        $sql .= ";

-- 
-- Dumping data for table `".$qa."`
-- 

";
        $r = @mysql_query("SELECT `*`
        FROM `".$qa."`");
        while($b = @mysql_fetch_array($r)){
            $sql.="
    INSERT INTO `".$qa."` VALUES(";
            $d = @array_values($b);
            for($i = 0; $i < count($d); $i+=2){
                @mysql_escape_string($sql.= '"'.$d[$i].'",');
            }
            $sql = substr($sql,0,strlen($sql)-1);
            $sql .= ");";
        }
    }
    $f = @fopen(getcwd()."backup.sql","w");
    @fwrite($f,$sql);
    @fclose($f);
    if(!$f){
    ?>Error writing Backup SQL.<br /><br /><?
    
    
}
That does work - I use it myself... Slightly messy code but I was just bothered about getting it made at the time.
AxE is offline
Reply With Quote
View Public Profile
 
Old 09-28-2005, 08:14 PM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
that should work harvey. Did you change the pass and user to your own username and password? Are you running it from an SSH command line?

If you have phpMyAdmin installed, you could use that. Or if you are using a control panel such as cPanel, there may be a backup option for you in there as well.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to SQL backup code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.10442 seconds with 12 queries