Restore Backup

Discussion in 'PHP' started by gigamike, Jun 4, 2007.

  1. #1
    Guys,

    Please, i need your help. I created a module which is a backup and restore. The strategy i did was im extracting data from mysql and save it to a textfile. The format of text inside the textfile is SQL statement or exactly the same with the EXPORT result of phpmyadmin. Now my problem is with restoring. Im reading the textfile and i used semi-colon as a separator of the SQL statements. Example

    insert into CountryCodes (ccode, cname, countrySHORT) values (964, "Iraq", "IQ");
    insert into CountryCodes (ccode, cname, countrySHORT) values (353, "Ireland (Irish Republic; Eire)", "IE");

    the first insert was successful but the second is not because of the semicolon near Irish Republic value. Any idea on what should i do?

    Thanks again,

    Mike
     
    gigamike, Jun 4, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?
    $read = fopen( 'backup.sql', 'r' );
    
    while( !feof( $read ) )
    {
    	$sql = fgets( $read, 4096 ) ;
    	
    	if( !mysql_query( $sql ) )
    	{
    		die( sprintf( 'mysql_query( "%s" ) failed', $sql ) );		
    	}
    	else
    	{
    		printf("%s\r\n", $sql );
    	}
    }
    
    fclose( $read );
    ?>
    
    PHP:
     
    krakjoe, Jun 4, 2007 IP
  3. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Hi Krackjoe,

    Thanks for your quick help. Im just wondering, what will be your delimiter let say the textfile contains 10 or more insert statement of lines. i think mysql_query cant query many insert statement in 1 call, example

    $sql="INSERT INTO table(val) VALUE(1); INSERT INTO table(val) VALUE(2);";
    mysql_query($sql);

    Thanks,

    Mike

     
    gigamike, Jun 4, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    There is no need to delimit anything, it will read one line at a time and execute it, fgets automatically reads upto a newline ( \n ) OR until length bytes have been read - whichever comes first.
     
    krakjoe, Jun 4, 2007 IP
  5. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    thanks man, ill try it.

     
    gigamike, Jun 5, 2007 IP
  6. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Hi Guys,

    still no luck, i cant restore my generated sql textfile

    http://gigamike.net/test01.txt

    it seems my textfile created same as the output of phpmyadmin export. I cant figure it out on how i can restore it without using any delimiters.

    Please help guys,

    Mike

     
    gigamike, Jun 6, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #7
    that's because the CREATE TABLE statements aren't on one line.

    why can't you just use phpmyadmin to restore it ??

    alternatively, use preg_replace() to remove comments and execute it as one statement, assuming your server allows you to upload files larger than the backup
     
    krakjoe, Jun 6, 2007 IP
  8. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    Hi krakjoe,

    it works fine now, i removed the\n\r and comments since im the one generating the backup textfile.

    I want to create my own backup and restore utility inorder for my admin to use it without the coder help.

    Thanks again,

    Mike

     
    gigamike, Jun 10, 2007 IP