Highly Available and Scalable Information System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
11 KiB

#!/bin/bash
#EuryBOX detect functions file
#Desc: detect archive content from description file
#1 arg required: detected_archive_num
eurybox_detect_archive_content ()
{
local ARCH_NUM=$1
local DESTINATION=${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FILE]}
local BKP_ENC
local BKP_COMP
local BKP_TYPE
local BKP_VERS
local BKP_ECC
local BKP_VMS_LIST
local STATUS
local ARCH_DESC=`sudo cat $DESTINATION 2>&1`
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
#FIXME: STATUS IS NOT 0 WHEN FORBIDEN TO ACCESS FILE
eurybox_display_message warning DETECT "Archive description access failed: \n$ARCH_DESC"
else
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,DESC]=$ARCH_DESC
eurybox_display_message debug DETECT "Archive description access success: \n$ARCH_DESC"
BKP_ENC=`echo "${ARCH_DESC}" | grep "ENCRYPTED_ARCHIVE" | awk -F":" '($NF == "true") || ($NF == "false") {print $NF}' 2>&1`
if [[ $BKP_ENC = "" ]]
then
eurybox_display_message warning DETECT "Archive description encryption search failed"
else
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,ENC]=$BKP_ENC
eurybox_display_message debug DETECT "Archive description encryption search success - found: $BKP_ENC"
fi
BKP_COMP=`echo "${ARCH_DESC}" | grep "ARCHIVE_FORMAT" | awk -F":" '{print $NF}' 2>&1`
case $BKP_COMP in
"" )
eurybox_display_message warning DETECT "Archive description compression/format search failed"
;;
tar )
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FORMAT]=$BKP_COMP
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,COMP]="none"
eurybox_display_message debug DETECT "Archive description compression/format search success - found: none"
;;
tar.gz|tar.bz2 )
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FORMAT]=$BKP_COMP
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,COMP]=$BKP_COMP
eurybox_display_message debug DETECT "Archive description compression/format search success - found: $BKP_COMP"
;;
* )
eurybox_display_message warning DETECT "Archive description compression/format search failed - unknown backup format found: $BKP_COMP"
;;
esac
BKP_TYPE=`echo "${ARCH_DESC}" | grep "ARCHIVE_TYPE" | awk -F":" '{print $NF}' 2>&1`
case $BKP_TYPE in
"" )
eurybox_display_message warning DETECT "Archive description type search failed"
;;
vm_cold|vm_hot|full_hot|full_cold )
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TYPE]=$BKP_TYPE
eurybox_display_message debug DETECT "Archive description type search success - found: $BKP_TYPE"
;;
* )
eurybox_display_message warning DETECT "Archive description type search failed - unknown backup type found: $BKP_TYPE"
;;
esac
BKP_VERS=`echo "${ARCH_DESC}" | grep "ARCHIVE_VERSION" | awk -F":" '{print $NF}' 2>&1`
case $BKP_VERS in
"" )
eurybox_display_message warning DETECT "Archive description version search failed"
;;
1.0 )
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VERS]=$BKP_VERS
eurybox_display_message debug DETECT "Archive description version search success - found: $BKP_VERS"
;;
* )
eurybox_display_message warning DETECT "Archive description version search failed - unknown backup version found: $BKP_VERS"
;;
esac
BKP_ECC=`echo "${ARCH_DESC}" | grep "FEC_FORMAT" | awk -F":" '{print $NF}' 2>&1`
case $BKP_ECC in
"" )
eurybox_display_message warning DETECT "Archive description correcting codes search failed"
;;
par2|zfec )
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FEC]=$BKP_ECC
eurybox_display_message debug DETECT "Archive description correcting codes search success - found: $BKP_ECC"
;;
* )
eurybox_display_message warning DETECT "Archive description correcting codes search failed - unknown backup correcting codes found: $BKP_ECC"
;;
esac
BKP_HASH=`echo "${ARCH_DESC}" | grep "HASH_FORMAT" | awk -F":" '{print $NF}' 2>&1`
case $BKP_HASH in
"" )
eurybox_display_message warning DETECT "Archive description hash search failed"
;;
sha256|sha512 )
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,HASH]=$BKP_HASH
eurybox_display_message debug DETECT "Archive description hash search success - found: $BKP_HASH"
;;
* )
eurybox_display_message warning DETECT "Archive description hash search failed - unknown backup hash found: $BKP_HASH"
;;
esac
BKP_VMS_LIST=( $(echo "${ARCH_DESC}" | awk '{ if(content == 1) {print $NF} else { if($0 == "ARCHIVE_CONTENT:") {content=1} } }' | grep "libvirt.xml") )
if [[ ${BKP_VMS_LIST[@]} = "" ]]
then
eurybox_display_message warning DETECT "Archive description VMs search failed"
else
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VM_NUM]=${#BKP_VMS_LIST[@]}
eurybox_display_message debug DETECT "${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VM_NUM]} archive VM(s) found"
eurybox_display_message debug DETECT "Detected:\n${BKP_VMS_LIST[*]}"
for (( VM_NUM=0;VM_NUM<${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VM_NUM]};VM_NUM++ ))
do
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,$VM_NUM,FILE]=${BKP_VMS_LIST[$VM_NUM]}
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,$VM_NUM,NAME]=`echo "${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,$VM_NUM,FILE]}" | awk -F"/" '{ print $NF }' | awk -F"." '{ for(i=1; i<(NF-1); i++) { if(i==1) { name=$i } else { name=name"."$i } } ; print name }'`
eurybox_display_message debug DETECT "Archive VM detected: ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,$VM_NUM,NAME]} / filename: ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,$VM_NUM,FILE]}"
done
fi
fi
}
#Desc: detect archives in specified directory
#1 arg required: search_path
eurybox_detect_archives ()
{
local DESTINATION=$1
local BKP_LIST
local FILES_LIST=`sudo ls $DESTINATION 2>&1`
local STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message error DETECT "Archive description listing failed - error $STATUS\n$FILES_LIST"
else
#Filter by desc extension, extract filename and store in an array
BKP_LIST=( $(echo "$FILES_LIST" | awk -F"." '($NF == "desc") {print $0}' | awk -F"/" '{print $NF}') )
if [[ ${BKP_LIST[@]} = "" ]]
then
eurybox_display_message error DETECT "No archive found in following files\n$FILES_LIST"
else
EURYBOX_DETECTED_ARCHIVES[NUM]=${#BKP_LIST[@]}
eurybox_display_message message DETECT "${EURYBOX_DETECTED_ARCHIVES[NUM]} archive description(s) found"
eurybox_display_message debug DETECT "Detected:\n${BKP_LIST[@]}"
for (( ARCH_NUM=0;ARCH_NUM<${EURYBOX_DETECTED_ARCHIVES[NUM]};ARCH_NUM++ ))
do
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FILE]=${BKP_LIST[$ARCH_NUM]}
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FILENAME]=`echo "${BKP_LIST[$ARCH_NUM]}" | awk -F"." '{ for(i=1; i<(NF); i++) { if(i==1) { filename=$i } else { filename=filename"."$i } } ; print filename }'`
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VERSION]=`echo "${BKP_LIST[$ARCH_NUM]}" | awk -F"." '{ for(i=1; i<(NF); i++) { if(i==1) { version=$i } else { version=version"."$i } } ; print version }'`
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,NAME]=`echo "${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VERSION]}" | awk -F"_" '{ for(i=1; i<(NF-5); i++) { if(i==1) { name=$i } else { name=name"_"$i } } ; print name }'`
EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]=`echo "${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VERSION]}" | awk -F"_" '{ for(i=(NF-5); i<=NF; i++) { if(i==(NF-5)) { name=$i } else { name=name"_"$i } } ; print name }'`
eurybox_display_message debug DETECT "Archive detected: ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,NAME]} / timestamp: ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,TIME]} / version: ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,VERSION]} / filename: ${EURYBOX_DETECTED_ARCHIVES[$ARCH_NUM,FILE]}"
done
fi
fi
}
#Desc: detect the libvirt environment
#No arg required
eurybox_detect_libvirt_env ()
{
local NETWORKS
local DOMAINS
local DISKS
#Detect networks
NETWORKS=( $(sudo virsh $EURYBOX_VIRSH_OPTIONS net-list --all | awk '{print $1}') )
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message error DETECT "Error on hypervisor network detection"
else
if [[ !($NETWORKS = "") ]]
then
EURYBOX_DETECTED_NETWORKS[NUM]=${#NETWORKS[@]}
eurybox_display_message message DETECT "Hypervisor networks - detection OK: ${EURYBOX_DETECTED_NETWORKS[NUM]} virtual network(s) found"
for (( NET_NUM=0;NET_NUM<${EURYBOX_DETECTED_NETWORKS[NUM]};NET_NUM++ ))
do
EURYBOX_DETECTED_NETWORKS[NAME,$NET_NUM]=${NETWORKS[NET_NUM]}
eurybox_display_message debug DETECT "Network detected: ${EURYBOX_DETECTED_NETWORKS[NAME,$NET_NUM]}"
done
else
eurybox_display_message warning DETECT "No hypervisor network detected"
fi
fi
#Detect domains
DOMAINS=( $(sudo virsh $EURYBOX_VIRSH_OPTIONS list --all | awk '{print $2}') )
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message error DETECT "Error on hypervisor domain detection"
else
if [[ !($DOMAINS = "") ]]
then
EURYBOX_DETECTED_DOMAINS[NUM]=${#DOMAINS[@]}
eurybox_display_message message DETECT "Hypervisor domains (VM) - detection OK: ${EURYBOX_DETECTED_DOMAINS[NUM]} domain(s) found"
for (( DOM_NUM=0;DOM_NUM<${EURYBOX_DETECTED_DOMAINS[NUM]};DOM_NUM++ ))
do
EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]=${DOMAINS[DOM_NUM]}
eurybox_display_message debug DETECT "VM detected: ${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]}"
done
else
eurybox_display_message warning DETECT "No hypervisor domain detected"
fi
fi
#Detect disks associated to each domain
for (( DOM_NUM=0;DOM_NUM<${EURYBOX_DETECTED_DOMAINS[NUM]};DOM_NUM++ ))
do
DISKS=( $(sudo virsh $EURYBOX_VIRSH_OPTIONS domblklist ${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]} | awk '($2 != "-") {print $2}') )
STATUS=$?
if [[ !($STATUS -eq 0) ]]
then
eurybox_display_message error DETECT "Error on VM disk detection: ${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]}"
else
if [[ !($DISKS = "") ]]
then
EURYBOX_DETECTED_DISKS[${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]},NUM]=${#DISKS[@]}
eurybox_display_message message DETECT "VM ${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]} - ${EURYBOX_DETECTED_DISKS[${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]},NUM]} disk(s) found"
for (( DISK_NUM=0;DISK_NUM<${EURYBOX_DETECTED_DISKS[${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]},NUM]};DISK_NUM++ ))
do
EURYBOX_DETECTED_DISKS[${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]},NAME,$DISK_NUM]=${DISKS[DISK_NUM]}
eurybox_display_message debug DETECT "VM ${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]} - disk detected: ${EURYBOX_DETECTED_DISKS[${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]},NAME,$DISK_NUM]}"
done
else
eurybox_display_message warning DETECT "No domain disk detected for VM ${EURYBOX_DETECTED_DOMAINS[NAME,$DOM_NUM]}"
fi
fi
done
}