Changeset 373
- Timestamp:
- 08/03/12 15:41:46 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tiny-import.pl
r372 r373 33 33 } 34 34 35 usage() if !@ARGV; 36 37 my %importcfg = ( 38 rw => 0, 39 conv => 0, 40 trial => 0, 41 ); 42 # Handle some command-line arguments 43 while ($ARGV[0] =~ /^-/) { 44 my $arg = shift @ARGV; 45 usage() if $arg !~ /^-[rct]+$/; 46 # -r rewrite imported files to comment imported records 47 # -c coerce/downconvert A+PTR = records to PTR 48 # -t trial mode; don't commit to DB or actually rewrite flatfile (disables -r) 49 $arg =~ s/^-//; 50 my @tmp = split //, $arg; 51 foreach (@tmp) { 52 $importcfg{rw} = 1 if $_ eq 'r'; 53 $importcfg{conv} = 1 if $_ eq 'c'; 54 $importcfg{trial} = 1 if $_ eq 't'; 55 } 56 } 57 $importcfg{rw} = 0 if $importcfg{trial}; 58 59 sub usage { 60 die q(usage: tiny-import.pl [-r] [-c] datafile1 datafile2 ... datafileN ... 61 -r Rewrite all specified data files with a warning header indicating the 62 records are now managed by web, and commenting out all imported records. 63 The directory containing any given datafile must be writable. 64 -c Convert any A+PTR (=) record to a bare PTR if the forward domain is 65 not present in the database. Note this does NOT look forward through 66 a single file, nor across multiple files handled in the same run. 67 Multiple passes may be necessary if SOA and = records are heavily 68 intermixed and not clustered together. 69 -t Trial run mode; spits out records that would be left unimported. 70 Disables -r if set. 71 72 -r and -c may be combined (-rc) 73 74 datafileN is any tinydns record data file. 75 ); 76 } 77 35 78 my $code; 36 79 my ($dbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost}); … … 48 91 import(file => $file); 49 92 # import(file => $file, nosoa => 1); 50 $dbh->rollback ;51 # $dbh->commit;93 $dbh->rollback if $importcfg{trial}; 94 $dbh->commit unless $importcfg{trial}; 52 95 }; 53 96 if ($@) { 54 print "bleh: $@\n"; 55 die "die harder: $errstr\n"; 97 print "Failure trying to import $file: $@\n $errstr\n"; 98 unlink ".$file.$$" if $importcfg{rw}; # cleanup 99 $dbh->rollback; 56 100 } 57 101 } 58 102 59 foreach (keys %cnt) { 60 print " $_ $cnt{$_}\n"; 61 } 103 # print summary count of record types encountered 104 foreach (keys %cnt) { 105 print " $_ $cnt{$_}\n"; 106 } 62 107 63 108 exit 0; … … 66 111 our %args = @_; 67 112 my $flatfile = $args{file}; 113 my @fpath = split '/', $flatfile; 114 $fpath[$#fpath] = ".$fpath[$#fpath]"; 115 my $rwfile = join('/', @fpath);#.".$$"; 116 68 117 open FLAT, "<$flatfile"; 118 119 if ($importcfg{rw}) { 120 open RWFLAT, ">$rwfile" or die "Couldn't open tempfile $rwfile for rewriting: $!\n"; 121 print RWFLAT "# WARNING: Records in this file have been imported to the web UI.\n#\n"; 122 } 69 123 70 124 our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl,location) ". … … 74 128 75 129 while (<FLAT>) { 76 next if /^#/; 77 next if /^\s*$/; 130 if (/^#/ || /^\s*$/) { 131 print RWFLAT "#$_" if $importcfg{rw}; 132 next; 133 } 78 134 chomp; 79 135 s/\s*$//; 80 recslurp($_); 81 } 82 83 # Try the deferred records again, once. 136 my $recstat = recslurp($_); 137 if ($importcfg{rw}) { 138 if ($recstat) { 139 print RWFLAT "#$_\n"; 140 } else { 141 print RWFLAT "$_\n"; 142 } 143 } 144 } 145 146 # Move the rewritten flatfile in place of the original, so that any 147 # external export processing will pick up any remaining records. 148 if ($importcfg{rw}) { 149 close RWFLAT; 150 rename "$rwfile", $flatfile; 151 } 152 153 # Show the failed records 84 154 foreach (@deferred) { 85 # print "trying $_ again\n"; 86 recslurp($_, 1); 87 } 88 89 print scalar(@deferred)." deferred records in $flatfile\n"; 155 print "failed to import $_\n"; 156 } 157 158 ##fixme: hmm. can't write the record back to the flatfile in the 159 # main while above, then come down here and import it anyway, can we? 160 # # Try the deferred records again, once. 161 # foreach (@deferred) { 162 # print "trying $_ again\n"; 163 # recslurp($_, 1); 164 # } 165 166 # .. but we can at least say how many records weren't imported. 167 print scalar(@deferred)." deferred records in $flatfile\n"; 168 $#deferred = -1; 169 90 170 91 171 # Sub for various nonstandard types with lots of pure bytes expressed in octal … … 167 247 my $rec = shift; 168 248 my $nodefer = shift || 0; 249 my $impok = 1; 169 250 170 251 $errstr = $rec; # this way at least we have some idea what went <splat> … … 191 272 } else { 192 273 push @deferred, $rec unless $nodefer; 274 $impok = 0; 193 275 # print "$tmporig deferred; can't find both forward and reverse zone parents\n"; 194 276 } … … 222 304 } else { 223 305 push @deferred, $rec unless $nodefer; 306 $impok = 0; 224 307 # print "$tmporig deferred; can't find parent zone\n"; 225 308 } … … 249 332 } else { 250 333 push @deferred, $rec unless $nodefer; 334 $impok = 0; 251 335 } 252 336 } else { … … 257 341 } else { 258 342 push @deferred, $rec unless $nodefer; 343 $impok = 0; 259 344 } 260 345 } … … 287 372 } else { 288 373 push @deferred, $rec unless $nodefer; 374 $impok = 0; 289 375 } 290 376 … … 306 392 } else { 307 393 push @deferred, $rec unless $nodefer; 394 $impok = 0; 308 395 } 309 396 … … 355 442 } else { 356 443 push @deferred, $rec unless $nodefer; 444 $impok = 0; 357 445 } 358 446 … … 379 467 } else { 380 468 push @deferred, $rec unless $nodefer; 469 $impok = 0; 381 470 } 382 471 } … … 491 580 } else { 492 581 push @deferred, $rec unless $nodefer; 582 $impok = 0; 493 583 } 494 584 … … 508 598 } else { 509 599 push @deferred, $rec unless $nodefer; 600 $impok = 0; 510 601 } 511 602 … … 521 612 } else { 522 613 push @deferred, $rec unless $nodefer; 614 $impok = 0; 523 615 } 524 616 } else { … … 528 620 } else { 529 621 push @deferred, $rec unless $nodefer; 622 $impok = 0; 530 623 } 531 624 } … … 547 640 } else { 548 641 push @deferred, $rec unless $nodefer; 642 $impok = 0; 549 643 } 550 644 } else { … … 554 648 } else { 555 649 push @deferred, $rec unless $nodefer; 650 $impok = 0; 556 651 } 557 652 } … … 569 664 } else { 570 665 push @deferred, $rec unless $nodefer; 571 } 572 573 } else { 666 $impok = 0; 667 } 668 669 } else { 670 print "unhandled rec $rec\n"; 671 $impok = 0; 574 672 # ... uhhh, dunno 575 673 } … … 579 677 print " $_\n"; 580 678 } 581 } 679 680 return $impok; # just to make sure 681 } # recslurp() 582 682 583 683 close FLAT;
Note:
See TracChangeset
for help on using the changeset viewer.