]> git.uio.no Git - usit-rt.git/blame - sbin/rt-importer
Merge branch 'master' of git.uio.no:usit-rt
[usit-rt.git] / sbin / rt-importer
CommitLineData
320f0092
MKG
1#!/usr/bin/perl
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
6# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
7# <sales@bestpractical.com>
8#
9# (Except where explicitly superseded by other copyright notices)
10#
11#
12# LICENSE:
13#
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
17# from www.gnu.org.
18#
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.
23#
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.
29#
30#
31# CONTRIBUTION SUBMISSION POLICY:
32#
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.)
38#
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.
47#
48# END BPS TAGGED BLOCK }}}
49use strict;
50use warnings;
51
52# fix lib paths, some may be relative
53BEGIN {
54 require File::Spec;
55 my @libs = ("lib", "local/lib");
56 my $bin_path;
57
58 for my $lib (@libs) {
59 unless ( File::Spec->file_name_is_absolute($lib) ) {
60 unless ($bin_path) {
61 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
62 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
63 }
64 else {
65 require FindBin;
66 no warnings "once";
67 $bin_path = $FindBin::Bin;
68 }
69 }
70 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
71 }
72 unshift @INC, $lib;
73 }
74
75}
76
77use RT;
78RT::LoadConfig();
79RT->Config->Set(RecordBaseClass => "DBIx::SearchBuilder::Record");
80RT::Init();
81
82use RT::Migrate;
83use RT::Migrate::Importer::File;
84use Getopt::Long;
85use Pod::Usage qw//;
86use Time::HiRes qw//;
87
88my %OPT = (resume => 1);
89GetOptions(
90 \%OPT,
91 "help|?",
92 "quiet|q!",
93 "list|l!",
94
95 "resume!",
96 "originalid|i=s",
97
98 "ask",
99 "ignore-errors",
100
101 "dump=s@",
102) or Pod::Usage::pod2usage();
103
104Pod::Usage::pod2usage(-verbose => 1) if $OPT{help};
105
106Pod::Usage::pod2usage() unless @ARGV == 1;
107my ($dir) = @ARGV;
108$dir =~ s|/$||;
109die "No such directory $dir\n" unless -d $dir;
110die "$dir doesn't appear to contain serialized data\n"
111 unless -f "$dir/001.dat";
112
113if ($OPT{dump}) {
114 die "Dumping objects only works in conjunction with --list\n"
115 unless $OPT{list};
116
117 $OPT{dump} = [ split /,/, join(',', @{$OPT{dump}}) ];
118}
119
120my $error_handler;
121if ($OPT{ask}) {
122 die "Interactive mode (--ask) doesn't work when STDERR and STDIN aren't terminals.\n"
123 unless -t STDERR and -t STDIN;
124
125 $error_handler = sub {
126 my $importer = shift;
127 local $| = 1;
128 print STDERR "\n", @_, "\n";
129 print STDERR "Hit any key to abort import, or type 'ignore' to continue anyway.\n";
130 print STDERR "Continuing may leave you with a corrupt database. > ";
131 chomp( my $resp = <STDIN> );
132 return lc($resp) eq 'ignore';
133 };
134}
135elsif ($OPT{'ignore-errors'}) {
136 $error_handler = sub {
137 my $importer = shift;
138 warn "Ignoring error: ", @_;
139 return 1;
140 };
141}
142
143my $import = RT::Migrate::Importer::File->new(
144 Directory => $dir,
145 OriginalId => $OPT{originalid},
146 DumpObjects => $OPT{dump},
147 Resume => $OPT{resume},
148 HandleError => $error_handler,
149);
150
151if ($import->Metadata and -t STDOUT and not $OPT{quiet}) {
152 $import->Progress(
153 RT::Migrate::progress(
154 counts => sub { $import->ObjectCount },
155 max => $import->Metadata->{ObjectCount},
156 )
157 );
158}
159
160my $log = RT::Migrate::setup_logging( $dir => 'importer.log' );
161print "Logging warnings and errors to $log\n" if $log;
162
163my %counts;
164if ($OPT{list}) {
165 %counts = $import->List;
166
167 my $org = $import->Organization;
168 print "=========== Dump of $org ===========\n\n";
169} else {
170 %counts = $import->Import;
171
172 my $org = $import->Organization;
173 print "========== Import of $org ==========\n\n";
174}
175
176print "Total object counts:\n";
177for (sort {$counts{$b} <=> $counts{$a}} keys %counts) {
178 printf "%8d %s\n", $counts{$_}, $_;
179}
180
181my @missing = $import->Missing;
182if (@missing) {
183 warn "The following UIDs were expected but never observed:\n";
184 warn " $_\n" for @missing;
185}
186
187my @invalid = $import->Invalid;
188if (@invalid) {
189 warn "The following UIDs (serialized => imported) referred to objects missing from the original database:\n";
190 for my $info (@invalid) {
191 my $uid = delete $info->{uid};
192 my $obj = $import->LookupObj($uid);
193 warn sprintf " %s => %s (%s)\n",
194 $uid,
195 ($obj && $obj->Id ? $obj->UID : '(not imported)'),
196 join(", ", map { "$_ => $info->{$_}" }
197 grep { defined $info->{$_} }
198 sort keys %$info);
199 }
200}
201
202if ($log and -s $log) {
203 print STDERR "\n! Some warnings or errors occurred during import."
204 ."\n! Please see $log for details.\n\n";
205}
206
207exit @missing;
208
209=head1 NAME
210
211rt-importer - Import a serialized RT database on top of the current one
212
213=head1 SYNOPSIS
214
215 rt-importer path/to/export/directory
216
217This script is used to import the contents of a dump created by
218C<rt-serializer>. It will create all of the objects in the dump in the
219current database; this may include users, queues, and tickets.
220
221It is possible to stop the import process with ^C; it can be later
222resumed by re-running the importer.
223
224=head2 OPTIONS
225
226=over
227
228=item B<--list>
229
230Print a summary of the data contained in the dump.
231
232=item B<--originalid> I<cfname>
233
234Places the original ticket organization and ID into a global custom
235field with the given name. If no global ticket custom field with that
236name is found in the current database, it will create one.
237
238=item B<--ask>
239
240Prompt for action when an error occurs inserting a record into the
241database. This can often happen when importing data from very old RTs
242where some attachments (usually spam) contain invalid UTF-8.
243
244The importer will pause and ask if you want to ignore the error and
245continue on or abort (potentially to restart later). Ignoring errors
246will result in missing records in the database, which may cause database
247integrity problems later. If you ignored any errors, you should run
248C<rt-validator> after import.
249
250=item B<--ignore-errors>
251
252Ignore all record creation errors and continue on when importing. This
253is equivalent to running with C<--ask> and manually typing "ignore" at
254every prompt. You should always run C<rt-validator> after importing
255with errors ignored.
256
257B<This option can be dangerous and leave you with a broken RT!>
258
259=item B<--dump> I<class>[,I<class>]
260
261Prints L<Data::Dumper> representations of the objects of type I<class> in the
262serialized data. This is mostly useful for debugging.
263
264Works only in conjunction with C<--list>.
265
266=back
267
268
269=head1 CLONED DATA
270
271Some dumps may have been taken as complete clones of the RT system,
272which are only suitable for inserting into a schema with no data in it.
273You can setup the required database state for the receiving RT instance
274by running:
275
276 sbin/rt-setup-database --action create,schema,acl --prompt-for-dba-password
277
278The normal C<make initdb> step will B<not> work because it also inserts
279core system data.
280
281
282=cut