#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
#  @(#)  5f56dbd 43haes/lib/ksh93/hacmp/KLIB_HACMP_add_backup.sh, 726, 2147A_aha726, Mar 08 2021 04:44 PM

# Start of POD-formatted documentation. Viewing suggestions:
#      perldoc <FILENAME>
#      pod2text -c <FILENAME>
#      pod2text -c --code <FILENAME>
#      pod2html <FILENAME>
function devDoc {
    : <<'=cut' >/dev/null 2>&1

=head1 NAME

 KLIB_HACMP_add_backup

=head1 SYNOPSIS

 For creating a cloud backup:
 clmgr add backup <backup_profile>

 add => create, make, mk
 backup => ba, backup

=head1 DESCRIPTION

  Attempts to create a cloud backup for backup profile in PowerHA SystemMirror.

=head1 ARGUMENTS

 1. properties [REQUIRED] [hash ref]
 An associative array within which data about the
 created object can be returned to the caller.

 2. backup_profile [REQUIRED] [string]
 Backup Profile name should be single backup profile.

=head1 RETURN

 0: no errors were detected; the operation appears to have been successful
 1: a general error has occurred
 2: a specified resource does not exist, or could not be found
 3: some required input was missing
 4: some detected input was incorrect in some way
 5: a required dependency does not exist
 6: a specified search failed to match any data

=cut
} # End of POD-formatted documentation.


#============================================================================
#
# Name:        KLIB_HACMP_add_backup
#
# Description: This is the main, FPATH function that is invoked by clmgr
#              to add/create backup in cloud. The cl_cbm_backup utility is
#              invoked to create the backup in cloud.
#
# Inputs:      See the "devDoc()" function at the top of this file.
#
# Outputs:     The properties hash is populated. The only other outputs are
#              any error messages that might be needed.
#
# Returns:     Zero if no errors are detected. Otherwise, an appropriate
#              non-zero value is returned. Refer to the "RETURN" section
#              of the "devDoc()" function at the top of this file for
#              the standard return code values/meanings for clmgr.
#
#============================================================================
function KLIB_HACMP_add_backup {
    . $HALIBROOT/log_entry "$0()" "$CL"

    typeset -n properties=$1
    typeset bp_name=$2

    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset available_bps="" # To store the backup rg's
    typeset backup_type="" # To store the backup method, which is cloud or remote
    typeset cloud_bps="" # To store the backup rgs which are configured for cloud
    typeset output=""
    typeset bOpt=""

    #====================================
    : Check python is installed or not
    #====================================
    LANG=C cl_get_python_version 2>&1 >>/dev/null
    rc=$?
    if (( $rc != 0 )); then
        cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1315 "\nERROR: Python must be installed for using backup feature.\n\n" 1>&2
        log_return_msg "$rc" "$0()" "$LINENO"
        return $rc
    fi

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        rc=$RC_MISSING_DEPENDENCY
    elif [[ -z $bp_name ]]
    then
        cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
        rc=$RC_MISSING_INPUT
    else
        print -- "$0()[$LINENO]($SECONDS): cl_cbm_list" >>$CLMGR_TMPLOG # Always log command result
        available_bps=$(LANG=C cl_cbm_list 2>>$CLMGR_TMPLOG)
        rc=$?
        print -- "$0()[$LINENO]($SECONDS): cl_cbm_list RC:$rc" >>$CLMGR_TMPLOG # Always log command result
        if (( $rc != RC_SUCCESS ));then
            cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1197 "\nERROR: No resource group configured for backup.\n\n" 1>&2
            rc=$RC_ERROR
        fi
    fi

    # Get the list of backup rgs which are configured for cloud storage.
    for bp in $available_bps
    do
        backup_type=$(LANG=C cl_cbm_list $bp | grep -w Backup_method | cut -f2 -d'=' | sed -e 's/^ *//')
        if [[ $backup_type == "cloud" ]]
        then
            cloud_bps="$cloud_bps $bp"
        fi
    done

    # Validate inputs
    if (( rc == RC_UNKNOWN || rc == RC_SUCCESS ))
    then
        :
        # Checking the given rg is configured for cloud backup or not
        :
        echo $cloud_bps | grep -wq $bp_name
        if (( $? != 0 ))
        then
            cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1204 "\nERROR: The provided resource group, %1\$s, is not configured for cloud backup.\n\n" "$bp_name" 1>&2
            cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1208 "Available resource groups which are configured for cloud are:\n\n" "$cloud_bps" 1>&2
            output=$(echo "$cloud_bps" | tr " " "\n")
            print -u2 "$output" 
            rc=$RC_INCORRECT_INPUT
        fi
    fi

    # Invoking Backup utility for creating Cloud backup
    if (( rc == RC_UNKNOWN || rc == RC_SUCCESS ))
    then
        print -- "$0()[$LINENO]($SECONDS): cl_cbm_backup -b $bp_name &" >>$CLMGR_TMPLOG # Always log command result
        LANG=C cl_cbm_backup -b $bp_name &
        pid=$!
        print -- "$0()[$LINENO]($SECONDS): cl_cbm_backup -b $bp_name & PID:$pid" >>$CLMGR_TMPLOG # Always log command result
        cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1221 "Backup process has been triggered in background with the pid %1\$s for a backup profile %2\$s.\n" "$pid" "$bp_name" 1>&2
    fi

    log_return_msg "$rc" "$0()" "$LINENO"
    return $rc
} # End of "KLIB_HACMP_add_backup()"

#================================================
# The following, commented line enforces coding
# standards when this file is edited via vim.
#================================================
# vim:tabstop=4:shiftwidth=4:expandtab:smarttab
#================================================
