2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
7 # <sales@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
31 # CONTRIBUTION SUBMISSION POLICY:
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
48 # END BPS TAGGED BLOCK }}}
51 rt-shredder - Script which wipe out tickets from RT DB
55 rt-shredder --plugin list
56 rt-shredder --plugin help-Tickets
57 rt-shredder --plugin 'Tickets=query,Queue="general" and Status="deleted"'
59 rt-shredder --sqldump unshred.sql --plugin ...
60 rt-shredder --force --plugin ...
64 rt-shredder - is script that allow you to wipe out objects
65 from RT DB. This script uses API that L<RT::Shredder> module adds to RT.
66 Script can be used as example of usage of the shredder API.
70 You can use several options to control which objects script
75 =head2 --sqldump <filename>
77 Outputs INSERT queries into file. This dump can be used to restore data
80 By default creates files
81 F<< <RT_home>/var/data/RT-Shredder/<ISO_date>-XXXX.sql >>
83 =head2 --object (DEPRECATED)
85 Option has been deprecated, use plugin C<Objects> instead.
87 =head2 --plugin '<plugin name>[=<arg>,<val>[;<arg>,<val>]...]'
89 You can use plugins to select RT objects with various conditions.
90 See also --plugin list and --plugin help options.
94 Output list of the available plugins.
96 =head2 --plugin help-<plugin name>
98 Outputs help for specified plugin.
102 Script doesn't ask any questions.
111 use warnings FATAL => 'all';
113 # fix lib paths, some may be relative
116 my @libs = ("@RT_LIB_PATH@", "@LOCAL_LIB_PATH@");
119 for my $lib (@libs) {
120 unless ( File::Spec->file_name_is_absolute($lib) ) {
122 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
123 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
128 $bin_path = $FindBin::Bin;
131 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
140 require RT::Shredder;
142 use Getopt::Long qw(GetOptions);
145 use RT::Shredder::Plugin ();
146 # prefetch list of plugins
147 our %plugins = RT::Shredder::Plugin->List;
152 my $shredder = RT::Shredder->new;
155 my $plugin = eval { $shredder->AddDumpPlugin( Arguments => {
156 file_name => $opt{'sqldump'},
160 print STDERR "ERROR: Couldn't open SQL dump file: $@\n";
161 exit 1 if $opt{'sqldump'};
163 print STDERR "WARNING: It's strongly recommended to use '--sqldump <filename>' option\n";
164 unless( $opt{'force'} ) {
165 exit 0 unless prompt_yN( "Do you want to proceed?" );
168 print "SQL dump file is '". $plugin->FileName ."'\n";
172 my @objs = process_plugins( $shredder );
173 prompt_delete_objs( \@objs ) unless $opt{'force'};
175 $shredder->PutObjects( Objects => $_ ) foreach @objs;
176 eval { $shredder->WipeoutAll };
178 require RT::Shredder::Exceptions;
179 if( my $e = RT::Shredder::Exception::Info->caught ) {
180 print "\nERROR: $e\n\n";
186 sub prompt_delete_objs
190 print "Objects list is empty, try refine search options\n";
193 my $list = "Next ". scalar( @$objs ) ." objects would be deleted:\n";
194 foreach my $o( @$objs ) {
195 $list .= "\t". $o->_AsString ." object\n";
198 exit(0) unless prompt_yN( "Do you want to proceed?" );
204 print "$text [y/N] ";
205 unless( <STDIN> =~ /^(?:y|yes)$/i ) {
213 require RT::Shredder::POD;
214 RT::Shredder::POD::shredder_cli( $0, \*STDOUT );
221 Getopt::Long::Configure( "pass_through" );
223 if( GetOptions( 'object=s' => \@objs ) && @objs ) {
224 print STDERR "Option --object had been deprecated, use plugin 'Objects' instead\n";
229 if( GetOptions( 'plugin=s' => \@plugins ) && @plugins ) {
230 $opt{'plugin'} = \@plugins;
231 foreach my $str( @plugins ) {
232 if( $str =~ /^\s*list\s*$/ ) {
234 } elsif( $str =~ /^\s*help-(\w+)\s*$/ ) {
235 show_plugin_help( $1 );
236 } elsif( $str =~ /^(\w+)(=.*)?$/ && !$plugins{$1} ) {
237 print "Couldn't find plugin '$1'\n";
243 # other options make no sense without previouse
244 usage() unless keys %opt;
246 if( GetOptions( 'force' => \$tmp ) && $tmp ) {
250 if( GetOptions( 'sqldump=s' => \$tmp ) && $tmp ) {
251 $opt{'sqldump'} = $tmp;
258 my $shredder = shift;
261 foreach my $str( @{ $opt{'plugin'} } ) {
262 my $plugin = RT::Shredder::Plugin->new;
263 my( $status, $msg ) = $plugin->LoadByString( $str );
265 print STDERR "Couldn't load plugin\n";
266 print STDERR "Error: $msg\n";
269 if ( lc $plugin->Type eq 'search' ) {
270 push @res, _process_search_plugin( $shredder, $plugin );
272 elsif ( lc $plugin->Type eq 'dump' ) {
273 _process_dump_plugin( $shredder, $plugin );
276 return RT::Shredder->CastObjectsToRecords( Objects => \@res );
279 sub _process_search_plugin {
280 my ($shredder, $plugin) = @_;
281 my ($status, @objs) = $plugin->Run;
283 print STDERR "Couldn't run plugin\n";
284 print STDERR "Error: $objs[1]\n";
289 ($status, $msg) = $plugin->SetResolvers( Shredder => $shredder );
291 print STDERR "Couldn't set conflicts resolver\n";
292 print STDERR "Error: $msg\n";
298 sub _process_dump_plugin {
299 my ($shredder, $plugin) = @_;
300 $shredder->AddDumpPlugin(
307 print "Plugins list:\n";
308 print "\t$_\n" foreach( grep !/^Base$/, keys %plugins );
315 require RT::Shredder::POD;
316 unless( $plugins{ $name } ) {
317 print "Couldn't find plugin '$name'\n";
320 RT::Shredder::POD::plugin_cli( $plugins{'Base'}, \*STDOUT, 1 );
321 RT::Shredder::POD::plugin_cli( $plugins{ $name }, \*STDOUT );