Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

gav
  (email not shown publicly)
http://www.estey.com/
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Saturday March 20, 2004
04:59 PM

MySQL Dump Files

[ #17983 ]

MySQL Tip of the Day: make sure you're keeping regular backups of your data (with mysqldump ) in case you do something stupid and delete a whole bunch of data.

This little script takes a dump file and extracts the SQL INSERT statements into a new file for the table you specify. It's handy if you want to restore just a single table without deleting the existing data in it.

#!/usr/bin/perl -w
use strict;
use Getopt::Long;
GetOptions(
    'file=s' => \my $fn_in, 'out=s' => \my $fn_out, 'table=s' => \my $table
);
unless ($fn_in && $table) {
    die "Usage: $0 --file=<filename> --table=<table> [--out=<output_file>]\n"
}
$fn_out ||= "$fn_in.$table";
open my $in, '<', $fn_in or die "Fatal: cannot open '$fn_in': $!\n";
open my $out, '>', $fn_out or die "Fatal: cannot write to '$fn_out': $!\n";
my $write;
while (<$in>) {
    if ($write) {
        last if /^UNLOCK TABLES/;
        print {$out} $_;
    } else {
        $write = 1 if /^LOCK TABLES $table/;
    }
}
close $in;
close $out;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.