Debian Codegraph @

scripts/dbs_dumptabstruct

raw
1 #! /usr/bin/env perl
2
3 # dbs_dumptabstruct - creates file set with SQL table schemas
4
5 # This script queries a SQL database and creates one file for each
6 # table in the database containing the schema 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 Getopt::Long;
33 use Term::ReadKey;
34
35 # process commandline parameters
36 my %opts;
37 my $whandler = $SIG{__WARN__};
38 $SIG{__WARN__} = sub {print STDERR "$0: @_";};
39 unless (GetOptions (\%opts, 'dump-options|d|o=s', 'exclude-matching-tables=s',
40 'filter', 'pipe|p', 'tables|t=s')) {
41 exit 1;
42 }
43 $SIG{__WARN__} = $whandler;
44
45 # sanity check on commandline parameters
46 if (exists $opts{'exclude-matching-tables'}) {
47 eval {'' =~ /$opts{'exclude-matching-tables'}/;};
48 if ($@) {
49 $@ =~ s%at /.*$%%;
50 die "$0: $@";
51 }
52 }
53
54 my $dbif;
55 my $pwdused = 0;
56 my $pwd;
57 my @tables;
58
59 my ($driver, $database, $user) = @ARGV;
60 $dbif = new DBIx::Easy ($driver, $database, $user);
61
62 # handler for DBI error messages and missing password
63 $dbif -> install_handler (\&fatal);
64
65 # we need to explicitly establish the connection
66 # for the case that a password is needed
67 $dbif -> connect;
68
69 # dump procedures for the different drivers
70 my $dumpproc;
71 if ($driver eq 'mysql') {
72 if ($opts{filter}) {
73 $dumpproc = \&kvfilter;
74 } else {
75 $dumpproc = sub {
76 my ($database, $table) = @_;
77
78 my $header;
79 my $optstr = '';
80
81 if ($dbif -> {USER}) {
82 $optstr = " -u " . $dbif -> {USER};
83 }
84 if ($dbif -> {HOST}) {
85 $optstr .= " -h " . $dbif -> {HOST};
86 }
87 if (defined $pwd) {
88 $optstr .= " -p";
89 }
90 if ($opts{'dump-options'}) {
91 $optstr .= " $opts{'dump-options'}";
92 }
93 open (DUMP, qq{mysqldump -d $optstr "$database" "$table" |})
94 || die ("$0: Couldn't launch mysqldump: $!\n");
95 while (<DUMP>) {
96 if ($header) {
97 print;
98 next;
99 }
100 # skip header
101 $header = 1 unless (/\S/);
102 }
103 close (DUMP)
104 || die ("$0: mysqldump execution errors\n");
105 }
106 }
107 } elsif ($driver eq 'Pg') {
108 if ($opts{filter}) {
109 die "$0: --filter option not supported for PostgreSQL databases\n";
110 } else {
111 $dumpproc = sub {
112 my ($database, $table) = @_;
113
114 my $optstr = '';
115
116 if ($opts{'dump-options'}) {
117 $optstr = " $opts{'dump-options'}";
118 }
119 open (DUMP, qq{pg_dump "$database" -x -s $optstr -t "$table" |})
120 || die ("$0: Couldn't launch pg_dump: $!\n");
121 while (<DUMP>) {
122 print;
123 }
124 close (DUMP)
125 || die ("$0: pg_dump execution errors\n");
126 }
127 }
128 }
129
130 if ($opts{'tables'}) {
131 @tables = split (/,/, $opts{'tables'});
132 } else {
133 @tables = ($dbif->tables(), $dbif->sequences());
134 }
135
136 foreach my $table (@tables) {
137 my $outfile = lc($table) . '.sql';
138 my $regexp;
139
140 # skip tables unwanted by user
141 if (exists $opts{'exclude-matching-tables'}) {
142 next if $table =~ /$opts{'exclude-matching-tables'}/;
143 }
144 # call driver dependent dump procedure and fill
145 # output file with the results
146 unless ($opts{'pipe'}) {
147 open (OUT, ">$outfile") || die ("$0: Couldn't open $outfile: $!\n");
148 select (OUT);
149 }
150 &$dumpproc ($database, $table);
151 unless ($opts{'pipe'}) {
152 close (OUT);
153 }
154 select (STDOUT);
155 }
156 # Destroy Database object as written in manpage
157 undef $dbif;
158
159 # -----------------------------------
160 # FUNCTION: fatal
161 #
162 # Error handler called by DBIx::Easy.
163 # -----------------------------------
164
165 sub fatal {
166 my ($statement, $err, $msg) = @_;
167
168 if ($dbif->is_auth_error ($err)) {
169 unless ($pwdused) {
170 print "We need a password.\n";
171 $pwd = querypwd();
172 $pwdused = 1;
173
174 # retry the connection
175 if (length ($pwd)) {
176 $dbif = new DBIx::Easy ($driver, $database, $user, $pwd);
177 $dbif -> install_handler (\&fatal);
178 $dbif -> connect ();
179 return;
180 } else {
181 die ("$statement.\n");
182 }
183 }
184 }
185 die ("$statement.\n");
186 }
187
188 # ----------------------------
189 # FUNCTION: querypwd
190 #
191 # Queries user for a password.
192 # ----------------------------
193
194 sub querypwd () {
195 my $pwd;
196
197 print "Password: ";
198 ReadMode ('noecho'); # turn echo off
199 $pwd = ReadLine (0);
200 ReadMode ('restore'); # restore terminal
201 print "\n";
202 chomp ($pwd);
203 $pwd;
204 }
205
206 # FUNCTION: kvfilter
207 sub kvfilter {
208 my ($database, $table) = @_;
209 my $dbif = new DBIx::Easy ('mysql', $database);
210 my $sth = $dbif->process("describe $table");
211 my $row;
212 print join ("\t", @{$sth->{NAME}}), "\n";
213 while ($row = $sth->fetch()) {
214 print join("\t", map {$_ || ''} @$row), "\n";
215 }
216 }
217
218 =head1 NAME
219
220 dbs_dumptabstruct - Creates file set with SQL table schemas
221
222 =head1 DESCRIPTION
223
224 dbs_dumptabstruct is an utility to create a file set with SQL table schemas.
225 For each table in the database dbs_dumptabstruct calls the appropriate dumper
226 utility with the output directed to a file named I<table>.sql in the
227 current directory. dbs_dumptabstruct asks for a password if necessary.
228
229 =head1 COMMAND LINE PARAMETERS
230
231 Required command line parameters are the DBI driver
232 (C<Pg> for Postgres or C<mysql> for MySQL)
233 and the database name. The third parameter is optionally
234 and specifies the database user and/or the host where the
235 database resides (C<racke>, C<[email protected]> or C<@linuxia.de>).
236
237 =head1 COMMAND LINE OPTIONS
238
239 =head2 B<-d>=OPTIONS, B<-o>=OPTIONS, B<--dump-options>=OPTIONS
240
241 Pass options to the dumper utility, e.g. C<--compatible=mysql40>.
242
243 =head2 B<-p>, B<--pipe>
244
245 Prints the table dumps to standard output.
246
247 =head2 B<-t> TABLE[,TABLE,...], B<--tables>=TABLE[,TABLE,...]
248
249 Comma-separated list of tables to dump.
250
251 =head2 B<--exclude-matching-tables>=REGEXP
252
253 Excludes any table matching the regular expression REGEXP from dumping.
254
255 =head1 BUGS
256
257 Only mysql and Pg drivers are supported.
258
259 =head1 AUTHOR
260
261 Stefan Hornburg (Racke), [email protected]
262
263 =head1 VERSION
264
265 0.17
266
267 =head1 SEE ALSO
268
269 perl(1), DBIx::Easy(3)
270
271 =cut