verify-db.pl.in 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!@perlexec@
  2. #
  3. # BEGIN COPYRIGHT BLOCK
  4. # Copyright (C) 2013 Red Hat, Inc.
  5. # All rights reserved.
  6. #
  7. # License: GPL (version 3 or any later version).
  8. # See LICENSE for details.
  9. # END COPYRIGHT BLOCK
  10. #
  11. use lib qw(@perlpath@);
  12. use DSUtil;
  13. DSUtil::libpath_add("@db_libdir@");
  14. DSUtil::libpath_add("@libdir@");
  15. $ENV{'PATH'} = "@libdir@/@package_name@/slapd-$servid:@db_bindir@:/usr/bin:/";
  16. $ENV{'SHLIB_PATH'} = "$ENV{'LD_LIBRARY_PATH'}";
  17. my $custom_dbdir = 0;
  18. my $i = 0;
  19. sub usage
  20. {
  21. print "Usage: verify-db.pl [-Z serverID] [ -a <fullpath_to_db_dir> ] [-h]\n";
  22. }
  23. # getDbDir checks up to 4 levels of db dirs
  24. # e.g., <server_inst_dir>/db/<backend_instance_dir>/<subdir>
  25. sub getDbDir
  26. {
  27. (my $here) = @_;
  28. my @dbdirs = ();
  29. opendir(DIR0, $here) or die "can't opendir $here : $!";
  30. while (defined(my $file0 = readdir(DIR0)))
  31. {
  32. if ( "$file0" eq "\." || "$file0" eq "\.\." )
  33. {
  34. ;
  35. }
  36. elsif ( "$file0" eq "DBVERSION" )
  37. {
  38. $#dbdirs++;
  39. $dbdirs[$#dbdirs] = $here;
  40. }
  41. elsif ( -d $here . "/" . $file0 )
  42. {
  43. opendir(DIR1, $here . "/" . $file0) or die "can't opendir $file0 : $!";
  44. while (defined(my $file1 = readdir(DIR1)))
  45. {
  46. if ( "$file1" eq "\." || "$file1" eq "\.\." )
  47. {
  48. ;
  49. }
  50. elsif ( "$file1" eq "DBVERSION" )
  51. {
  52. $#dbdirs++;
  53. $dbdirs[$#dbdirs] = $here . "/" . $file0;
  54. }
  55. elsif ( -d $here . "/" . $file0 . "/" . $file1 )
  56. {
  57. opendir(DIR2, $here . "/" . $file0 . "/" . $file1) or die "can't opendir $file1 : $!";
  58. while (defined(my $file2 = readdir(DIR2)))
  59. {
  60. if ( "$file2" eq "\." || "$file2" eq "\.\." )
  61. {
  62. ;
  63. }
  64. elsif ("$file2" eq "DBVERSION")
  65. {
  66. $#dbdirs++;
  67. $dbdirs[$#dbdirs] = $here . "/" . $file0 . "/" . $file1;
  68. }
  69. elsif ( -d $here . "/" . $file0 . "/" . $file1 . "/" . $file2 )
  70. {
  71. opendir(DIR3, $here . "/" . $file0 . "/" . $file1 . "/" . $file2) or die "can't opendir $file1 : $!";
  72. while (defined(my $file3 = readdir(DIR3)))
  73. {
  74. if ( "$file3" eq "\." || "$file3" eq "\.\." )
  75. {
  76. ;
  77. }
  78. elsif ("$file3" eq "DBVERSION")
  79. {
  80. $#dbdirs++;
  81. $dbdirs[$#dbdirs] = $here . "/" . $file0 . "/" . $file1 . "/" . $file2;
  82. }
  83. }
  84. closedir(DIR3);
  85. }
  86. }
  87. closedir(DIR2);
  88. }
  89. }
  90. closedir(DIR1);
  91. }
  92. }
  93. closedir(DIR0);
  94. return \@dbdirs;
  95. }
  96. sub getLastLogfile
  97. {
  98. (my $here) = @_;
  99. my $logfile = "";
  100. opendir(DIR, $here) or die "can't opendir $here : $!";
  101. while (defined($file = readdir(DIR)))
  102. {
  103. if ($file =~ /log./)
  104. {
  105. $logfile = $file;
  106. }
  107. }
  108. closedir(DIR);
  109. return \$logfile;
  110. }
  111. $NULL = "/dev/null";
  112. while ($i <= $#ARGV) {
  113. if ( "$ARGV[$i]" eq "-a" ) { # path to search the db files
  114. $i++; $startpoint = $ARGV[$i];
  115. } elsif ( "$ARGV[$i]" eq "-Z" ) { # server instance identifier
  116. $i++; $servid = $ARGV[$i];
  117. } elsif ("$ARGV[$i]" eq "-h") { # help
  118. &usage; exit(0);
  119. } else {
  120. &usage; exit(1);
  121. }
  122. $i++;
  123. }
  124. ($servid, $notused_configdir) = DSUtil::get_server_id($servid, "@initconfigdir@");
  125. print("*****************************************************************\n");
  126. print("verify-db: This tool should only be run if recovery start fails\n" .
  127. "and the server is down. If you run this tool while the server is\n" .
  128. "running, you may get false reports of corrupted files or other\n" .
  129. "false errors.\n");
  130. print("*****************************************************************\n");
  131. if ( "$startpoint" eq "" ) {
  132. $startpoint = "@localstatedir@/lib/@PACKAGE_NAME@/slapd-$servid/db";
  133. } else {
  134. $custom_dbdir = 1;
  135. }
  136. # get dirs having DBVERSION
  137. my $dbdirs = getDbDir($startpoint);
  138. # Check transaction logs by db_printlog
  139. for (my $i = 0; "$$dbdirs[$i]" ne ""; $i++)
  140. {
  141. my $logfile = getLastLogfile($$dbdirs[$i]);
  142. if ( "$$logfile" ne "" )
  143. {
  144. # run db_printlog -h <dbdir> for each <dbdir>
  145. print "Verify log files in $$dbdirs[$i] ... ";
  146. open(PRINTLOG, "db_printlog -h $$dbdirs[$i] 2>&1 1> $NULL |");
  147. sleep 1;
  148. my $haserr = 0;
  149. while ($l = <PRINTLOG>)
  150. {
  151. if ("$l" ne "")
  152. {
  153. if ($haserr == 0)
  154. {
  155. print "\n";
  156. }
  157. print "LOG ERROR: $l";
  158. $haserr++;
  159. }
  160. }
  161. close(PRINTLOG);
  162. if ($haserr == 0 && $? == 0)
  163. {
  164. print "Good\n";
  165. }
  166. else
  167. {
  168. print "Log file(s) in $$dbdirs[$i] could be corrupted.\n";
  169. print "Please delete a log file $$logfile, and try restarting the server.\n";
  170. }
  171. }
  172. }
  173. # Check db files by db_verify
  174. print "Verify db files ... ";
  175. if ($custom_dbdir){
  176. open(DBVERIFY, "@sbindir@/dbverify -Z $servid -a $startpoint 2>&1 1> $NULL |");
  177. } else {
  178. open(DBVERIFY, "@sbindir@/dbverify -Z $servid 2>&1 1> $NULL |");
  179. }
  180. sleep 1;
  181. my $bad_index = 0;
  182. my $bad_id2entry = 0;
  183. my $isfirst = 1;
  184. while ($l = <DBVERIFY>)
  185. {
  186. if ($isfirst)
  187. {
  188. print "\n";
  189. $isfirst = 0;
  190. }
  191. if ("$l" =~ /verify failed/)
  192. {
  193. if ("$l" =~ /id2entry.db/)
  194. {
  195. $bad_id2entry++;
  196. }
  197. else
  198. {
  199. $bad_index++;
  200. }
  201. }
  202. print "$l";
  203. }
  204. close(DBVERIFY);
  205. if ($bad_id2entry > 0)
  206. {
  207. print "\nFound the db was corrupted\n";
  208. print "Please restore your backup and recover the database.\n";
  209. exit(1);
  210. }
  211. elsif ($bad_index > 0)
  212. {
  213. print "\nFound the index file(s) was corrupted\n";
  214. print "Please run db2index on the corrupted index\n";
  215. exit(1);
  216. }
  217. else
  218. {
  219. print "Good\n";
  220. exit(0);
  221. }