Removed /usr/local from CDPATH
[clearscm.git] / clients / Ameriquest / bin / backup.pl
1 #!/usr/bin/perl
2 #################################################################################
3 #
4 # File:         backup.pl
5 # Description:  This script performs backups of vobs. By backup we mean that it
6 #               will lock a vob then copy that vobs storage area to another area
7 #               on disk then unlock the vob.
8 # Author:       Andrew@DeFaria.com
9 # Created:      June 23, 2004
10 # Language:     Perl
11 # Warnings:     Since we use Windows commands like xcopy this script will only
12 #               work under Windows.
13 #
14 # (c) Copyright 2004, Andrew@DeFaria.com, all rights reserved.
15 #
16 ################################################################################
17 use strict;
18 use warnings;
19
20 my $me = $0;
21
22 $me =~ s/\.\///;
23
24 my $backup_loc  = "d:\\backup";
25 my $history_loc = "d:\\vobstore\\backup";
26 my $from_loc    = "d:\\";
27 my $vob_server  = "rtnlprod01";
28 my $day_nbr     = (localtime ()) [6]; # Day # of week.
29 my $total_size;
30
31 # Options
32 my $verbose     = "no";
33
34 sub Duration {
35   use integer;
36
37   my $start_time = shift;
38   my $end_time   = shift;
39
40   my $hours;
41   my $minutes;
42   my $seconds = $end_time - $start_time;
43
44   if ($seconds eq 0) {
45     return "less than a second";
46   } elsif ($seconds eq 1) {
47     return "a second";
48   } elsif ($seconds < 60) {
49     return "$seconds seconds";
50   } elsif ($seconds < (60 * 60)) {
51     $minutes = $seconds / 60;
52     $seconds = $seconds % 60;
53     my $minutes_string = ($minutes eq 1) ? "minute" : "minutes";
54     my $seconds_string = ($seconds eq 1) ? "second" : "seconds";
55     return "$minutes $minutes_string $seconds $seconds_string";
56   } else {
57     $hours   = $seconds / (60 * 60);
58     $seconds = $seconds % (60 * 60);
59     $minutes = $seconds / 60;
60     $seconds = $seconds % 60;
61     my $hours_string   = ($hours   eq 1) ? "hour"   : "hours";
62     my $minutes_string = ($minutes eq 1) ? "minute" : "minutes";
63     my $seconds_string = ($seconds eq 1) ? "second" : "seconds";
64     return "$hours $hours_string $minutes $minutes_string $seconds $seconds_string";
65   } # fi
66 } # Duration
67
68 sub Usage {
69   print "Usage $me: [-v]";
70   print "\nWhere:\n";
71   print "\t-v\tVerbose\n";
72   exit 1;
73 } # Usage
74
75 sub verbose {
76   my $msg = shift;
77
78   print "$msg\n" if $verbose eq "yes";
79 } # verbose
80
81 sub warning {
82   my $msg = shift;
83
84   print "$me: WARNING: $msg\n";
85 } # warning
86
87 sub error {
88   my $msg       = shift;
89   my $errno     = shift;
90
91   print "$me: ERROR ";
92   print "# $errno: " if defined $errno;
93   print "$msg\n";
94
95   exit $errno if defined $errno;
96 } # error
97
98 sub VobSize {
99   my $vob = shift;
100
101   my $size      = 0;
102   my $cleartext = 0;
103   my @space     = `cleartool space $vob 2> NUL`;
104
105   foreach (@space) {
106     if (/Subtotal $/) {
107       ($size) = split;
108     } # if
109     if (/cleartext/) {
110       ($cleartext) = split;
111     } # if
112   } # foreach
113
114   return $size - $cleartext;
115 } # VobSize
116
117 sub LockVobs {
118   my @vobs = @_;
119
120   my $status = 0;
121
122   foreach (@vobs) {
123     chomp;
124     my $return_code = system "cleartool lock vob:$_ > NUL 2>&1";
125     warning "Unable to lock vob $_" if $return_code ne 0;
126     $status += $return_code;
127   } # foreach
128
129   return $status;
130 } # LockVobs
131
132 sub UnlockVobs {
133   my @vobs = @_;
134
135   my $status = 0;
136
137   foreach (@vobs) {
138     my $return_code = system "cleartool unlock vob:$_ > NUL 2>&1";
139     warning "Unable to unlock vob $_" if $return_code ne 0;
140     $status += $return_code;
141   } # foreach
142
143   return $status;
144 } # UnlockVobs
145
146 sub MoveOldStorage {
147   my $vob_storage_basename = shift;
148
149   my $storage           = "$backup_loc\\$vob_storage_basename";
150   my $old_storage_loc   = "$history_loc\\$day_nbr\\$vob_storage_basename";
151   my @output;
152
153   if (-e $old_storage_loc) {
154     @output = `rmdir /s /q $old_storage_loc`;
155
156     if ($? ne 0) {
157       error "Error in removing old storage area $old_storage_loc";
158       error (join "\n", @output);
159     } else {
160       verbose "Removed old storage area $old_storage_loc";
161     } # if
162   } # if
163
164   @output = `move $storage $old_storage_loc`;
165
166   if ($? ne 0) {
167     error "Error in moving storage area $storage to $old_storage_loc";
168     error (join "\n", @output);
169   } else {
170     verbose "Moved old storage $storage to $old_storage_loc";
171   } # if
172 } # MoveOldStorage
173
174 sub CopyStorage {
175   my $vob                       = shift;
176   my $from                      = shift;
177   my $vob_storage_basename      = shift;
178
179   my $to        = "$backup_loc\\$vob_storage_basename";
180   my $size      = VobSize $vob;
181
182   $total_size += $size;
183   verbose "Copying $vob ($size meg) from $from -> $to";
184
185   my $start_time = time;
186
187   MoveOldStorage $vob_storage_basename if -e $to;
188
189   # Copy storage but exclude any file containing strings found in the
190   # d:\backup\exclude.strings file See
191   # http://www-1.ibm.com/support/docview.wss?rs=0&q1=being-deleted&uid=swg21129318&loc=en_US&cs=utf-8&cc=us&lang=en
192   # for more info.
193   my @output = `xcopy $from $to /q /e /i /h /k /x /exclude:d:\\backup\\exclude.strings`;
194
195   my $end_time = time;
196
197   if ($? ne 0) {
198     print "Error in copy of vob $vob\n";
199     print @output;
200   } else {
201     verbose "Copying of $vob ($size meg) took " . Duration $start_time, $end_time;
202   } # if
203 } # CopyStorage
204
205 # Get parms
206 while ($#ARGV >= 0) {
207   if ($ARGV [0] eq "-v") {
208     $verbose = "yes";
209     shift;
210     next;
211   } # if
212
213   if ($ARGV [0] ne "") {
214     error "Unknown option: \"" . $ARGV [0] . "\"\n";
215     Usage;
216   } # if
217 } # while
218
219 # Iterrate through the list of vobs
220 my $start_time = time;
221
222 my @vobs = `cleartool lsvob -short -host $vob_server 2> NUL`;
223
224 warning "Unable to lock all vobs" if (LockVobs @vobs) ne 0;
225
226 foreach (@vobs) {
227   chomp;
228   my $line = `cleartool lsvob $_ 2> NUL`;
229   chomp $line;
230   $line =~ s/\//\\/g;
231
232   my $storage;
233
234   if ($line =~ m/(\\\\\S*)/) {
235     $storage = $1;
236   } # if
237
238   $storage =~ s/\\\\$vob_server\\/$from_loc/;
239
240   my $vob_storage_basename = substr ($storage, rindex ($storage, "\\") + 1);
241
242   CopyStorage $_, $storage, $vob_storage_basename;
243 } # foreach
244
245 warning "Unable to unlock all vobs" if (UnlockVobs @vobs) ne 0;
246
247 my $end_time = time;
248
249 verbose "Total of $total_size meg copied in: " . Duration $start_time, $end_time;
250 # All done...
251 exit 0;