ensembl-hive  2.7.0
cleanup_tmp_tables.pl
Go to the documentation of this file.
1 #!/usr/bin/env perl
2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 =head1 NAME
19 
20 cleanup_tmp_tables.pl - delete temporary and backup tables from a database
21 
22 =head1 SYNOPSIS
23 
24  ./cleanup_tmp_tables.pl [arguments]
25 
26  ./cleanup_tmp_tables.pl --nolog --host localhost --port 3306 --user user --dbname DB --dry_run
27 
28  ./cleanup_tmp_tables.pl --nolog --host localhost --port 3306 --user user --dbname '%mydbs%' --dry_run
29 
30  ./cleanup_tmp_tables.pl --nolog --host localhost --port 3306 --user user --dbname DB --interactive 0
31 
32 Required arguments:
33 
34  --dbname, db_name=NAME database name NAME (can be a pattern)
35  --host, --dbhost, --db_host=HOST database host HOST
36  --port, --dbport, --db_port=PORT database port PORT
37  --user, --dbuser, --db_user=USER database username USER
38  --pass, --dbpass, --db_pass=PASS database passwort PASS
39 
40 Optional arguments:
41 
42  --mart Indicates we wish to search for mart
43  temporary tables which are normally
44  prefixed with MTMP_.
45  ONLY RUN IF YOU ARE A MEMBER OF
46  THE PRODUCTION TEAM
47 
48  --conffile, --conf=FILE read parameters from FILE
49  (default: conf/Conversion.ini)
50 
51  --logfile, --log=FILE log to FILE (default: *STDOUT)
52  --logpath=PATH write logfile to PATH (default: .)
53  --logappend, --log_append append to logfile (default: truncate)
54 
55  -v, --verbose=0|1 verbose logging (default: false)
56  -i, --interactive=0|1 run script interactively (default: true)
57  -n, --dry_run, --dry=0|1 don't write results to database
58  -h, --help, -? print help (this message)
59 
60 =head1 DESCRIPTION
61 
62 A script which looks for any table which we believe could be a temporary
63 table. This means any table which contains
64 
65 =over 8
66 
67 =item bak
68 
69 =item backup
70 
71 =item MTMP_ (only used when --mart is specified)
72 
73 =back
74 
75 You can run this over multiple DBs but caution is advised
76 
77 
78 =head1 AUTHOR
79 
80 Ensembl core API team
81 
82 =head1 CONTACT
83 
84 Please post comments/questions to the Ensembl development list
85 <http://lists.ensembl.org/mailman/listinfo/dev>
86 
87 =cut
88 
89 use strict;
90 use warnings;
91 no warnings 'uninitialized';
92 
93 use FindBin qw($Bin);
94 use vars qw($SERVERROOT);
95 
96 BEGIN {
97  $SERVERROOT = "$Bin/../..";
98  unshift(@INC, "$SERVERROOT/ensembl/modules");
99  unshift(@INC, "$SERVERROOT/bioperl-live");
100 }
101 
102 use Getopt::Long;
103 use Pod::Usage;
104 use Bio::EnsEMBL::Utils::ConversionSupport;
105 
106 $| = 1;
107 
108 my $support = new Bio::EnsEMBL::Utils::ConversionSupport($SERVERROOT);
109 
110 # parse options
111 $support->parse_common_options(@_);
112 $support->parse_extra_options(qw/mart!/);
113 $support->allowed_params(
114  $support->get_common_params, 'mart'
115 );
116 
117 if ($support->param('help') or $support->error) {
118  warn $support->error if $support->error;
119  pod2usage(1);
120 }
121 
122 # ask user to confirm parameters to proceed
123 $support->confirm_params;
124 
125 # get log filehandle and print heading and parameters to logfile
126 $support->init_log;
127 
128 $support->check_required_params;
129 
130 my @databases;
131 
132 # connect to database
133 my $dbh;
134 my $original_dbname = $support->param('dbname');
135 if($original_dbname =~ /%/) {
136  $support->param('dbname', q{});
137  $dbh = $support->get_dbconnection('');
138  my $ref = $dbh->selectall_arrayref('show databases like ?', {}, $original_dbname);
139  push(@databases, map {$_->[0]} @{$ref})
140 }
141 else {
142  $dbh = $support->get_dbconnection('');
143  push(@databases, $original_dbname);
144 }
145 
146 my @patterns = map { '%\\_'.$_.'%' } qw/bak backup/;
147 if($support->param('mart')) {
148  if($support->user_proceed('--mart was specified on the command line. Do not run this during a mart build. Do you wish to continue?')) {
149  push(@patterns, 'MTMP\\_%');
150  }
151 }
152 
153 foreach my $db (@databases) {
154  my %tables;
155  $support->log('Switching to '.$db."\n");
156  $dbh->do('use '.$db);
157  foreach my $pattern (@patterns) {
158  my $ref = $dbh->selectall_arrayref('show tables like ?', {}, $pattern);
159  $tables{$_->[0]} = 1 for @{$ref};
160  }
161 
162  my @tables = sort keys %tables;
163 
164  if ($support->param('dry_run')) {
165  # for a dry run, only show which databases would be deleted
166  if(scalar(@tables) > 0) {
167  $support->log("Temporary and backup tables found:\n");
168  foreach my $table (@tables) {
169  $support->log("$table\n", 1);
170  }
171  }
172 
173  } else {
174  # delete tables
175  foreach my $table (@tables) {
176  if ($support->user_proceed("Drop table $table?")) {
177  $support->log("Dropping table $table...\n");
178  $dbh->do("DROP TABLE $table");
179  $support->log("Done.\n");
180  }
181  }
182  }
183 }
184 # finish logfile
185 $support->finish_log;
186 
run
public run()