#!/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"
BACKUP_COMMAND=proxmox-backup-client

vmx-nas_SRC=$1

source /opt/scripts_local/shared_functions.sh
sudo_check
DST_PATH=/opt/scripts_local
PART1="rsync -avP -e 'ssh -o StrictHostKeyChecking=no' root@vmx-nas.swallow-grue.ts.net:/mnt/union/shared/bck2pbs"
PART2="$DST_PATH/ >/dev/null 2>&1"
MY_CMD="$PART1/$HOSTNAME/bck2pbs_source.txt $PART2"

if [ "$HOSTNAME" == "vmx-nas" ]; then
 	rsync -avP -e ssh -o StrictHostKeyChecking=no "/mnt/union/shared/bck2pbs/vmx-nas/$KET_NAS_SRC" "$DST_PATH/bck2pbs_source.txt" >/dev/null 2>&1
 	# rsync -avP -e ssh -o StrictHostKeyChecking=no /mnt/union/shared/bck2pbs/vmx-nas/bck2pbs_source.txt "$DST_PATH/" >/dev/null 2>&1
 	rsync -avP -e ssh -o StrictHostKeyChecking=no /mnt/union/shared/bck2pbs/vmx-nas/bck2pbs_specific.sh "$DST_PATH/" >/dev/null 2>&1
else
	if ! eval "$MY_CMD" ; then
		# MY_CMD="$PART1/bck2pbs_source.txt $PART2"
		eval "$PART1/bck2pbs_source.txt $PART2"
		# MY_CMD="$PART1/bck2pbs_specific.sh $PART2"
		eval "$PART1/bck2pbs_specific.sh $PART2"
	else
		eval "$PART1/$HOSTNAME/bck2pbs_specific.sh $PART2"
	fi
fi


sudo apt-get clean cache
cat /opt/scripts_local/bck2pbs_source.txt
source /opt/scripts_local/bck2pbs_source.txt
cat /opt/scripts_local/bck2pbs_specific.sh
source /opt/scripts_local/bck2pbs_specific.sh

_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

MYCOMMAND="$BACKUP_COMMAND backup $BACKUP_SOURCE"
echo "$MYCOMMAND"

$MYCOMMAND