############################################################################################# # This utility performs the export/import of configuration across different releases. # For Export: It does the zipping of examcards database files and exports the zip along with # the configuration export (using p_apco_save_restore_configuration.pl EXPORT) # by doing a protective check of the amount of disk space available in the # destination export location. # For Import: It does the unzipping of exported examcards database zip file along with # the configuration import (using p_apco_save_restore_configuration.pl IMPORT) ############################################################################################# use strict; use Net::Ping; use Cwd; use oslnm; my $inputMode = $ARGV[0]; $inputMode = "\U$inputMode"; if($inputMode eq "") { $inputMode = "EXPORT"; } my $used_diskspace = 0; my $gyrosite = $ENV{"GyroSite"}; my $gyro_site = "$gyrosite\\site"; my $configuration_dir = "configuration"; my @g_spt_dir_list = ("spt\\report", "SPE", "spt\\batch\\TEST-IQT", "spt\\spt_report_files", "spt\\spt_SPE_Report_Files", "spt\\Spt_Batch_Files\\TEST-IQT"); my @g_dir_export_list = ("exportpp", "importpp", "persistent", "shim", "site", "speccom", @g_spt_dir_list, "stt", "gvnt"); #DVD-PC my $read_dvd = "ReadDVD"; my $virtual_dvd = "virtdvd7"; my $dvd_burner_name = "burner"; my $virtual_dvd_backup = "\\\\$dvd_burner_name\\$virtual_dvd"; my $virtual_read_dvd_backup = "\\\\$dvd_burner_name\\$read_dvd"; my $ping_timeout = 4; my $ping_str = "Check presence of DVD-PC, ping $dvd_burner_name. This may take $ping_timeout seconds.\n"; #INTERNAL-DVD+RW my $temp_config_drive = $ENV{"GyroConfigTemp"}; my $temp_config_drive_bak = $temp_config_drive; my $local_str = "local"; my $local_dir = $temp_config_drive . "\\" . $local_str; my $backup_config_dir = "$temp_config_drive\\$configuration_dir"; my $dvd_rw_drive = $ENV{"GyroDVDRW"}; my $dvd_rw_root = $dvd_rw_drive . "\\"; if(!($temp_config_drive =~ /\\$/)) { $temp_config_drive = $temp_config_drive . "\\"; $ENV{"GyroConfigTemp"} = $temp_config_drive; } my $exim_config_tool = "p_apco_save_restore_configuration.pl"; #DISK-DEV my $export_location = ""; #IMPORT my $import_location = ""; #--------------------------------------------------------------------- #prepare the path to unzip.exe #--------------------------------------------------------------------- my $program_files = $ENV{"ProgramFiles(x86)"}; my $zip = "$program_files\\info-zip\\zip.exe"; my $unzip = "$program_files\\info-zip\\unzip.exe"; my $output_zipFile = "ECdbfiles.zip"; my $export_zip = 0; if(($inputMode ne "IMPORT" && $inputMode ne "EXPORT") || $#ARGV > 0) { print "Error: Invalid Arguments\n"; DisplayUsage(); } print "\nChecking prerequisites...\n\n"; if (!(-e $unzip ) || !(-e $zip)) { print "Error: Zip Utility is not present. Aborting application...\n"; exit; } else { print "Zip Utility Present\n"; } if($inputMode eq "EXPORT") { execute_export(); } elsif ($inputMode eq "IMPORT") { execute_import(); } else { print "Error: Invalid Arguments\n"; DisplayUsage(); } $ENV{"GyroConfigTemp"} = $temp_config_drive_bak; sub execute_command($) { my $command = $_[0]; system($command); } ########################################################################################## # This method gets user choice of export medium and does a validation check of the export # location. It then invokes the script "p_apco_save_restore_configuration.pl" from the # current release with appropriate arguments to export the system configuration. ########################################################################################## sub execute_export { my $selectedChoice = DisplayExportMenu(); $used_diskspace = GetDiskUsage(@g_dir_export_list); my $status = ValidateExportMedium($selectedChoice); if($status == -1) { print "Aborting application...\n"; exit(); } print "\n"; if($status == 0 || $status == 1) #DVD-PC or INTERNAL DVD-RW { #Execute p_apco_save_restore_configuration.pl EXPORT without argument execute_command("$exim_config_tool EXPORT"); } elsif($status == 2) #DISK-DEV { #Execute p_apco_save_restore_configuration.pl EXPORT with destination #drive as argument. execute_command("$exim_config_tool EXPORT $export_location"); } print "\n"; if($export_zip) { #Delete Zipped file from G:\site execute_command("del /F \"$gyro_site\\$output_zipFile\""); } } ########################################################################################## # This method gets user choice of import medium and does a validation check of existance # of import location. It then invokes the script "p_apco_save_restore_configuration.pl" # from current release with appropriate arguments to import the configuration. ########################################################################################## sub execute_import { my $selectedChoice = DisplayImportMenu(); my $status = ValidateImportMedium($selectedChoice); if($status == -1) { print "Aborting application...\n"; exit(); } #unzip the ECdbfiles.zip file to G:\site from the import_location my $zipped_file_path = "$import_location\\$configuration_dir\\site\\$output_zipFile"; if(-e $zipped_file_path) { print "Found $zipped_file_path. Extracting to $gyro_site\n"; execute_command("\"$unzip\" -o $zipped_file_path -d $gyro_site"); } else { print "$zipped_file_path not found.\n"; } #Execute p_apco_save_restore_configuration.pl IMPORT with import #location as argument. print "\n"; execute_command("$exim_config_tool IMPORT $import_location"); print "\n"; #Delete ECdbfiles.zip from G:\Site after import is complete. if( -e "$gyro_site\\$output_zipFile") { execute_command("del /F \"$gyro_site\\$output_zipFile\""); } } ########################################################################################## # Displays usage of the p_apco_eximutility tool. ########################################################################################## sub DisplayUsage() { print "Command Usage: p_apco_eximutility.pl \n"; exit(); } ########################################################################################## # This method returns the used/free disk space of the location specified. # P1: Absolute path of the location for which disk space is required. # P2: "used" or "free" - Used to compute used or free disk space. ########################################################################################## sub GetDiskSpace($$) { my $path = $_[0]; my $cmd = $_[1]; my $free_space = 0; my $used_space = 0; if($cmd eq "used") { my $size = `fsutil volume diskfree $path 2>NUL | find "Total # of bytes"`; my @freetoks = split(':', $size); my $total_space = $freetoks[1]; my $availsize = `fsutil volume diskfree $path 2>NUL | find "Total # of avail free bytes"`; my @Availfreetoks = split(':', $size); $used_space = $total_space - $Availfreetoks[1]; $free_space = $used_space ; } else { my $size = `fsutil volume diskfree $path 2>NUL | find "Total # of avail free bytes"`; my @freetoks = split(':', $size); $free_space = $freetoks[1]; } return $free_space; } ########################################################################################## # Used to compute the total disk space required to export the configuration from list of # exported folders. # P1: Array of the list of folders to be exported from G:\ drive. ########################################################################################## sub GetDiskUsage(@) { my $disk_space = 0; my @dir_list = @_; foreach my $listvalue (@dir_list) { my $size = GetDiskSpace("$gyrosite\\$listvalue", "used"); $disk_space = $disk_space + $size; } return $disk_space; } ########################################################################################## # Displays folder selection UI and gets location choice from the user. ########################################################################################## sub select_folder_name() { my $selected_folder_name; my $retry_count = 0; do { $selected_folder_name = ""; my $logical_folder_name = "logical_folder_name_value"; my $command = "browse_folder_win_cs.exe $logical_folder_name"; system("$command"); oslnm::translate( $logical_folder_name, $selected_folder_name); oslnm::delete_global( $logical_folder_name); $retry_count++; if ( $selected_folder_name =~ / / ) { print "The selected folder name constains a space. Please select one WITHOUT spaces!!!.\n"; } } while ( ( ($selected_folder_name =~ / /) || ($selected_folder_name eq "") ) && ( $retry_count < 3) ); if ( $selected_folder_name eq "" ) { my $msg = "$retry_count retries. The application will exit.\n"; print "$msg"; exit(0); } return $selected_folder_name; } ########################################################################################## # Converts the value passed from bytes to MBs and returns it as formatted string. # P1: Disk space value in bytes ########################################################################################## sub ConvertToMB($) { my $val = $_[0]; my $conv_val = ($val)/(1024 * 1024); my $str = "$conv_val (MB)"; return $str; } ########################################################################################## # Validates the user selected choice of import medium. Checks the existance of the import # location and returns the same selection if validation succeeds else returns -1 to # indicate failure. # P1: User selected choice of import medium from Import Menu. Valid values are 0,1,2,3 # where, # 0 -> DVD-ROM # 1 -> DVD-PC # 2 -> DISK-DEV # 3 -> MOD ########################################################################################## sub ValidateImportMedium($) { my $selection = $_[0]; if($selection == 0) #DVD-ROM { oslnm::translate("GYRO_SWLOAD", $import_location); } elsif($selection == 1) #DVD-PC { if (!pingecho($dvd_burner_name, $ping_timeout) ) { print "NO DVD-PC available. Exiting...\n"; return -1; } $import_location = $virtual_read_dvd_backup; } elsif($selection == 2) #DISK-DEV { print "\nSelect Import Location...\n"; $import_location = select_folder_name(); print "Location selected = $import_location\n"; } elsif($selection == 3) #MOD { $import_location = $ENV{"GyroMOD"}; } else { print "Error: Invalid Import Choice Selection\n"; return -1; } my $import_path = "$import_location\\$configuration_dir"; if(!(-d $import_path)) { print "Import location $import_path does not exist\n"; return -1; } return $selection; } ########################################################################################## # Validates the user selected choice of export medium and does the following:- # - Checks the existance of export medium. # - Zips Examcards database files <*.dat> and puts it to G:\site\ECdbfiles.zip for export. # - Verifies the free disk space availability on the export medium. If insufficient space # found in export location then skips export of zipped file and prompts user to do a # manual backup. Returns -1 to indicate failure if still the disk space is insufficient. # P1: User selected choice of export medium from Export Menu. Valid values are 0,1,2 where, # 0 -> DVD-PC # 1 -> INTERNAL-DVD+RW # 2 -> DISK-DEV ########################################################################################## sub ValidateExportMedium($) { my $selection = $_[0]; if($selection == 0 || $selection == 1) { print "\nPlease insert a blank DVD. Press Enter when ready..."; my $input = ; } my $destination = ""; if($selection == 0) #DVD-PC { if (!pingecho($dvd_burner_name, $ping_timeout) ) { print "NO DVD-PC available. Exiting...\n"; return -1; } $destination = $virtual_dvd_backup; } elsif( $selection == 1) #INTERNAL-DVD+RW { if (!(-e $temp_config_drive )) { print "Internal-DVD+RW not available\n"; return -1; } $destination = $local_dir; if(!(-e $local_dir)) { my $my_md = `md $local_dir 2>&1`; } } elsif($selection == 2)#DISK-DEV { print "\nSelect Export Location...\n"; $export_location = select_folder_name(); print "Location selected = $export_location\n"; chomp($export_location); if(!(-e $export_location)) { print "Export Location: $export_location does not exist. Exiting...\n"; return -1; } $destination = $export_location; } else { print "Error: Invalid Export Choice Selection\n"; return -1; } #Get Destination disk space my $dest_free_space = GetDiskSpace($destination, "free"); if($dest_free_space <= 0) { print "Error: Insufficient Space at \"$destination\". Exiting...\n"; return -1; } #Zip all the examcard files *.dat my $saved_cur_dir = getcwd(); chdir($gyro_site); my $zip_args = ""; #PR : MR00125909 # export only these files my @dblist = ("ppdb.dat", "shimdb.dat", "tedb.dat", "tespydb.dat"); my @ndxlist = ("ppdb.ndx", "shimdb.ndx", "tedb.ndx", "tespydb.ndx"); push(@dblist, @ndxlist); my @slotfiles = <*_Slot*>; my @previousfiles = <*_Previous*>; my @skipfiles = (@slotfiles, @previousfiles); my $skipfilesize = 0; foreach my $filename (@skipfiles) { $filename = "\"$filename\""; $skipfilesize = $skipfilesize + GetDiskSpace($filename, "used"); } #Form the zip arguments and compute total *.dat file size foreach my $db (@dblist) { $db = "\"$db\""; $zip_args = $zip_args . " " . $db; } #zip the contents in G:\site only (assumming enough space in G:\site) #and compute the zip size. print "\nStarting zip of ECdatabase files...\n\n"; execute_command("\"$zip\" $output_zipFile $zip_args"); my $zippedSize = GetDiskSpace($output_zipFile, "used"); my $recomputed_used_diskspace = $used_diskspace - $skipfilesize + $zippedSize; print "\nExported Configuration Size: " . ConvertToMB($recomputed_used_diskspace) . "\n"; print "Export Location($destination) Free Space: " . ConvertToMB($dest_free_space) . "\n\n"; if($dest_free_space <= $recomputed_used_diskspace) { #delete zip file. execute_command("del /F \"$output_zipFile\""); #Abort operation print "Insufficient space...\nAbort(Y) or Skip export of ecdatabase files(N): "; my $option = ; chomp($option); $option = "\U$option"; if($option eq "N") { $recomputed_used_diskspace = $used_diskspace - $skipfilesize; print "\nNote: Export of ecdatabase files will be ignored. Please take backup manually.\n"; print "Press Enter to continue..."; my $val = ; print "\nExported Configuration Size: " . ConvertToMB($recomputed_used_diskspace) . "\n"; print "Destination($destination) Free Space: " . ConvertToMB($dest_free_space) . "\n\n"; if($dest_free_space <= $recomputed_used_diskspace) { print "Error: Insufficient space. Export aborted.\n"; return -1; } } else { return -1; } } else { #Zipped file will be exported. Already created in G:\site #Variable is used to determine if zip file is exported or the #raw *.dat files are exported. $export_zip = 1; print "ECdatabase files are zipped($output_zipFile) and ready to be exported.\n"; } chdir($saved_cur_dir); return $selection; } ########################################################################################## # Displays the menu options for configuration import and gets users choice selection. ########################################################################################## sub DisplayImportMenu { my $choice = 0; my $displaymenu = 0; do { print "\n\n********* EximUtility (Import) *********\n\n"; print " 0. DVD-ROM \n"; print " 1. DVD-PC \n"; print " 2. DISK-DEV \n"; print " 3. MOD \n"; print " 4. Exit \n"; print "\n****************************************\n"; print "\nEnter Choice: "; $choice = ; chomp($choice); if( $choice < 0 || $choice > 4 || $choice =~ m/\D/) { print "Invalid Choice! ReEnter\n\n\n"; $displaymenu = 1; } else { $displaymenu = 0; } } while($displaymenu); if($choice == 4) { print "\nExiting Application...\n"; exit(); } return abs($choice); } ########################################################################################## # Displays the menu options for configuration export and gets users choice selection. ########################################################################################## sub DisplayExportMenu { my $choice = 0; my $displaymenu = 0; do { print "\n\n********* EximUtility (Export) *********\n\n"; print " 0. DVD-PC \n"; print " 1. INTERNAL-DVD+RW \n"; print " 2. DISK-DEV \n"; print " 3. Exit \n"; print "\n****************************************\n"; print "\nEnter Choice: "; $choice = ; chomp($choice); if( $choice < 0 || $choice > 3 || $choice =~ m/\D/) { print "Invalid Choice! ReEnter\n\n\n"; $displaymenu = 1; } else { $displaymenu = 0; } } while($displaymenu); if($choice == 3) { print "\nExiting Application...\n"; exit(); } return abs($choice); } #***************************************************************************** # MODIFICATION HISTORY #//***************************************************************************** # 5-Aug-2013 Abinash Kumar Ojha # Initialversion and introduced for Serval software the contents # of this file is merged with P_apco_save_restore.pl and hence # When modifying this file make the next file also in sync with this # file infra\softwareinstallation\res\Scripts\dvd_instal\EximUtility_Version_1.0.pl