90 lines
3.6 KiB
Bash
90 lines
3.6 KiB
Bash
#!/bin/sh
|
|
|
|
LOCKFILE="/tmp/restic.lock"
|
|
LOGFILE="/var/log/restic/restic_$(date +%Y%m%d-%H%M%S).log"
|
|
LATEST_RESTIC_VERSION="0.12.1"
|
|
|
|
# make log directory
|
|
if [ ! -d $(dirname ${LOGFILE}) ]; then
|
|
mkdir $(dirname ${LOGFILE}) > /dev/null
|
|
fi
|
|
|
|
if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
|
|
echo "Restic backup already running" >> ${LOGFILE}
|
|
exit
|
|
fi
|
|
|
|
# make sure the lockfile is removed when we exit and then claim it
|
|
trap "rm -f ${LOCKFILE}; exit" INT TERM EXIT
|
|
echo $$ > ${LOCKFILE}
|
|
|
|
# self update
|
|
cd $(dirname "$0")
|
|
git pull > /dev/null
|
|
|
|
# restic version update
|
|
INSTALLED_RESTIC_VERSION=$(restic version | awk '{print $2}')
|
|
if [ "${LATEST_RESTIC_VERSION}" != "${INSTALLED_RESTIC_VERSION}" ] || [ ! -e '/usr/bin/restic' ]; then
|
|
# Determine the Kernel and Architecture
|
|
KERNEL=$(uname)
|
|
ARCH=$(uname -m)
|
|
if [ ${KERNEL} == 'Linux' ]; then
|
|
if [ ${ARCH} == 'x86_64' ]; then
|
|
curl -vL -o /usr/bin/restic.bz2 https://github.com/restic/restic/releases/download/v${LATEST_RESTIC_VERSION}/restic_${LATEST_RESTIC_VERSION}_linux_amd64.bz2 >> ${LOGFILE} 2>&1
|
|
bzip2 -fvd /usr/bin/restic.bz2 >> ${LOGFILE}
|
|
chmod +x /usr/bin/restic >> ${LOGFILE}
|
|
elif [ ${ARCH} == 'aarch64' ]; then
|
|
curl -vL -o /usr/bin/restic.bz2 https://github.com/restic/restic/releases/download/v${LATEST_RESTIC_VERSION}/restic_${LATEST_RESTIC_VERSION}_linux_arm64.bz2 >> ${LOGFILE} 2>&1
|
|
bzip2 -fvd /usr/bin/restic.bz2 >> ${LOGFILE}
|
|
chmod +x /usr/bin/restic >> ${LOGFILE}
|
|
else
|
|
echo "Kernel Type: ${KERNEL}"
|
|
echo "Arch: ${ARCH}"
|
|
echo "Arch not supported."
|
|
exit
|
|
fi
|
|
elif [ ${KERNEL} == 'FreeBSD' ]; then
|
|
if [ ${ARCH} == 'amd64' ]; then
|
|
curl -vL -o /usr/bin/restic.bz2 https://github.com/restic/restic/releases/download/v${LATEST_RESTIC_VERSION}/restic_${LATEST_RESTIC_VERSION}_freebsd_amd64.bz2 >> ${LOGFILE} 2>&1
|
|
bzip2 -fvd /usr/bin/restic.bz2 >> ${LOGFILE}
|
|
chmod +x /usr/bin/restic >> ${LOGFILE}
|
|
else
|
|
echo "Kernel Type: ${KERNEL}"
|
|
echo "Arch: ${ARCH}"
|
|
echo "Arch not supported."
|
|
exit
|
|
fi
|
|
else
|
|
echo "Kernel Type: ${KERNEL}"
|
|
echo "Arch: ${ARCH}"
|
|
echo "Kernel not supported."
|
|
exit
|
|
fi
|
|
fi
|
|
|
|
# check if the /.duplicacy/preferences file exists before continuing
|
|
if [ ! -e "/.duplicacy/preferences" ] || [ ! -e "/.duplicacy/filters" ]; then
|
|
echo "ERROR: Duplicacy not yet configured. Please refer to readme file"
|
|
exit
|
|
fi
|
|
|
|
# run the backup
|
|
cd /
|
|
duplicacy -verbose -log backup -stats >> ${LOGFILE} 2>&1
|
|
|
|
# prune the backup
|
|
duplicacy -verbose -log prune -keep 0:360 -keep 30:180 -keep 7:30 -keep 1:7 >> ${LOGFILE} 2>&1
|
|
|
|
# copy the backup to offsite but only if the backup id as passed in
|
|
if [ "${1}" == "--copyOffsite" ] || [ "${1}" == "-c" ]; then
|
|
BACKUP_ID=$(awk '/"id"/{print substr($2,2,length($2)-3);exit}' /.duplicacy/preferences)
|
|
duplicacy -verbose -log copy -to GauntBackupCRO -id ${BACKUP_ID} >> ${LOGFILE} 2>&1
|
|
duplicacy -verbose -log copy -to GauntBackupGail -id ${BACKUP_ID} >> ${LOGFILE} 2>&1
|
|
duplicacy -verbose -log copy -to GauntBackupNAS -id ${BACKUP_ID} >> ${LOGFILE} 2>&1
|
|
fi
|
|
|
|
# compress the logs
|
|
gzip $(dirname ${LOGFILE})/*.log
|
|
|
|
# clean up lockfile
|
|
rm -f ${LOCKFILE} |