Debian Codegraph @

scripts/dbs_empty

raw
1 #! /usr/bin/env perl
2
3 # dbs_empty - erases any data from a given SQL database
4
5 # Copyright (C) 1999-2012 Stefan Hornburg
6
7 # Author: Stefan Hornburg (Racke) <[email protected]>
8 # Maintainer: Stefan Hornburg (Racke) <[email protected]>
9 # Version: 0.19
10
11 # This file is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by the
13 # Free Software Foundation; either version 2, or (at your option) any
14 # later version.
15
16 # This file is distributed in the hope that it will be
17 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty
18 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 # General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this file; see the file COPYING. If not, write to the Free
23 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 use strict;
26 use warnings;
27
28 use DBIx::Easy;
29 use Getopt::Long;
30 use Term::ReadKey;
31
32 # process commandline parameters
33 my %opts;
34 my $whandler = $SIG{__WARN__};
35 $SIG{__WARN__} = sub {print STDERR "$0: @_";};
36 unless (GetOptions (\%opts, 'exclude-matching-tables=s', 'tables|t')) {
37 exit 1;
38 }
39 $SIG{__WARN__} = $whandler;
40
41 # sanity check on commandline parameters
42 if (exists $opts{'exclude-matching-tables'}) {
43 eval {'' =~ /$opts{'exclude-matching-tables'}/;};
44 if ($@) {
45 $@ =~ s%at /.*$%%;
46 die "$0: $@";
47 }
48 }
49
50 my $dbif;
51 my $pwdused = 0;
52
53 my ($driver, $database, $user) = @ARGV;
54 $dbif = new DBIx::Easy ($driver, $database, $user);
55
56 # handler for DBI error messages and missing password
57 $dbif -> install_handler (\&fatal);
58
59 # we need to explicitly establish the connection
60 # for the case that a password is needed
61 $dbif -> connect;
62
63 my %fieldmap;
64
65 my ($sth, $keyfield, $update);
66 my ($table, $key, $fieldnames, @values, $headline);
67 my (@columns, $routine);
68
69 for ($dbif->tables) {
70 # skip tables preserved by user
71 if (exists $opts{'exclude-matching-tables'}) {
72 next if $table =~ /$opts{'exclude-matching-tables'}/;
73 }
74
75 if ($opts{'tables'}) {
76 # remove table from database
77 $dbif -> process ("DROP TABLE $_");
78 } else {
79 # empty table
80 $dbif -> process ("DELETE FROM $_ WHERE 1 = 1");
81 }
82 }
83
84 # ensures commit
85 undef $dbif;
86
87 # -----------------------------------
88 # FUNCTION: fatal
89 #
90 # Error handler called by DBIx::Easy.
91 # -----------------------------------
92
93 sub fatal {
94 my ($statement, $err, $msg) = @_;
95 my $pwd;
96
97 if ($dbif->is_auth_error ($err)) {
98 unless ($pwdused) {
99 print "We need a password.\n";
100 $pwd = querypwd();
101 $pwdused = 1;
102
103 # retry the connection
104 if (length ($pwd)) {
105 $dbif = new DBIx::Easy ($driver, $database, $user, $pwd);
106 $dbif -> install_handler (\&fatal);
107 $dbif -> connect ();
108 return;
109 } else {
110 die ("$statement: $msg.\n");
111 }
112 }
113 }
114 die ("$statement: $msg.\n");
115 }
116
117 # ----------------------------
118 # FUNCTION: querypwd
119 #
120 # Queries user for a password.
121 # ----------------------------
122
123 sub querypwd () {
124 my $pwd;
125
126 print "Password: ";
127 ReadMode ('noecho'); # turn echo off
128 $pwd = ReadLine (0);
129 ReadMode ('restore'); # restore terminal
130 print "\n";
131 chomp ($pwd);
132 $pwd;
133 }
134
135 # script documentation (POD style)
136
137 =head1 NAME
138
139 dbs_empty - Empty SQL Databases
140
141 =head1 DESCRIPTION
142
143 dbs_empty is an utility that B<completely erases> any data from
144 a given SQL database.
145
146 =head1 COMMAND LINE PARAMETERS
147
148 Required command line parameters are the DBI driver
149 (C<Pg> for Postgres or C<mysql> for MySQL)
150 and the database name. The third parameter is optionally
151 and specifies the database user and/or the host where the
152 database resides (C<racke>, C<[email protected]> or C<@linuxia.de>).
153
154 =head1 COMMAND LINE OPTIONS
155
156 =head2 B<--exclude-matching-tables>=REGEXP
157
158 Excludes any table matching the regular expression REGEXP from dumping.
159
160 =head2 B<-t, --tables>
161
162 Remove tables from database too.
163
164 =head1 AUTHOR
165
166 Stefan Hornburg (Racke), [email protected]
167
168 =head1 SEE ALSO
169
170 perl(1), DBIx::Easy(3)
171
172 =cut