#!/bin/bash
export PBS_REPOSITORY=bck_ops@pbs@k3t-pbs.swallow-grue.ts.net:pbs-data
export PBS_PASSWORD=Wiry3-Finally-Juror-Glorious
export PBS_FINGERPRINT="e8:ca:7c:27:d1:1e:b3:07:ce:d8:32:38:a4:52:4f:1f:be:1b:fb:92:c4:28:1b:f3:24:e8:62:49:fa:c7:25:03"

export BACKUP_SOURCE="\
etc.pxar:/etc \
opt.pxar:/opt \
var.pxar:/var \
usr.pxar:/usr/local \
home.pxar:/home \
root.pxar:/root"

export BACKUP_EXCLUDE="--exclude={\
/media/**,\
/mnt/**,\
/rclone_cache/**,\
/tmp/**,\
/usr/bin/**,\
/var/lib/docker/**,\
/usr/lib/code-server/**,\
/usr/lib/firmware/**,\
/usr/lib/gcc/**,\
/usr/lib/jvm/**,\
/usr/lib/modules/**,\
/usr/lib/node_modules/**,\
/usr/local/**,\
/usr/lib/x86_64-linux-gnu/**,\
/usr/share/**,\
/var/lib/vz/**\
}"
if [ $(hostname) == "kur" ]; then
  export BACKUP_INCLUDE="\ 
  --include-dev /data"
  # INCLUDE="/data/docker_data $docker_data.pxar"
fi
if [ $(hostname) == "vmx-nas" ]; then
  exit 0
fi
#export BACKUP_INCLUDE="\
#--include-dev /srv/disk-by-id-0b911c7d-2079-4ea5-8891-a444cb10385b \
#"
#--include-dev /srv/disk-by-id-0d43f59a-e714-4163-a976-7c1ce9f92c57 \
#--include-dev /srv/disk-by-id-11eacddd-50cd-4b0e-b5b4-a98b5345bca5 \
#--include-dev /srv/disk-by-id-15da14fc-c756-4b9e-beb7-9bf924400f2f \
#--include-dev /srv/disk-by-id-2df0212d-0a74-4940-bc89-844fcf332dbc \
#--include-dev /srv/disk-by-id-39d56650-4ffb-4889-971f-b545c9a1bd6c \
#--include-dev /srv/disk-by-id-4e3bc302-6806-42d2-9dd7-c4e8f03ae1c4 \
#--include-dev /srv/disk-by-id-52e5a1e5-a43b-4317-9b2d-5d8fc7d4c04e \
#--include-dev /srv/disk-by-id-5a55ea1d-c559-4425-8f26-0259220aa4c6 \
#--include-dev /srv/disk-by-id-5b7fbdde-b61b-4655-8c36-d63e38815c5c \
#--include-dev /srv/disk-by-id-66932afc-654c-4abf-af4b-90de96c661a2 \
#--include-dev /srv/disk-by-id-71b18337-84fb-4b55-92c7-83b69cf27cc9 \
#--include-dev /srv/disk-by-id-7a3bd189-adfc-4f71-8ef3-5f27d2c15c30 \
#--include-dev /srv/disk-by-id-8539b6d6-eec8-4109-a1c9-65c555617946 \
#--include-dev /srv/disk-by-id-86f5c3b7-bbab-466e-bb9a-dcf804bb9c24 \
#--include-dev /srv/disk-by-id-89ee2b7a-8f20-4564-9a79-58566565ad97 \
#--include-dev /srv/disk-by-id-8c2a4baf-e2d2-4796-beee-02bfe8cf0ef4 \
#--include-dev /srv/disk-by-id-95b05b44-ec57-4401-9dd8-70e6f875512b \
#--include-dev /srv/disk-by-id-96fd1f61-ad2a-49b3-895d-da794eb5be95 \
#--include-dev /srv/disk-by-id-a9d8348d-dec9-4571-bf77-8baaf5c9dec7 \
#--include-dev /srv/disk-by-id-c0f7a397-d6a0-471f-8782-48bd80938042 \
#--include-dev /srv/disk-by-id-da94c155-57af-4c9e-837f-82078e026481 \
#--include-dev /srv/disk-by-id-e7f52bc0-2e8e-4766-8cc5-346de135f595 \
#--include-dev /srv/disk-by-id-f30a77ef-5e9d-4b77-9429-09a5f79ce6da \
#--include-dev /srv/disk-by-id-f5f3d09b-6aee-4d65-97f3-b4a488386f76 \
#"
apt-get clean cache
export HC_ID=$1
export BCK_INLUDE=$2
export BCK_EXCLUDE=$3
export BACKUP_COMMAND=""
export HC_COMMAND=""
[[ $EUID != 0 ]] && echo "sudo required!" && exit
[ "test$HC_ID" != "test"  ] && curl -fsS -m 10 --retry 5 -o /dev/null https://hc.noxs.me/ping/"$HC_ID"/start

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'
}
get_architecture() {
  ARCHI=""
  case $(uname -m) in
      i386 | i686)   	ARCHI="386" ;;
      x86_64) 		ARCHI="amd64" ;;
      arm | aarch64)    		dpkg --print-architecture | grep -q "arm64" && ARCHI="arm64" || ARCHI="arm" ;;
    armv7l)			ARCHI="old_arm" ;;
    *)   			echo "Unable to determine system architecture."; exit 1 ;;
  esac
}

strings=( /etc /opt /var /usr/local /home /root )
for i in "${strings[@]}"; do
	bck_include "$i"
done
get_architecture

if [ "$ARCHI" = "old_arm" ] ; then
	BACKUP_COMMAND=/usr/local/bin/pbs_client/proxmox-backup-client.sh
else
	BACKUP_COMMAND=proxmox-backup-client
fi
# Healthcheck ID provided
if [ -n "$HC_ID"  ] ; then
	HC_COMMAND="curl -fsS -m 10 --retry 5 -o /dev/null https://hc.noxs.me/ping/'$HC_ID'"
else
	HC_COMMAND="echo done"
fi

( "$BACKUP_COMMAND" backup $BACKUP_EXCLUDE $BACKUP_INCLUDE $BACKUP_SOURCE ) && $HC_COMMAND