containers-borg/backup.sh

48 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
# Password can't be empty
test -n "${BORG_PASSPHRASE}"
test -n "${DEST}"
test -n "${SSH_ALIAS}"
# Defaults
SSH_PORT="${SSH_PORT:-22}"
SSH_SERVER="${SSH_SERVER:-${SSH_ALIAS}}"
SSH_USER="${SSH_USER:-root}"
umask 077
DATE="$(date +%F)"
ORIG=/srv/data
if ! grep -q "${SSH_ALIAS}" /root/.ssh/config ; then
echo "Host ${SSH_ALIAS}" >> /root/.ssh/config
echo " Port ${SSH_PORT}" >> /root/.ssh/config
echo " HostName ${SSH_SERVER}" >> /root/.ssh/config
echo " User ${SSH_USER}" >> /root/.ssh/config
fi
touch /root/.ssh/known_hosts
if ! ssh-keygen -F "[${SSH_SERVER}]:${SSH_PORT}"; then
echo "${SSH_KNOWN_HOSTS}" >> /root/.ssh/known_hosts
fi
ssh -fN ${SSH_ALIAS}
for signal in TERM QUIT HUP EXIT INT KILL; do
trap "ssh -O exit \"${SSH_ALIAS}\"" ${signal}
done
# It'll fail the second time
borg init --encryption=repokey "${SSH_ALIAS}:${DEST}" 2>&1 | tee /tmp/backup.log
borg create -v --stats "${SSH_ALIAS}:${DEST}::${DATE}" "${ORIG}" 2>&1 | tee -a /tmp/backup.log
borg prune -v --stats --keep-daily ${KEEP} "${SSH_ALIAS}:${DEST}" 2>&1 | tee -a /tmp/backup.log
borg list "${SSH_ALIAS}:${DEST}" 2>&1 | tee -a /tmp/backup.log
if test -n "${ZULIP_KEY}"; then
cat /tmp/backup.log | zulip-client -u "${ZULIP_URL}" -b "${ZULIP_BOT}" -s "${ZULIP_STREAM}" -t "${ZULIP_TOPIC}"
fi
exit $?