Debian Codegraph @

scripts/dbs_dumptabdata

raw
1 #! /usr/bin/env perl
2
3 # dbs_dumptabdata - creates file set with SQL table data
4
5 # This script queries a SQL database and creates one file for each
6 # table in the database containing the data of the table.
7
8 # Copyright (C) 1999-2012 Stefan Hornburg
9
10 # Author: Stefan Hornburg (Racke) <[email protected]>
11 # Maintainer: Stefan Hornburg (Racke) <[email protected]>
12 # Version: 0.19
13
14 # This file is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by the
16 # Free Software Foundation; either version 2, or (at your option) any
17 # later version.
18
19 # This file is distributed in the hope that it will be
20 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
23
24 # You should have received a copy of the GNU General Public License
25 # along with this file; see the file COPYING. If not, write to the Free
26 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27
28 use strict;
29 use warnings;
30
31 use DBIx::Easy;
32 use FindBin;
33 use Getopt::Long;
34 use Term::ReadKey;
35
36 # process commandline parameters
37 my %opts;
38 my $printtabopts = '';
39 my $whandler = $SIG{__WARN__};
40 $SIG{__WARN__} = sub {print STDERR "$0: @_";};
41 unless (GetOptions (\%opts, 'exclude-matching-tables=s',
42 'sort=s',
43 'tables|t=s',
44 'use-printtab')) {
45 exit 1;
46 }
47 $SIG{__WARN__} = $whandler;
48
49 # sanity check on commandline parameters
50 if (exists $opts{'exclude-matching-tables'}) {
51 eval {'' =~ /$opts{'exclude-matching-tables'}/;};
52 if ($@) {
53 $@ =~ s%at /.*$%%;
54 die "$0: $@";
55 }
56 }
57
58 my $dbif;
59 my $pwdused = 0;
60 my $pwd;
61 my @tables;
62
63 my ($driver, $database, $user) = @ARGV;
64 $dbif = new DBIx::Easy ($driver, $database, $user);
65
66 # handler for DBI error messages and missing password
67 $dbif -> install_handler (\&fatal);
68
69 # we need to explicitly establish the connection
70 # for the case that a password is needed
71 $dbif -> connect;
72
73 # customize dump procedure
74 my $dumpproc;
75
76 if ($opts{'use-printtab'}) {
77 # generic dump procedure
78 $dumpproc = sub {
79 my ($database, $table, $user) = @_;
80
81 unless (defined $user) {$user = ''};
82 print $dbif->view ($table);
83 }
84 } else {
85 # dump procedure provided by DBMS
86 if ($driver eq 'mysql') {
87 $dumpproc = sub {
88 my ($database, $table) = @_;
89
90 my $header;
91 my $optstr = '';
92
93 if ($dbif -> {USER}) {
94 $optstr = " -u " . $dbif -> {USER};
95 }
96 if ($dbif -> {HOST}) {
97 $optstr .= " -h " . $dbif -> {HOST};
98 }
99 if (defined $pwd) {
100 $optstr .= " -p";
101 }
102 # -c: Use complete insert statements.
103 open (DUMP, "mysqldump -t -c $optstr $database $table |")
104 || die ("$0: Couldn't launch mysqldump: $!\n");
105 while (<DUMP>) {
106 if ($header) {
107 print;
108 next;
109 }
110 # skip header
111 $header = 1 unless (/\S/);
112 }
113 close (DUMP)
114 || die ("$0: mysqldump execution errors\n");
115 }
116 } elsif ($driver eq 'Pg') {
117 $dumpproc = sub {
118 my ($database, $table) = @_;
119
120 # -D => dump data as inserts with attribute names
121 open (DUMP, "pg_dump $database -a -D -t $table |")
122 || die ("$0: Couldn't launch pg_dump: $!\n");
123 while (<DUMP>) {
124 print;
125 }
126 close (DUMP)
127 || die ("$0: pg_dump execution errors\n");
128 }
129 }
130 }
131
132 if ($opts{'tables'}) {
133 @tables = split (/,/, $opts{'tables'});
134 } else {
135 @tables = $dbif->tables ();
136 }
137
138 foreach my $table (@tables) {
139 my $outfile = lc($table) . '.sql';
140
141 # skip tables unwanted by user
142 if (exists $opts{'exclude-matching-tables'}) {
143 next if $table =~ /$opts{'exclude-matching-tables'}/;
144 }
145 # call driver dependent dump procedure and fill
146 # output file with the results
147 open (OUT, ">$outfile") || die ("$0: Couldn't open $outfile: $!\n");
148 select (OUT);
149 &$dumpproc ($database, $table, $user);
150 close (OUT);
151 }
152
153 # Destroy Database Object as written in manpage
154 undef $dbif;
155
156 # -----------------------------------
157 # FUNCTION: fatal
158 #
159 # Error handler called by DBIx::Easy.
160 # -----------------------------------
161
162 sub fatal {
163 my ($statement, $err, $msg) = @_;
164
165 if ($dbif->is_auth_error ($err)) {
166 unless ($pwdused) {
167 print "We need a password.\n";
168 $pwd = querypwd();
169 $pwdused = 1;
170
171 # retry the connection
172 if (length ($pwd)) {
173 $dbif = new DBIx::Easy ($driver, $database, $user, $pwd);
174 $dbif -> install_handler (\&fatal);
175 $dbif -> connect ();
176 return;
177 } else {
178 die ("$statement.\n");
179 }
180 }
181 }
182 die ("$statement.\n");
183 }
184
185 # ----------------------------
186 # FUNCTION: querypwd
187 #
188 # Queries user for a password.
189 # ----------------------------
190
191 sub querypwd () {
192 my $pwd;
193
194 print "Password: ";
195 ReadMode ('noecho'); # turn echo off
196 $pwd = ReadLine (0);
197 ReadMode ('restore'); # restore terminal
198 print "\n";
199 chomp ($pwd);
200 $pwd;
201 }
202
203 =head1 NAME
204
205 dbs_dumptabdata - Creates file set with SQL table data
206
207 =head1 DESCRIPTION
208
209 dbs_dumptabdata is an utility to create a file set with SQL table data.
210 For each table in the database dbs_dumptabdata calls the appropriate dumper
211 utility with the output directed to a file named I<table>.sql in the
212 current directory. dbs_dumptabdata asks for a password if necessary.
213
214 =head1 COMMAND LINE PARAMETERS
215
216 Required command line parameters are the DBI driver
217 (C<Pg> for Postgres or C<mysql> for MySQL)
218 and the database name. The third parameter is optionally
219 and specifies the database user and/or the host where the
220 database resides (C<racke>, C<[email protected]> or C<@linuxia.de>).
221
222 =head1 COMMAND LINE OPTIONS
223
224 =head2 B<-t> TABLE[,TABLE,...], B<--tables>=TABLE[,TABLE,...]
225
226 Comma-separated list of tables to dump.
227
228 =head2 B<--exclude-matching-tables>=REGEXP
229
230 Excludes any table matching the regular expression REGEXP from dumping.
231
232 =head2 B<--use-printtab>
233
234 Uses generic dump (similar to C<dbs_printtab>) instead of the dumper
235 utility provided by the DBMS.
236
237 =head1 BUGS
238
239 msql is not supported.
240
241 =head1 AUTHOR
242
243 Stefan Hornburg (Racke), [email protected]
244
245 =head1 SEE ALSO
246
247 perl(1), DBIx::Easy(3)
248
249 =cut