This file is indexed.

/usr/bin/dbs_dumptabdata is in libdbix-easy-perl 0.17-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# dbs_dumptabdata - creates file set with SQL table data

# This script queries a SQL database and creates one file for each
# table in the database containing the data of the table.

# Copyright (C) 1999,2000,2001 Stefan Hornburg

# Author: Stefan Hornburg (Racke) <racke@linuxia.de>
# Maintainer: Stefan Hornburg (Racke) <racke@linuxia.de>
# Version: 0.13

# This file 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.

# This file 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 should have received a copy of the GNU General Public License
# along with this file; see the file COPYING.  If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

use strict;
use DBIx::Easy;
use FindBin;
use Getopt::Long;
use Term::ReadKey;

# process commandline parameters
my %opts;
my $printtabopts = '';
my $whandler = $SIG{__WARN__};
$SIG{__WARN__} = sub {print STDERR "$0: @_";};
unless (GetOptions (\%opts, 'exclude-matching-tables=s',
					'sort=s',
					'tables|t=s',
                    'use-printtab')) {
    exit 1;
}
$SIG{__WARN__} = $whandler;

# sanity check on commandline parameters
if (exists $opts{'exclude-matching-tables'}) {
    eval {'' =~ /$opts{'exclude-matching-tables'}/;};
    if ($@) {
        $@ =~ s%at /.*$%%;
        die "$0: $@";
    }
}

my $dbif;
my $pwdused = 0;
my $pwd;
my @tables;

my ($driver, $database, $user) = @ARGV;
$dbif = new DBIx::Easy ($driver, $database, $user);

# handler for DBI error messages and missing password
$dbif -> install_handler (\&fatal);

# we need to explicitly establish the connection
# for the case that a password is needed
$dbif -> connect;

# customize dump procedure
my $dumpproc;

if ($opts{'use-printtab'}) {
	# generic dump procedure
	$dumpproc = sub {
		my ($database, $table, $user) = @_;
        
        unless (defined $user) {$user = ''};
		print $dbif->view ($table);
	}
} else {
	# dump procedure provided by DBMS
	if ($driver eq 'mysql') {
		$dumpproc = sub {
			my ($database, $table) = @_;
    
			my $header;
            my $optstr = '';

            if ($dbif -> {USER}) {
                $optstr = " -u " . $dbif -> {USER};
            }
            if ($dbif -> {HOST}) {
                $optstr .= " -h " . $dbif -> {HOST};
            }
            if (defined $pwd) {
                $optstr .= " -p";
            }
			# -c: Use complete insert statements.
			open (DUMP, "mysqldump -t -c $optstr $database $table |")
				|| die ("$0: Couldn't launch mysqldump: $!\n");
			while (<DUMP>) {
				if ($header) {
					print;
					next;
				}
				# skip header
				$header = 1 unless (/\S/);
			}
			close (DUMP)
				|| die ("$0: mysqldump execution errors\n");
		}
	} elsif ($driver eq 'Pg') {
		$dumpproc = sub {
			my ($database, $table) = @_;

			# -D => dump data as inserts with attribute names
			open (DUMP, "pg_dump $database -a -D -t $table |")
				|| die ("$0: Couldn't launch pg_dump: $!\n");
			while (<DUMP>) {
				print;
			}
			close (DUMP)
				|| die ("$0: pg_dump execution errors\n");
		}
	}    
}

if ($opts{'tables'}) {
    @tables = split (/,/, $opts{'tables'});
} else {
    @tables = $dbif->tables ();
}

foreach my $table (@tables) {
    my $outfile = lc($table) . '.sql';

    # skip tables unwanted by user
    if (exists $opts{'exclude-matching-tables'}) {
        next if $table =~ /$opts{'exclude-matching-tables'}/;
    }
    # call driver dependent dump procedure and fill
    # output file with the results
    open (OUT, ">$outfile") || die ("$0: Couldn't open $outfile: $!\n");
    select (OUT);
    &$dumpproc ($database, $table, $user);
    close (OUT);
}

# Destroy Database Object as written in manpage
undef $dbif;

# -----------------------------------
# FUNCTION: fatal
#
# Error handler called by DBIx::Easy.
# -----------------------------------

sub fatal {
  my ($statement, $err, $msg) = @_;

  if ($dbif->is_auth_error ($err)) {
    unless ($pwdused) {
      print "We need a password.\n";
      $pwd = querypwd();
      $pwdused = 1;
    
      # retry the connection
      if (length ($pwd)) {
        $dbif = new DBIx::Easy ($driver, $database, $user, $pwd);
        $dbif -> install_handler (\&fatal);
        $dbif -> connect ();
        return;
      } else {
        die ("$statement.\n");
      }
    }
  }
  die ("$statement.\n");
}

# ----------------------------
# FUNCTION: querypwd
#
# Queries user for a password.
# ----------------------------

sub querypwd () {
  my $pwd;

  print "Password: ";
  ReadMode ('noecho');  # turn echo off
  $pwd = ReadLine (0);
  ReadMode ('restore'); # restore terminal
  print "\n";
  chomp ($pwd);
  $pwd;
}

=head1 NAME

dbs_dumptabdata - Creates file set with SQL table data

=head1 DESCRIPTION

dbs_dumptabdata is an utility to create a file set with SQL table data.
For each table in the database dbs_dumptabdata calls the appropriate dumper
utility with the output directed to a file named I<table>.sql in the
current directory. dbs_dumptabdata asks for a password if necessary.

=head1 COMMAND LINE PARAMETERS

Required command line parameters are the DBI driver
(C<Pg> for Postgres or C<mysql> for MySQL)
and the database name. The third parameter is optionally
and specifies the database user and/or the host where the
database resides (C<racke>, C<racke@linuxia.de> or C<@linuxia.de>).

=head1 COMMAND LINE OPTIONS

=head2 B<-t> TABLE[,TABLE,...], B<--tables>=TABLE[,TABLE,...]

Comma-separated list of tables to dump.

=head2 B<--exclude-matching-tables>=REGEXP

Excludes any table matching the regular expression REGEXP from dumping.

=head2 B<--use-printtab>

Uses generic dump (similar to C<dbs_printtab>) instead of the dumper
utility provided by the DBMS.

=head1 BUGS

msql is not supported.

=head1 AUTHOR

Stefan Hornburg (Racke), racke@linuxia.de

=head1 SEE ALSO

perl(1), DBIx::Easy(3)

=cut