#!/usr/bin/perl   
## @file sysbackup
# @brief Perform system backup (both base and applications).
# @details User's level interface to configure backup - define where to
#          backup data (USB or SFTP). Creates configuration file under
#          "/etc/opt/nortel/base/recovery/" directory.
# @param -b One time backup.
# @param -c Add backup task to cron's scheduler.
# @param -r Remove backup task from cron's scheduler.
# @param -s Show current backup time settings.
# @param -h, --help Display a help message.
# @author Kate Bereznyakova
# @date February 2007
# @version 1.00 - Initial version
# @version 1.10 - Added special configuration files for cron backup.
#                 Added using semaphores.
# @version 1.11 - Function EscCurlyBrackets() was renamed to EscSpecialCharacters().
#                 Added check for '\' character.
# @version 1.12 - POD section was added; the usage is displayed by the
#                 common function - print_pod_usage() now.
# @version 1.13 - noninteractive and auto backup mode was added
# @version 1.14 - Two colons were removed before shared functions and variables.
# @version 1.15 - USB node detection changed.
# @version 1.20 - Function ShowBackupSettings() was replaced with cron_backup_manager;
#                 system() is replaced with do_system()
# @version 1.21 - Function collectSFTPInfo() has been modified to set up error message to deployment configuration file
# @version 1.22 - get_hostname() is now used instead of `hostname`
# @version 1.23 - $ENV{'PATH'} is now patched to contain /bin and /usr/bin/ under restricted shell ('tar' command requires it).
# @version 1.24 - Parsing the network conf files is removed from genSFTPConfig() function.
# @version 1.25 - "group members can write" policy is now applied to fsbackup* files in /etc/opt/nortel/base/recovery/
# @version 1.26 - Backup scripts error handling was added. New parameter was implemented.
#  @li NONINTERACTIVE - local variable 'noninteractive' was replace with global recovery::backup::NONINTERACTIVE
#                       related action is the same.
#  @li BACKUP_ERR_MODE - was implemented into recovery::backup. If this parameter is passed to sysbackup,
#                        if some backup script returns ERROR backup will failed.
# @version 1.30 - new set_status() function is added.
# @version 1.31 - $tmpMountDir is now created under root account
# @note

=pod

=head1 NAME

B<sysbackup> - User's level interface to configure backup.

=head1 SYNOPSIS

B<sysbackup> B<-r>|B<-s>|B<-b>|B<-c>|[B<--help>]

=head1 DESCRIPTION

This script allows users to define the storage to backup data (USB device or
SFTP server), and choose the type of backup (one-time backup or scheduled).
It uses the crontab mechanism to perform scheduled backup.

=head1 OPTIONS

=over 4

=item B<-r>

remove sysbackup task from schedule

=item B<-s>

show sysbackup task schedule settings

=item B<-b>

execute one-time backup

=item B<-c>

setup a backup schedule

=item B<--help>

Print this help message and exit.

=back

=cut

use lib "/opt/nortel/base/lib";

use common_variables;
use common_functions;
use baselog;
use base_semctl;
use strict;
use warnings;
use base_system;
use base_deploy;
use Switch;
use recovery::backup;
use Getopt::Long;
use CommonInstallUtils;

my %ftpData;
my $config  = "";     #path to configuration file
my $DM_mode = $FALSE;

# @skipline
sub main();           #entry point

# @skipline
sub makeUSBConfig();  #create configuration file for fsbackup described
                      #special parameters for backup to USB device

# @skipline
sub genSFTPConfig();  #create configuration file for fsbackup described
                      #special parameters for backup to SFTP-server

# @skipline
sub collectSFTPInfo();  #function to get all parameters to access to
                        #SFTP-server's backup storage

# @skipline
sub prepareUSB($);      #checks device and formats it

# @skipline
sub EscSpecialCharacters($);  #escaping special characters

# @skipline
sub set_status();

# types of backup configuration
my $USB_CONF  = 1;            # USB
my $SFTP_CONF = 2;            # SFTP
my $UPGR_CONF = 3;            # UPGRADE (local storage)

$NONINTERACTIVE  = $FALSE;
$BACKUP_ERR_MODE = $FALSE;
my $auto     = $FALSE;
my $path     = undef;
my $user     = undef;
my $pwd      = undef;
my $sftp     = undef;
my @primopt  = ();
my $hostname = undef;
my $cfg_file = undef;
exit(main());

#################################################
#          Main function (entry point)          #
#               /user interface/                #
#################################################
#  Supported keys are:
#  -r               : remove sysbackup task from schedule
#  -s               : show sysbackup task schedule settings
#  -b               : execute one-time backup
#  -c               : setup a backup schedule
#  --upgrade        : make default upgrade archive and
#                     put it to /admin partition
#                     this key is not available to the
#                     end-user through the CLI
#  -noninteractive  : run script in noninteractive mode
#                     this key is not available to the
#                     end-user through the CLI
#  -auto            : auto collect sftp params
#                     this key is not available to the
#                     end-user through the CLI
#  -path            : path to backup archive (use with auto mode)
#  -sftp            : IP address of the Backup Server (use with auto mode)
#  -user            : backup user ID (use with auto mode)
#  -pwd             : not encrypted backup user password (use with auto mode)
#  -backup_err_mode : if passed, then backup wiil fail if one of backup scripts
#                     fails(actual only for noninteractive mode)

sub main()
{
   if (warning_if_not_nortel_grp())
   {
      return $ERROR;
   }
   signals_ignore();
   recover_env_path();

   if (defined $ARGV[0] and $ARGV[0] =~ /help$/)
   {
      print_pod_usage();
      return $OK;
   }
   my $retval = $ERROR;

   my $opt_b;        #flag for sysbackup option '-b'
   my $opt_c;        #flag for sysbackup option '-c'
   my $opt_r;        #flag for sysbackup option '-r'
   my $opt_h;        #flag for sysbackup option '-h'
   my $opt_s;        #flag for sysbackup option '-s'
   my $opt_upgrade;  #flag for sysbackup option '--upgrade'

   my @semlist = ($SEM_BACKUP, $SEM_PATCH);

   if (!defined(@ARGV))
   {
      print_pod_usage();
      return $ERROR;
   }

   GetOptions(
              "noninteractive"  => \$NONINTERACTIVE,
              "auto"            => \$auto,
              "sftp:s"          => \$sftp,
              "user:s"          => \$user,
              "pwd:s"           => \$pwd,
              "path:s"          => \$path,
              "b"               => \$opt_b,
              "c"               => \$opt_c,
              "r"               => \$opt_r,
              "h"               => \$opt_h,
              "s"               => \$opt_s,
              "upgrade"         => \$opt_upgrade,
              "backup_err_mode" => \$BACKUP_ERR_MODE,
             );

   $config = $fsbackup_config_script;

   #check the mode by script name.
   #$0 (perl's var): Contains the name of the program being executed.
   if ((get_basename($0)) eq (get_basename($RMT_SYSBACKUP_CMD)))
   {
      $DM_mode  = $TRUE;
      $hostname = get_hostname();
      $cfg_file = get_deploy_cfg_path($hostname);
   } ## end if ((get_basename($0))...

   # create semaphore manager object
   my $semmngr = base_semctl::new("SYSBACKUP");
   if (!defined $semmngr)
   {
      baselog($LOG_ERROR, "Cannot create semaphore manager object ($!).");
      print "Error during starting backup: cannot create semaphore manager object.\n";
      set_status();
      return $ERROR;
   } ## end if (!defined $semmngr)
   if (!$semmngr->sem_take(\@semlist, IPC_NOWAIT))
   {
      undef $semmngr;
      if ($DM_mode)
      {
         update_section($cfg_file, $DEPERRORPATH, 'RSBLK');
         set_status();
      }
      return $ERROR;
   } ## end if (!$semmngr->sem_take...

   # remove sysbackup task from schedule
   if ($opt_r)
   {
      system("$SUDO $BACKUP_CRON_MNGR -r");
      $retval = $OK;
   }

   # show sysbackup task schedule settings
   elsif ($opt_s)
   {
      do_system([$SUDO, $BACKUP_CRON_MNGR, '-s'], {$dsSTDOUT => $TRUE});
      $retval = $OK;
   }

   # execute one-time backup or setup a backup schedule
   elsif (($opt_b) or ($opt_c))
   {

      # cron backup has it's own configuration file
      if ($opt_c)
      {
         $config .= $CRON_MARK;
      }
      $ftpData{'backup_mode'} = ($opt_b and (!$NONINTERACTIVE)) ? "interactive" : "background";
      my $is_end = $FALSE;
      my $opt;
      while (!$is_end)
      {
         if (!$NONINTERACTIVE)
         {
            print "$USB_CONF. Backup to USB device.\n";
            print "$SFTP_CONF. Backup to SFTP server.\n\n";
            print "Enter your choice (q for exit): ";
            chomp($opt = <STDIN>);
         } ## end if (!$NONINTERACTIVE)
         else
         {
            $opt    = $SFTP_CONF;
            $is_end = $TRUE;
         }

         switch ($opt)
         {
            case ["Q", "q"]
            {
               $retval = $ERROR;
               $is_end = $TRUE;
            }
            case [$USB_CONF, $SFTP_CONF]
            {
               if (createConfig($opt))
               {
                  if ($opt_b)
                  {
                     my $key = "";
                     if ($opt_upgrade)
                     {
                        $key = "--upgrade";
                     }
                     if (backupNow($key))
                     {
                        $retval = $OK;
                     }
                  } ## end if ($opt_b)
                  elsif ($opt_c)
                  {
                     system("$SUDO $BACKUP_CRON_MNGR -c  $config");
                     $retval = $OK;
                  }
                  $is_end = $TRUE;
               } ## end if (createConfig($opt)...
               else
               {
                  baselog($LOG_ERROR, "Creation of configuration file failed.\n", !$NONINTERACTIVE);
                  baselog($LOG_ERROR, "Backup will not be created.\n\n",          !$NONINTERACTIVE);
               }
            } ## end case [$USB_CONF, $SFTP_CONF...
            else
            {
               print "Incorrect input. Try again.\n";
            }
         } ## end switch
      } ## end while (!$is_end)
   } ## end elsif (($opt_b) or ($opt_c...
   elsif ($opt_upgrade)
   {
      $ftpData{'backup_mode'} = "background";
      if (createConfig($UPGR_CONF))
      {
         if (backupNow("--upgrade"))
         {
            if (-e $config)
            {
               if ((unlink $config) != 1)
               {
                  baselog($LOG_WARNING, "Cannot remove $config file.");
               }
            } ## end if (-e $config)

            $retval = $OK;
         } ## end if (backupNow("--upgrade"...
      } ## end if (createConfig($UPGR_CONF...
      else
      {
         baselog($LOG_ERROR, "Creation of configuration file failed.\n", !$NONINTERACTIVE);
         baselog($LOG_ERROR, "Backup will not be created.\n\n",          !$NONINTERACTIVE);
      }
   } ## end elsif ($opt_upgrade)

   # if the passed parameter is help or unknown
   else
   {
      print_pod_usage();
      $retval = $ERROR;
   }

   if (!$semmngr->sem_give(\@semlist))
   {
      baselog($LOG_WARNING, "Cannot release $SEM_BACKUP and $SEM_PATCH semaphores.");
   }
   undef $semmngr;
   set_status();
   return $retval;
} ## end sub main()

#################################################
#                Subroutines                    #
#################################################

###-------------------------------------------------------------------------
# Purpose:
#  - Create configuration file for the fsbackup utility
#  INPUT:  backup storage type:
#              1 - USB;
#              2 - SFTP;
#              3 - local/UPGRADE;
#  OUTPUT: $TRUE  (1) - success
#          $FALSE (0) - error
###-------------------------------------------------------------------------
sub createConfig($)
{
   my $backup_storage = shift @_;
   my $retval         = $FALSE;
   if ($backup_storage eq $USB_CONF)
   {
      $config .= "USB";
      $retval = makeUSBConfig();
   }
   elsif ($backup_storage eq $SFTP_CONF)
   {
      $config .= "SFTP";
      $retval = genSFTPConfig();
   }
   elsif ($backup_storage eq $UPGR_CONF)
   {
      $config .= "UPGRADE";
      $retval = genUPGRADEConfig();
   }
   else
   {
      baselog($LOG_ERROR, "This type of backup storage ($backup_storage) is not supported.");
   }

   if ($retval)
   {
      if (system("$CAT $configBase >> $config") != 0)
      {
         baselog($LOG_ERROR, "Appending default data to configuration file problem.");
         $retval = $FALSE;
      }
   } ## end if ($retval)

   return $retval;
} ## end sub createConfig($)

###-------------------------------------------------------------------------
# Purpose:
#  - Make one-time backup operation
#  INPUT:  $key       - additional parameter
#  OUTPUT: $TRUE  (1) - success
#          $FALSE (0) - error
###-------------------------------------------------------------------------
sub backupNow
{
   my $key    = shift @_;
   my $retval = $FALSE;
   baselog($LOG_INFO, "Backup started. Please wait...\n", !$NONINTERACTIVE);
   if (!do_backup($config, $key))
   {
      baselog($LOG_ERROR, "Backup operation failed.\n", !$NONINTERACTIVE);
   }
   else
   {
      baselog($LOG_INFO, "Backup complete.\n", !$NONINTERACTIVE);
      $retval = $TRUE;
   }
   return $retval;
} ## end sub backupNow

###-------------------------------------------------------------------------
# Purpose:
#  - create config. file for fsbackup script with
#    specific for upgrade case (temporary backup)
#  INPUT:  none
#  OUTPUT: $TRUE  (1) - success
#          $FALSE (0) - error
###-------------------------------------------------------------------------
sub genUPGRADEConfig()
{
   my $hostname = get_hostname();

   # Remove the previous config
   if (-e $config)
   {
      if ((unlink $config) != 1)
      {
         baselog($LOG_ERROR, "Old configuration file found. Cannot remove $config file.");
         return $FALSE;
      }
   } ## end if (-e $config)

   if (not defined $hostname or $hostname eq "")
   {
      baselog($LOG_ERROR, "Cannot determine host name.");
      return $FALSE;
   }

   # Open config for writing
   unless (open(CONFIG, ">$config"))
   {
      baselog($LOG_ERROR, "Cannot open $config file for writing.");
      return $FALSE;
   }

   # Generate the first part of config
   print CONFIG "\$backup_mode = q{$ftpData{'backup_mode'}};\n";
   print CONFIG "\$cfg_cache_dir = q{$cfg_cache_dir};\n";
   print CONFIG "\$cfg_backup_name = q{$hostname};\n";
   print CONFIG "\$cfg_type = q{upgrade};\n";

   close CONFIG;
   return $TRUE;
} ## end sub genUPGRADEConfig()

###-------------------------------------------------------------------------
# Purpose:
#  - create config. file for fsbackup script with
#    specific for usb device parameters (this is a "local" backup type)
#  INPUT:
#  OUTPUT: $TRUE  (1) - success
#          $FALSE (0) - error
###-------------------------------------------------------------------------
sub makeUSBConfig()
{
   my $usbnode  = "";
   my $hostname = get_hostname();

   # Remove the previous config
   if (-e $config)
   {
      if ((unlink $config) != 1)
      {
         baselog($LOG_ERROR, "Old configuration file found. Cannot remove $config file.");
         return $FALSE;
      }
   } ## end if (-e $config)

   if (not defined $hostname or $hostname eq "")
   {
      baselog($LOG_ERROR, "Cannot determine host name.");
      return $FALSE;
   }

   my $sn = get_USB_SN_list();
   if (!is_array($sn))
   {
      baselog($LOG_ERROR, 'Cannot find USB device.', $TRUE);
      return $FALSE;
   }

   my $usb_dev_numb = scalar(@$sn);

   if ($usb_dev_numb != 1)
   {
      baselog($LOG_ERROR, "Only one USB storage device can be present during backup opreration. There are currently $usb_dev_numb devices attached.", $TRUE);
      return $FALSE;
   }

   baselog($LOG_DEBUG, "USB serial number is: @$sn[0]");

   # check if USB device is properly formated.
   if (!prepareUSB(@$sn[0]))
   {
      baselog($LOG_ERROR, 'This USB device cannot be used for backup operation.');
      return $FALSE;
   }

   # Open config for writing
   my $fd;
   if (not open($fd, ">", $config))
   {
      baselog($LOG_ERROR, "Cannot open $config file for writing.");
      return $FALSE;
   }

   # Generate the first part of config
   print $fd "\$backup_mode = q{$ftpData{'backup_mode'}};\n";
   print $fd "\$cfg_usb_sn = q{@$sn[0]};\n";
   print $fd "\$cfg_cache_dir = q{$cfg_cache_dir};\n";
   print $fd "\$cfg_backup_name = q{$hostname};\n";
   print $fd "\$cfg_type = q{local};\n";

   if (not close $fd)
   {
      baselog($LOG_ERROR, "Cannot close the file descriptor: $!.");
      return $FALSE;
   }

   unless (chmod 0660, $config)
   {
      baselog($LOG_ERROR, "Cannot change permissions for config file $config.");
      return $FALSE;
   }

   return $TRUE;
} ## end sub makeUSBConfig()

#######################################################################
## @fn genSFTPConfig()
# @brief generate config. file for fsbackup script with
#        specific for sftp-storage parameters (this is a "remote-ftp" backup type)
# @details
# @param none
# @return $FALSE - error
# @return $TRUE  - success
# @author
# @date
# @note
#######################################################################
sub genSFTPConfig()
{
   my $hostname        = get_hostname();
   my $tmp_config_name = $config;        # name of backup configuration file
   my $oldConf         = undef;

   # find the previous backup configuration file. Store it's name in $tmp_config_name variable
   if ($tmp_config_name =~ /$CRON_MARK/)
   {
      if (not -e $tmp_config_name)
      {
         $tmp_config_name =~ s/$CRON_MARK//;
      }
   } ## end if ($tmp_config_name =~...
   if (-e $tmp_config_name)
   {
      $oldConf = $tmp_config_name . 'Old';
   }

   if (defined $oldConf)
   {
      if (-e $oldConf)
      {
         unless (unlink $oldConf)
         {
            baselog($LOG_ERROR, "Cannot remove old configuration file $oldConf.");
         }
      } ## end if (-e $oldConf)
      system($CP, $tmp_config_name, $oldConf);
      require $oldConf;
      if (defined($::cfg_remote_host))
      {
         $ftpData{'remoteIP'} = $::cfg_remote_host;
      }
      if (defined($::cfg_remote_login))
      {
         $ftpData{'user'} = $::cfg_remote_login;
      }
      if (defined($::cfg_remote_path))
      {
         $ftpData{'remoteDir'} = $::cfg_remote_path;
      }
   } ## end if (defined $oldConf)

   # Remove the previous config file in all situations except one, when default values are taken
   # from file for runtime backup, when cron backup is performed. Because we must leave runtime
   # backup configuration file, when we use information from it for cron backup.
   if ((-e $tmp_config_name) && (($tmp_config_name =~ /$CRON_MARK/) || ($config !~ /$CRON_MARK/)))
   {
      if ((unlink $tmp_config_name) != 1)
      {
         baselog($LOG_ERROR, "Old configuration file found. Cannot remove $tmp_config_name file.");
         return $FALSE;
      }
   } ## end if ((-e $tmp_config_name...

   if ($hostname eq "")
   {
      baselog($LOG_ERROR, "Cannot determine host name.");
      return $FALSE;
   }
   if (collectSFTPInfo() == $FALSE)
   {
      baselog($LOG_ERROR, "No data about remote backup storage.");
      return $FALSE;
   }

   # Generate the first part of config
   my $tmp_remdir = EscSpecialCharacters($ftpData{'remoteDir'});
   my $ret        = undef;
   if ($config =~ m/$CRON_MARK/)
   {  #collect pass for scheduled backup
      baselog($LOG_DEBUG, "Password is stored for scheduled backup.");
      do_sudo([$SMCRYPT, $ftpData{'passwd'}, $cronsftp_identity], {$dsRETVAL => \$ret});
   }
   else
   {  #collect pass for manual backup
      baselog($LOG_DEBUG, "Password is stored for manual backup.");
      do_sudo([$SMCRYPT, $ftpData{'passwd'}, $sftp_identity], {$dsRETVAL => \$ret});
   }
   if ($ret)
   {
      baselog($LOG_ERROR, "Error storing password.\n");
      return $FALSE;
   }

   # Open config for writing
   my $fd;
   if (not open($fd, ">", $config))
   {
      baselog($LOG_ERROR, "Cannot open $config file for writing.");
      return $FALSE;

   } ## end if (not open($fd, ">",...

   print $fd "\$backup_mode = q{$ftpData{'backup_mode'}};\n";
   print $fd "\$cfg_backup_name = q{$hostname};\n";
   print $fd "\$cfg_cache_dir = q{$cfg_cache_dir};\n";
   print $fd "\$cfg_type = q{remote_ftp};\n";
   print $fd "\$cfg_remote_host = q{$ftpData{'remoteIP'}};\n";
   print $fd "\$cfg_remote_login = q{$ftpData{'user'}};\n";
   print $fd "\$cfg_remote_path = q{$tmp_remdir};\n";

   if (not close $fd)
   {
      baselog($LOG_ERROR, "Cannot close the file descriptor: $!.");
      return $FALSE;
   }

   unless (chmod 0660, $config)
   {
      baselog($LOG_ERROR, "Cannot change permissions for config file $config.");
      return $FALSE;
   }

   return $TRUE;
} ## end sub genSFTPConfig()

## @fn EscSpecialCharacters($)
# @brief Escaping special characters in the string for using
#        this string into q{} statement
# @details Replacement of characters '{', '}', '\' by '\{', '\}', '\\'
# @param $_[0] - source string
# @return $dest - the string with escaped characters
# @author
# @date
# @note
sub EscSpecialCharacters($)
{
   my $dest = shift @_;
   $dest =~ s/([{}\\])/\\$1/g;
   return $dest;
}

## @fn prepareUSB($)
# @brief It checks if USB device is properly formatted and formats it if required.
# @details
# @param $sn - device serial number
# @return   $TRUE  - operation successful
# @return   $FALSE - some error happens
sub prepareUSB($)
{
   my $sn = shift @_;

   my ($USBNode, $first_part) = get_USB_node_path_by_SN($sn);

   if ((!defined $USBNode) or ($USBNode eq ''))
   {
      baselog($LOG_ERROR, 'Cannot define USB device node name.', $TRUE);
      return $FALSE;
   }

   if (!-e $USBNode)
   {
      baselog($LOG_ERROR, "Node '$USBNode' does not exist.");
      return $FALSE;
   }

   unless (do_sudo([$SUDO_NORTEL, $CREATE_USB_MOUNTPOINT]))
   {
      baselog($LOG_ERROR, 'Cannot create USB mount point.', $TRUE);
      return $FALSE;
   } ## end if (!-e $tmpMountDir)

   umountUSB($first_part);

   #get node name only from the path: /dev/sd?1
   my (undef, undef, $name_first_part) = split('/', $first_part);
   baselog($LOG_DEBUG, "First partition node is '$name_first_part'.");

   if (!checkUSBBackupDevice($USBNode, $name_first_part, $tmpMountDir))
   {
      if (!needFormat($USBNode, $name_first_part, $tmpMountDir))
      {
         baselog($LOG_ERROR, "Warning: format operation interrupted.\n", $TRUE);
         umountUSB($first_part);
         return $FALSE;
      } ## end if (!needFormat($USBNode...
   } ## end if (!checkUSBBackupDevice...
   return $TRUE;
} ## end sub prepareUSB($)

###-------------------------------------------------------------------------
# Purpose:
# - To collect information for sftp connection
###-------------------------------------------------------------------------
sub collectSFTPInfo()
{
   my $retval = $FALSE;

   #
   # see if we have any default values
   #
   my $is_end = $FALSE;
   while (($is_end == $FALSE) && (!$auto))
   {

      # 1) get remote machine ip
      # 2) get remote sftp username
      # 3) get remote sftp password
      # 4) get remote directory
      #
      $ftpData{'remoteIP'} = craphack(getValidIP("Enter the secure FTP server's IP address", $ftpData{'remoteIP'}));
      $ftpData{'user'} = craphack(getLogin("Enter the SFTP login", $ftpData{'user'}));

      # Password should be hidden. Do not show this input
      system("$STTY -echo");
      $ftpData{'passwd'} = craphack(getTextResponse("Enter the SFTP password", ""));
      system("$STTY echo");

      #as input for password was not out to screen so new line symbol should be added
      print "\n";
      $ftpData{'remoteDir'} = craphack(getTextResponse("Enter the remote SFTP directory", $ftpData{'remoteDir'}));

      if (validateRemoteCIQInfo(%ftpData))
      {
         $retval = $TRUE;
         $is_end = $TRUE;
      }
   } ## end while (($is_end == $FALSE...

   if ($auto)
   {
      if ((!defined $user) or ($user eq ''))
      {
         baselog($LOG_ERROR, 'Backup user ID is undefined.');
         return $FALSE;
      }
      if ((!defined $sftp) or ($sftp eq ''))
      {
         baselog($LOG_ERROR, 'Backup ip address is undefined.');
         return $FALSE;
      }
      if ((!defined $path) or ($path eq ''))
      {
         baselog($LOG_ERROR, 'Backup archive file name is undefined.');
         return $FALSE;
      }
      if (!defined $pwd)
      {
         baselog($LOG_ERROR, 'Backup user password is undefined.');
         return $FALSE;
      }
      $ftpData{'remoteIP'}  = $sftp;
      $ftpData{'user'}      = $user;
      $ftpData{'passwd'}    = $pwd;
      $ftpData{'remoteDir'} = $path;

      $retval = $TRUE;

   } ## end if ($auto)

   if (system("$PING -q -c 3 $ftpData{'remoteIP'} > /dev/null 2>&1") != 0)
   {
      baselog($LOG_ERROR, "\nRemote host (SFTP server) is not accessible via Network.\n", !$NONINTERACTIVE);
      if (!$NONINTERACTIVE) {print "Check network configuration.\n\n";}
      if (($DM_mode) and (!update_section($cfg_file, $DEPERRORMSGPATH, 'SFTP server is not accessible via Network.', $KEEPOLDCNT)))
      {
         baselog($LOG_ERROR, 'Cannot update the deployment.xml file.');
      }
      $retval = $FALSE;
   } ## end if (system("$PING -q -c 3 $ftpData{'remoteIP'} > /dev/null 2>&1"...
   return $retval;
} ## end sub collectSFTPInfo()

###############################################################################
## @fn set_status()
# @brief   It sets operation status to None for Deployment Mode operation.
# @details None.
# @return  $TRUE
sub set_status()
{
   if ($DM_mode)
   {
      update_section($cfg_file, "/target/$DEPSTATUS", $DEPLOYSTATUS{none});
   }
   return $TRUE;
} ## end sub set_status()
