archive data to tape from EB disks. Sergey.
authorhadaq <hadaq>
Tue, 7 Sep 2010 11:04:37 +0000 (11:04 +0000)
committerhadaq <hadaq>
Tue, 7 Sep 2010 11:04:37 +0000 (11:04 +0000)
disks/data2tape.pl [new file with mode: 0755]

diff --git a/disks/data2tape.pl b/disks/data2tape.pl
new file mode 100755 (executable)
index 0000000..90a5136
--- /dev/null
@@ -0,0 +1,187 @@
+#!/usr/bin/perl -w
+
+use strict;
+use Getopt::Long;
+use Data::Dumper;
+use File::stat;
+use FileHandle;
+use Time::Local;
+use List::MoreUtils qw(any apply);
+use File::Basename;
+
+my $opt_help = 0;
+my $opt_startDate;
+my $opt_endDate;
+my @prefix_list = ("be");
+my @file_list;
+my $file_list_aref = \@file_list;
+my $archive = "hadessep10raw";
+my $opt_arch = 0;
+
+GetOptions ('h|help'      => \$opt_help,
+            's|start=s'   => \$opt_startDate,
+            'e|end=s'     => \$opt_endDate,
+            'p|prefix=s'  => \@prefix_list,
+           'a|arch'      => \$opt_arch);
+
+if(-1 == &checkArgs()){
+    exit(0);
+}
+
+my $startSec = &date2sec($opt_startDate);
+my $endSec   = &date2sec($opt_endDate);
+
+&getFileList();
+
+&archive();
+
+exit(0);
+
+########################### END OF MAIN ############################
+
+sub checkArgs()
+{
+    my $retval = 0;
+
+    unless( defined $opt_startDate ){
+       print "Start date is not given!\n";
+       $retval = -1;
+    }
+
+    unless( defined $opt_endDate ){
+       print "End date is not given!\n";
+       $retval = -1;
+    }
+
+    return $retval;
+}
+
+sub date2sec()
+{
+    my ($date_time) = @_;
+
+    my $sec_epoch;
+
+    if( $date_time =~ /(\d{4})-(\d{2})-(\d{2})_(\d{2}):(\d{2}):(\d{2})/ ){
+
+       #- Correct to get proper format if needed
+       my $year  = $1;
+       my $mon   = $2 - 1;  # 0..11
+       my $mday  = $3;      # 1..31
+       my $hour  = $4;
+       my $min   = $5;
+       my $sec   = $6;
+
+       #- Convert to Epoch seconds in a local time zone
+       $sec_epoch = timelocal($sec, $min, $hour, $mday, $mon, $year);
+    }
+    else{
+       print "Wrong format: $date_time\nExit.\n";
+       exit(0);
+    }
+
+    return $sec_epoch;
+}
+
+sub getFileList()
+{
+    #- Loop over disks
+    foreach my $diskNr (1..22){ 
+       my $path = sprintf("/data%02d/data", $diskNr);
+       
+       my @data = glob("$path/*.hld");
+
+       foreach my $hldfile (@data){
+
+           #- File size must be above 1kB
+           next if(stat($hldfile)->size < 1024);
+
+           #- Check prefix
+           if($hldfile =~ /(\w{2})\d+\.hld/){
+               my $prefix = $1;
+
+               #- File must have a predefined prefix
+               next unless( any {$_ eq $prefix} @prefix_list );
+           }
+           else{
+               print "=====> Strange hld file name: $hldfile\n";
+           }
+
+           #- Check time interval
+           my $my_sec = stat($hldfile)->mtime;
+
+           next unless(stat($hldfile)->mtime > $startSec && stat($hldfile)->mtime < $endSec);
+
+           push(@file_list, $hldfile);
+       }
+    }
+}
+
+sub archive()
+{
+    my $nrOfFiles = scalar @file_list;
+
+    print "Number of files to archive: $nrOfFiles\n\n";
+
+    if($opt_arch){
+       print "The data will be written to the $archive archive!\n";
+       &askUser();
+    }
+
+    foreach my $hldfile (@file_list){
+
+       my $cmd = "gstore arch \"$hldfile\" $archive \"hld\"";
+       print "cmd: $cmd\n";
+       system($cmd) if($opt_arch);
+    }
+}
+
+sub askUser()
+{
+    my $answer = &promptUser("Continue?", "yes/no");
+    if( $answer eq "no" || $answer eq "n" ){
+       print "Exit.\n";
+       exit(0);
+    }
+    else{
+       print "Continue...\n";
+    }    
+}
+
+sub promptUser {
+
+   #  two possible input arguments - $promptString, and $defaultValue 
+   #  make the input arguments local variables.                        
+
+   my ($promptString,$defaultValue) = @_;
+
+   #  if there is a default value, use the first print statement; if  
+   #  no default is provided, print the second string.                 
+
+   if ($defaultValue) {
+      print $promptString, "[", $defaultValue, "]: ";
+   } else {
+      print $promptString, ": ";
+   }
+
+   $| = 1;               # force a flush after our print
+   my $input = <STDIN>;  # get the input from STDIN (presumably the keyboard)
+
+   # remove the newline character from the end of the input the user gave us
+
+   chomp($input);
+
+   #  if we had a $default value, and the user gave us input, then   
+   #  return the input; if we had a default, and they gave us no     
+   #  no input, return the $defaultValue.                            
+   #                                                                  
+   #  if we did not have a default value, then just return whatever  
+   #  the user gave us.  if they just hit the <enter> key,           
+   #  the calling routine will have to deal with that.               
+
+   if ("$defaultValue") {
+      return $input ? $input : $defaultValue; # return $input if it has a value
+   } else {
+      return $input;
+   }
+}