This little script will aid in your database backup. It will copy the data from one database to another, but the table definitions must be present in the empty DB. Currently only supports mysql platform. Tested -- DBI 1.14, mysql 3.23.43, Redhat 2.2.19 Enjoy!
#!/usr/local/bin/perl ####################### # dbCopy.pl # Version 1.0 # This utility can backup the data from one mysql database to another. # It requires that the table definitions on your production # database + are in place in the backup database. # TODO: # 1. Parse Table Definitions to issue create table # statements to backup db. # 2. Extend to other DB Platforms # 3. Write in Package Format, possibly as DBI::Utils or something ###################### use DBI; main(); # Initialize values for DBI connection. # $dbHref is the DB to be backed up [eg, production]. # $dbBakHref is the backup database # Change the hash values to your own needs. sub initDBHash { my ($dbHRef,$dbBakHRef) = @_; %$dbHRef = ( dbName => "dbName", dbUser => "dbUser", dbPWord => "dbPass" ); %$dbBakHRef = ( dbName => "dbBakName", dbUser => "dbBakUser", dbPWord => "dbBakPass" ); } # Create DBI object in the accepted hash ref. sub dbConnect { my $hRef = shift; $$hRef{connect}=DBI->connect ("dbi:mysql:$$hRef{dbName}", "$$hRef{dbUser}","$$hRef{dbPWord}", {AutoCommit=>1, PrintError=>0}); } sub main { my (%dbHash,%dbBakHash); initDBHash (\%dbHash,\%dbBakHash); #Connect to each DB for (\%dbHash,\%dbBakHash) { dbConnect($_); } #Get Table Names my $tableRefs = getData ("show tables",\%dbHash); #Loop through each table foreach my $table (@$tableRefs) { #Get data from current table my $rowRefs = getData ("select * from $$table[0]",\%dbHash); #Build bind string, using map to get correct number of ?'s -- ?,?, +?,?..... my $str = join (",",map {"?"}(0..$#{$rowRefs->[0]})); #Create Handle for DBBackup insert, using $str bind values. my $insert = $dbBakHash{connect}->prepare ("insert into $$tabl +e[0] values ($str)"); #Execute the insertion over each row. foreach my $row (@$rowRefs) { $insert->execute (@$row); } } #Disconnect $dbHash{connect}->disconnect(); $dbBakHash{connect}->disconnect(); } #Prepare,execute,and retrieve data from SQL statement. sub getData { my ($sql,$dbHash) = @_; my $query = $$dbHash{connect}->prepare("$sql"); $query->execute(); my $rows = $query->fetchall_arrayref(); $rows; }

In reply to dbCopy.pl by abaxaba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.