borgbackup/templates/bin/borgbackup.sh.j2

51 lines
2.4 KiB
Django/Jinja

#!/bin/bash
# Set variables
BORG_USER="{{ item.borguser }}"
BORG_SERVER="{{ item.borgserver }}"
BORG_REPO_PATH="{{ borg_backup_dir }}{{ item.hostname }}/{{ item.repo_name }}"
BORG_SOURCE_PATH="{{ item.backup_path }}"
BORG_LOG="{{ borg_log_dir }}{{ item.hostname }}_{{ item.repo_name }}.log"
TEMPBORG_LOG="/tmp/{{ item.hostname }}_{{ item.repo_name }}.log"
BORG_STATS_FILE="{{ borg_log_dir }}{{ item.hostname }}_{{ item.repo_name }}.stats"
BORG_BACKUP_SERVER="${BORG_USER}@${BORG_SERVER}"
# Initiate borg repository if not existing
export BORG_PASSPHRASE="{{ item.repo_password }}"
borg info ${BORG_BACKUP_SERVER}:${BORG_REPO_PATH}
if [ ${?} == 2 ]; then
borg init --encryption=repokey ${BORG_BACKUP_SERVER}:${BORG_REPO_PATH}
fi
# Run backup keep the last {{ item.retention }} days
borg create --stats ${BORG_BACKUP_SERVER}:${BORG_REPO_PATH}::$(date +'%Y-%m-%dT%H:%M:%S') ${BORG_SOURCE_PATH} > ${TEMPBORG_LOG} 2>&1
borg prune --stats -d {{ item.retention }} ${BORG_BACKUP_SERVER}:${BORG_REPO_PATH}
cat ${TEMPBORG_LOG} >> ${BORG_LOG}
{% if item.report_zabbix is defined and item.report_zabbix == 'true' %}
# Send backup report to ZABBIX
BORG_ZABBIX_URL="{{ item.zabbix_url }}"
borg_origsize=$(borg info ${BORG_BACKUP_SERVER}:${BORG_REPO_PATH} | grep "All archives:" | awk '{print $3}')
borg_compressedsize=$(borg info ${BORG_BACKUP_SERVER}:${BORG_REPO_PATH} | grep "All archives:" | awk '{print $5}')
zabbix_sender --tls-connect=psk --tls-psk-identity="Bog Backup" --tls-psk-file=/etc/bogbackup/borgbackup.psk -z ${BORG_ZABBIX_URL} -p 10051 -s ${BORG_SERVER} -k borg.{{ item.hostname }}-{{ item.repo_name }}_backupsize -o ${borg_origsize}
zabbix_sender --tls-connect=psk --tls-psk-identity="Bog Backup" --tls-psk-file=/etc/bogbackup/borgbackup.psk -z ${BORG_ZABBIX_URL} -p 10051 -s ${BORG_SERVER} -k borg.{{ item.hostname }}-{{ item.repo_name }}_compressedsize -o ${borg_compressedsize}
{% endif %}
{% if item.report_xmpp is defined and item.report_xmpp == 'true' %}
# Send backup report to XMPP
source {{ borg_xmppshouter_cfg }}
echo "💾️ BorgBackup Stats for {{ item.hostname }} - {{ item.repo_name }} 💾️" >> ${BORG_STATS_FILE}
awk '/^-+$/ {count++} count==2, count==3 {if(count==1) next; print}' ${TEMPBORG_LOG} >> ${BORG_STATS_FILE}
echo "------------------------------------------------------------------------------" >> ${BORG_STATS_FILE}
cat ${BORG_STATS_FILE} | $SENDXMPP
rm ${BORG_STATS_FILE}
{% endif %}
#Remove borg password and exit
unset BORG_PASSPHRASE
rm ${TEMPBORG_LOG}