ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I want to backup my data which is presented in remote server. For that I created .sql file on particular date by using

 mysqldump -u root -h $host $db $table --where="Date='$date'"> db_backup.sql

Due to huge data I cant take the whole backup. I succeed upto creating .sql file from remote server. Now I need to append this data to the existing data in the local server. But if you open the file(.sql) it contains

DROP TABLE IF EXISTS `information`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `information` ( `Date` date DEFAULT NULL, `Name` varchar(20) DEFAULT NULL, `Cadar` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Its dropping the existing table and creating the table again. By this I am loosing my previous data. But as per my requirement I need to "append" the data without loosing the previous data. i am dumping the data into my sql using the following command

 mysql -h***.**.*.*** -u root ravi  < db_backup.sql

I removed my ip for security reasons

To make that(append) I commented the above lines manually & run the program. Its working properly. then I tried like this

open (FILE,"+<db_backup.sql") or die "Could not open file db_backup.s +ql, $!"; open (RES,">result.sql"); my $total = 0; while (my $line = <FILE>) { $line =~ s/DROP/\/*/g; $line =~ s/CREATE/\/*/g; print RES $line; }

Due to huge data opening a file and comparing each letter and writing into another file taking too long. Is there any way to reduce my code. I am using CentOS as root user.

Thanks for helping

Replies are listed 'Best First'.
Re: Trying to append mysql dump into DB
by Corion (Patriarch) on Sep 10, 2015 at 11:49 UTC

    Consider reading the documentation for mysqldump, which also shows how to make the program only dump the data instead of recreating the tables.

    Alternatively, you could write a Perl program that parses through the SQL file and removes the unwanted statements. This would not be hard and you have already received help on similar topics.

      I replaced my code like this. Its skipping the DROP & CREATE statements in the .sql file. Now its executing in so less time. Thanks for information

       mysqldump -u root -h $host $db $table --no-create-info --where="Date='$date'"> db_backup.sql
Re: Trying to append mysql dump into DB
by choroba (Cardinal) on Sep 10, 2015 at 11:50 UTC
    To comment the line that starts with DROP TABLE IF EXISTS, just do
    perl -i~ -pe 'print "-- " if /^DROP TABLE IF EXISTS/' db_backup.sql

    • -i~ creates a backup file;
    • -p processes the file line by line, printing each line;
    • the code itself prepends -- to a line if it starts (^) with the given statement.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ