#!/usr/bin/perl -w ## ## ==================================================================== ## (c) Copyright by Karl Heinz Marbaise 2004 ## ## :::::::::::: Partial Database Dump Utility (PDBDump) ::::::::::::::: ## -------------------------------------------------------------------- ## ## (C) 2004 by by Karl-Heinz Marbaise ## ## pdbdump.pl is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## pdbdump.pl is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You may have received a copy of the GNU General Public License ## along with pdbdump.pl; see the file COPYING. If not, write to the ## Free Software Foundation, Inc., 59 Temple Place - Suite 330, ## Boston, MA 02111-1307, USA. ## ## $Id: pdbdump.pl,v 1.1 2004/03/26 19:01:41 kama Exp $ ## ## =head1 Partial Database Dump (PDBDump) (currently only MySQL is supported!) Just dump tables with the given pattern =cut # use DBI; use strict; # # Command line argument analyzing use Getopt::Long; use Getopt::Std; # # Debugging puposes use Data::Dumper; # # # In case we have to print it out: my $VERSION = '$Revision: 1.1 $'; $VERSION =~ s/\S+\s+(\S+)\s+\S+/$1/; # # Commandline Options # my $OptHelp = 0; my $OptVersion = 0; my $OptVerbose = 0; # my $OptDBType = 'mysql'; my $OptDBHost = 'localhost'; my $OptDBUser = ''; my $OptDBPassword = ''; my $OptDBDatabase = ''; my $OptDBTables = '.*'; my $OptNoData= 0; # # Read Option of command line &GetOptions ( "help" => \$OptHelp, "version" => \$OptVersion, "verbose" => \$OptVerbose, "db=s" => \$OptDBDatabase, "host=s" => \$OptDBHost, "type=s" => \$OptDBType, "user=s" => \$OptDBUser, "pass=s" => \$OptDBPassword, "tables=s" => \$OptDBTables, "no-data" => \$OptNoData, ); # # sub Version () { print "pdbdump.pl version ${VERSION}; distributed under the GNU GPL.\n"; } # # sub Usage () { print <<'END_OF_INFO'; NAME: pdbdump.pl -- Partial Database Dump Utilitly (PDBDump) SYNOPSIS: pdbdump.pl [OPTIONS] outputfile DESCRIPTION: Dump tables of a database using a RegEx to select which tables will be dump and which not. ATTENTION: (Currently only MySQL is supported!) OPTIONS: --help Print out Help --verbose produce more output while running...output will be sent to STDERR instead of STDOUT. --version Print out the version of the Script --type=### Kind of database (e.g. mysql, postgresql etc.) currently only MySQL supported. --db=### Name of the database. --host=### Name of the host; default localhost --user=### Name of the database user --pass=## Password for the database. If not given no password will be given to mysqldump. --tables=## regex with the name of the tables which should be dumped. --no-data No data will be dumped (just structure of database) AUTHOR: Karl-Heinz Marbaise REPORTING BUGS: If you found a bug or have suggestions to improve the given utility just write an email to me. Such kind of feedback is apreciated. COPYRIGHT: (c) by Karl-Heinz Marbaise 2004 under GNU GPL Version 2 or later. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. EXAMPLE: pdbdump.pl --db=test --user=root --table='^gacl_.*|tcms_user' outputfilename Will dump all tables which name starts with gacl and tcms_user table will be dumpe either. END_OF_INFO } # # # sub aReadTableNamesMySQL { my ($oDBH) = @_; my $oHStatement = $oDBH->prepare("SHOW TABLES"); my @aResult = (); $oHStatement->execute (); while (my @aRow = $oHStatement->fetchrow_array) { push @aResult, @aRow; } return @aResult; } # # # Creation of the mysqldump commandline using the different # Options.... # MySQLDump # sub aGetCommandLineMySQL { my ($sOutputFileName, @aMatcheTableNames) = @_; my @aArgs = ("mysqldump"); push @aArgs, '--complete-insert'; push @aArgs, '--no-data' if ($OptNoData); push @aArgs, '--host=' . $OptDBHost; push @aArgs, '--user=' . $OptDBUser if ($OptDBUser); push @aArgs, '--password=\'' . $OptDBPassword . '\'' if ($OptDBPassword); push @aArgs, $OptDBDatabase; push @aArgs, @aMatcheTableNames; push @aArgs, '--result-file=' . $sOutputFileName if ($sOutputFileName); print STDERR 'Commandline: ' . join (' ', @aArgs) . "\n" if ($OptVerbose); return @aArgs; } # sub aGetMatchedTables { my ($sPattern, @aTableNames) = @_; my @aResult = (); foreach my $sTableName (@aTableNames) { push @aResult, $sTableName if ($sTableName =~ /$sPattern/); } return @aResult; } # # # ============================================================================= # ======================= M A I N ============================================= # ============================================================================= # # if ($OptHelp) { Usage (); exit 1; } # if ($OptVersion) { Version (); exit 1; } # if (!$OptDBDatabase) { print STDERR "You have to give a database name!\n"; exit 1; } # dbi:DriverName:database=database_name;host=hostname;port=port my $oDBH = DBI->connect ( 'DBI:' . $OptDBType . ':database=' . $OptDBDatabase . ';' . 'host=' . $OptDBHost ); my @aTableNames = aReadTableNamesMySQL ($oDBH); print STDERR "The Tables which are in the database:\n" . Dumper (\@aTableNames) if ($OptVerbose); my @aMatchedTableNames = aGetMatchedTables ($OptDBTables, @aTableNames); print STDERR "The matched table names in the database:\n" . Dumper (\@aMatchedTableNames) if ($OptVerbose);; # end database connection $oDBH->disconnect; # Create the commandline for calling mysqldump (currently) my @aArgs = aGetCommandLineMySQL ($ARGV[0], @aMatchedTableNames); system(@aArgs) == 0 or die "system @aArgs failed: $?"; # everthing fine.... exit 0;