| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use Getopt::Std; | ||
| 6 | use Date::Parse; | ||
| 7 | |||
| 8 | |||
| 9 | use NBU; | ||
| 10 | |||
| 11 | my %opts; | ||
| 12 | getopts('?dvftw:c:s:', \%opts); | ||
| 13 | |||
| 14 | my $window = defined($opts{'w'}) ? $opts{'w'} : 10; | ||
| 15 | my $controlSize = defined($opts{'c'}) ? $opts{'c'} : 5 * 1024; | ||
| 16 | my $skipCount = defined($opts{'s'}) ? $opts{'s'} : 0; | ||
| 17 | |||
| 18 | # | ||
| 19 | # Activate internal debugging if -d was specified | ||
| 20 | NBU->debug($opts{'d'}); | ||
| 21 | |||
| 22 | # | ||
| 23 | # The class representing the SAP backup we're interested in and a target date/time are | ||
| 24 | # all we need: | ||
| 25 | if ((@ARGV != 2) || $opts{'?'}) { | ||
| 26 | print STDERR <<EOT; | ||
| 27 | Usage: refresh.pl <SAP-INSTANCE> <time-of-split> | ||
| 28 | Options: | ||
| 29 | -s <count> Number of backups to skip backwards | ||
| 30 | -f Force volume list even if not all archive logs can be found | ||
| 31 | -v Display decision process when hunting for backup images | ||
| 32 | -t Only list volume ids | ||
| 33 | EOT | ||
| 34 | exit -1; | ||
| 35 | } | ||
| 36 | |||
| 37 | my $splitTime = str2time($ARGV[1]); | ||
| 38 | if (!defined($splitTime)) { | ||
| 39 | print STDERR "refresh.pl: Could not parse split time: \"".$ARGV[1]."\"\n"; | ||
| 40 | exit -1; | ||
| 41 | } | ||
| 42 | print "Using split target of ".localtime($splitTime)."\n" if (!$opts{'t'}); | ||
| 43 | |||
| 44 | # | ||
| 45 | # Create an NBU Class object corresponding to the class we're interested in | ||
| 46 | my $targetInstance = $ARGV[0]; | ||
| 47 | my $targetClass = NBU::Class->byName("NBU".$targetInstance); | ||
| 48 | if (!defined($targetClass)) { | ||
| 49 | print STDERR "refresh.pl: No such SAP-INSTANCE as \"".$ARGV[0]."\"\n"; | ||
| 50 | exit -1; | ||
| 51 | } | ||
| 52 | elsif ($targetClass->type !~ /SAP/) { | ||
| 53 | print STDERR "refresh.pl: supplied class is not of type SAP\n"; | ||
| 54 | exit -1; | ||
| 55 | } | ||
| 56 | |||
| 57 | # | ||
| 58 | # Sort the images from this class in reverse chronological order (most | ||
| 59 | # recent first). | ||
| 60 | my @imageList = (sort { $b->ctime <=> $a->ctime } $targetClass->images); | ||
| 61 | print "Found ".@imageList." images for class ".$targetClass->name."\n" if ($opts{'v'}); | ||
| 62 | |||
| 63 | # | ||
| 64 | # At this point there are two alternatives: | ||
| 65 | # Find the youngest backup that started before the split, or the youngest backup that | ||
| 66 | # finished before the split. This script currently defaults to latter, somewhat safer technique | ||
| 67 | my $controlFileSet; | ||
| 68 | my $hit; | ||
| 69 | my @targets; | ||
| 70 | for my $i (@imageList) { | ||
| 71 | |||
| 72 | if ($i->ctime > $splitTime) { | ||
| 73 | print "Skipping ".$i->size."k image from ".localtime($i->ctime)." as it is not old enough\n" if ($opts{'v'}); | ||
| 74 | next; | ||
| 75 | } | ||
| 76 | |||
| 77 | if ($i->size < $controlSize) { | ||
| 78 | print "".(defined($hit) ? "Ignoring" : "Skipping"). | ||
| 79 | " image smaller than ${controlSize}k, guessing it only contains control files\n" if ($opts{'v'}); | ||
| 80 | $controlFileSet = $i if (!defined($hit)); | ||
| 81 | next; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (!defined($hit)) { | ||
| 85 | if (!defined($controlFileSet)) { | ||
| 86 | print "Skipping ".$i->size."k image from ".localtime($i->ctime)." as it is still not old enough\n" if ($opts{'v'}); | ||
| 87 | next; | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($skipCount == 0) { | ||
| 91 | print "BINGO: Found image dated ".localtime($i->ctime)."\n" if (!$opts{'t'}); | ||
| 92 | |||
| 93 | $hit = $i; | ||
| 94 | push @targets, $controlFileSet; | ||
| 95 | push @targets, $hit; | ||
| 96 | } | ||
| 97 | else { | ||
| 98 | $skipCount -= 1; | ||
| 99 | print "Skipping allowed image dated ".localtime($i->ctime).", $skipCount left to go\n" if (!$opts{'t'}); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | if (($hit->ctime - $i->ctime) < ($window * 60)) { | ||
| 104 | print "Adding image dated ".localtime($i->ctime)." as it is within $window minutes\n" if ($opts{'v'}); | ||
| 105 | push @targets, $i; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | last; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | if (@targets == 0) { | ||
| 114 | print STDERR "No images met the criteria:\n"; | ||
| 115 | print STDERR " NetBackup class is ".$ARGV[0]."\n"; | ||
| 116 | print STDERR " Control file image < ${controlSize}k\n"; | ||
| 117 | print STDERR " Control file image older than ".localtime($splitTime)."\n"; | ||
| 118 | exit -1; | ||
| 119 | } | ||
| 120 | |||
| 121 | # | ||
| 122 | # With the main backup in hand, we now go hunting for the archive log images needed to make the | ||
| 123 | # restore whole. The question becomes: Knowing when the backup started, do we know when it ended? | ||
| 124 | # As answer we use the ctime of the control file backup. | ||
| 125 | # Thus, we want all archive logs written since the backup started up to and at least one image past | ||
| 126 | # the time the control files were backed up: | ||
| 127 | my $windowEnd = $targets[0]->ctime; | ||
| 128 | my $windowStart = $targets[$#targets]->ctime; | ||
| 129 | my $targetHost = $hit->client; | ||
| 130 | |||
| 131 | @imageList = (sort { $a->ctime <=> $b->ctime} $targetHost->images); | ||
| 132 | my $priorSafety; | ||
| 133 | my $postSafety; | ||
| 134 | |||
| 135 | # | ||
| 136 | # In the interest of expediency we are only going to consider images less than 36 hours old | ||
| 137 | my $tooOld = $windowStart - (144 * 60 * 60); | ||
| 138 | |||
| 139 | my $logCount = 0; | ||
| 140 | my $logSpace = 0; | ||
| 141 | |||
| 142 | for my $i (@imageList) { | ||
| 143 | next if ($i->class->type ne $targetClass->type); | ||
| 144 | next if ($i->class->name !~ /archive/i); | ||
| 145 | |||
| 146 | next if ($i->ctime < $tooOld); | ||
| 147 | |||
| 148 | my $thisLogCount = 0; | ||
| 149 | # | ||
| 150 | # There are two types of archive log images produced by SAP's brarchive; one contains bonafide | ||
| 151 | # archive log files, the other contains some control file data. We are only interested in the | ||
| 152 | # former. | ||
| 153 | my $containsLogs = 0; | ||
| 154 | for my $f ($i->fileList) { | ||
| 155 | if ($f =~ /$targetInstance.*\.dbf$/) { | ||
| 156 | $containsLogs += 1; | ||
| 157 | $thisLogCount += 1; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | next if (!$containsLogs); | ||
| 161 | |||
| 162 | if ($i->ctime < $windowStart) { | ||
| 163 | $priorSafety = $i; | ||
| 164 | next; | ||
| 165 | } | ||
| 166 | |||
| 167 | if ($i->ctime < $splitTime) { | ||
| 168 | push @targets, $i; | ||
| 169 | $logCount += $thisLogCount; | ||
| 170 | $logSpace += $i->size; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | last if (defined($postSafety)); | ||
| 174 | $postSafety = $i; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | if (!defined($postSafety) || !defined($priorSafety)) { | ||
| 179 | print STDERR "refresh.pl: No extra safety archive log backup prior to ".localtime($windowStart)." found\n" | ||
| 180 | if (!defined($priorSafety)); | ||
| 181 | print STDERR "refresh.pl: No extra safety archive log backup after ".localtime($splitTime)." found\n" | ||
| 182 | if (!defined($postSafety)); | ||
| 183 | exit -1 unless ($opts{'f'}); | ||
| 184 | } | ||
| 185 | push @targets, $priorSafety if (defined($priorSafety)); | ||
| 186 | push @targets, $postSafety if (defined($postSafety)); | ||
| 187 | |||
| 188 | my %needed; | ||
| 189 | print "Backup images needed for this split are:\n" if (!$opts{'t'}); | ||
| 190 | for my $i (sort { $a->ctime <=> $b->ctime} @targets) { | ||
| 191 | print " ".localtime($i->ctime)." ".$i->class->name."\n" if (!$opts{'t'}); | ||
| 192 | for my $f ($i->fragments) { | ||
| 193 | if (!$opts{'t'}) { | ||
| 194 | print " Used volume ".$f->volume->id."\n"; | ||
| 195 | } | ||
| 196 | $needed{$f->volume->id} += 1; | ||
| 197 | } | ||
| 198 | next if ($i->class->name !~ /archive/i); | ||
| 199 | if (!$opts{'t'}) { | ||
| 200 | print " Archive log files:\n"; | ||
| 201 | for my $f (sort $i->fileList) { | ||
| 202 | print " ".$f."\n"; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | print "Volumes needed for this split are:\n" if (!$opts{'t'}); | ||
| 208 | for my $id (sort (keys %needed)) { | ||
| 209 | my $m = NBU::Media->byID($id); | ||
| 210 | print " $id"; | ||
| 211 | print " (".$m->group.")" if (!$opts{'t'} && defined($m->group)); | ||
| 212 | print "\n"; | ||
| 213 | } | ||
| 214 | |||
| 215 | $logSpace = sprintf("%.2f", $logSpace / 1024 / 1024); | ||
| 216 | if (!$opts{'t'}) { | ||
| 217 | print "The $logCount archive log files will need ${logSpace}Gb\n"; | ||
| 218 | } | ||
| 219 | |||
| 220 | =head1 NAME | ||
| 221 | |||
| 222 | refresh.pl - Compute tape volumes needed for WM(S/Q) refresh | ||
| 223 | |||
| 224 | =head1 SYNOPSIS | ||
| 225 | |||
| 226 | refresh.pl <SAP-Instance> <split-time> [-t|-v] [-f] [-w <window>] [-s <count>] [-c <size>] | ||
| 227 | |||
| 228 | =head1 DESCRIPTION | ||
| 229 | |||
| 230 | SAP instance backups made using SAP's brtools suite are occasionally used to clone | ||
| 231 | such instances. This utility figures out which tape volumes are needed to perform such | ||
| 232 | a cloning operation. | ||
| 233 | |||
| 234 | Given a master SAP instance name and point-in-time to be used as the reference state, | ||
| 235 | refresh.pl goes through and works backwards from that poin-in-time to find the most recent | ||
| 236 | SAP backup immediately preceding the cut off. As this backup is located, a list of intervening | ||
| 237 | archive log backups is maintained as well. The final list of tapes then covers the | ||
| 238 | main database backup as well as all required archive logs. | ||
| 239 | |||
| 240 | =head1 OPTIONS | ||
| 241 | |||
| 242 | =over 4 | ||
| 243 | |||
| 244 | =item B<-t> | ||
| 245 | |||
| 246 | Provide terse output. This pretty much boils down to tape volume ids only. | ||
| 247 | |||
| 248 | =item B<-v> | ||
| 249 | |||
| 250 | At the other extreme verbose output will show exactly what volumes are needed for what | ||
| 251 | reasons. | ||
| 252 | |||
| 253 | =item B<-f> | ||
| 254 | |||
| 255 | Normally refresh.pl wants to include at least one extra set of archive logs past the split-time | ||
| 256 | to ensure all necessary data will be available to the DBA's when rolling the database forward | ||
| 257 | from the backup. Should such an extra set not be available, the B<-f> option will force refresh.pl | ||
| 258 | to proceed and list volume ids after all. | ||
| 259 | |||
| 260 | =item B<-w> window | ||
| 261 | |||
| 262 | Almost always, a single brbackup run results in multiple NetBackup jobs. With the default settings | ||
| 263 | refresh.pl assumes all such NetBackup jobs started within a 10 minute window. The B<-w> option allows | ||
| 264 | you to change this number. | ||
| 265 | |||
| 266 | =item B<-s> count | ||
| 267 | |||
| 268 | If you know the most recent SAP backup is corrupt or the tapes onto which it is written have been | ||
| 269 | damaged, providing refresh.pl with skip count will have it ignore that many SAP backups as it scans | ||
| 270 | backwards in time. Note that the intervening archive logs are still needed! | ||
| 271 | |||
| 272 | =item B<-c> size | ||
| 273 | |||
| 274 | A key item in determining when an SAP instance backup completed is the fact that once the actual database | ||
| 275 | files have been written out, a small set of files is written in a separate NetBackup job. To distinguish | ||
| 276 | between the bulk data and this bit of control information the assumption is made that the control | ||
| 277 | information takes up less than 5 MBytes. The B<-c> size option lets you change this to some other number of | ||
| 278 | MBytes. | ||
| 279 | |||
| 280 | =back | ||
| 281 | |||
| 282 | =head1 AUTHOR | ||
| 283 | |||
| 284 | Winkeler, Paul pwinkeler@pbnj-solutions.com | ||
| 285 | |||
| 286 | =head1 COPYRIGHT | ||
| 287 | |||
| 288 | Copyright (C) 2002 Paul Winkeler | ||
| 289 | |||
| 290 | =cut |