#!/usr/bin/perl #myrestore: a tool to restore data dumped by mybackup #by Marc Swanson #please note that this is not blob column safe. To restore #your blob column types, you'd need to use the "load_file" #string command on the contents of your blob field. #I'll modify this code when I get the chance. Anyone wishing #to include the mod is welcome and encouraged to send me the source :) use DBI; $dbuser = "root"; $dbpass = ""; my $base_dir = shift; if(!-d $base_dir) { print "Usage: myrestore mybackup_directory_name\n"; exit; } $dbh = DBI->connect("dbi:mysql:dbname=mysql", $dbuser, $dbpass) || die "Could not connect to database: $DBI::errstr"; if($base_dir =~ /^(.*?)\/$/) { $base_dir = $1; #strip trailing slash } opendir DIR, "$base_dir" or die "could not open $base_dir for read: $!\n"; while(defined ($dbname = readdir(DIR))) { if(-d "$base_dir/$dbname") { #this should be the name of a database print "Processing database: $dbname\n"; opendir DBDIR, "$base_dir/$dbname" or die "could not open $base_dir/$dbname for read: $!\n"; while(defined ($file = readdir(DBDIR))) { if($file =~ /(\S+)\.txt/ && -e "$base_dir/$dbname/$1.sql") { #this appears to be a valid db import file $tablename = $1; print "\tProcessing table: $tablename\n"; $query = "load data local infile '$base_dir/$dbname/$file'" . "ignore into table $dbname.$tablename fields terminated by ',' enclosed by \"'\""; $dbh->do($query); } } closedir(DBDIR); } } closedir(DIR); $dbh->disconnect()