#!/bin/bash
export PBS_REPOSITORY=bck_ops@pbs@vmx-pbs.tail:kut-backup
export PBS_PASSWORD=Wiry3-Finally-Juror-Glorious
export PBS_FINGERPRINT="58:0d:62:d1:64:18:8a:7b:fd:56:71:45:47:28:53:a3:dc:9e:b5:74:4b:d2:f3:1f:56:6c:14:d5:98:13:43:40"
apt-get clean cache
HC_ID=$1
EXCLUDE1=$2

bck_include() {
# Includes in  the backup set if $1 is a file or a directory
# This is necessary to have a backup without errors when the requested dir does not exists
# e.g. /etc/pve exists only on Proxmox host machines
if [ -d "$1" ] || [ -f "$1" ]
then
        INCLUDE_CLEAN=$(escape_spec_char "$1")
        INCLUDE="$INCLUDE $INCLUDE_CLEAN.pxar:$1"  
fi
}

escape_spec_char()
{
  # (rev | sed -e 's/|/`/' | rev) --> reverse the string and replace the 1st / (last / before rev) with nothing then rev again
  # (sed -e 's/\///') --> replace the 1st / with nothing 
  # (-e 's/\//\-/g') --> replace the following / with _
  # -e ()'s/\./\_/g') --> replace the . with _
   echo "$1" | rev | sed -e 's/|/`/' | rev | sed -e 's/\///' -e 's/\//\-/g' -e 's/\./\_/g'  
}

strings=(
    /etc/pve
)
for i in "${strings[@]}"; do
  bck_include "$i"
done


cd /usr/local/bin/pbs_client
./proxmox-backup-client.sh backup root.pxar:/ $INCLUDE && curl -m 10 --retry 5 https://hc.noxs.me/ping/"$HC_ID"
