Files
duplicacy-scripts/restic.sh

108 lines
4.3 KiB
Bash

#!/bin/sh
# restic.sh [Hostname] [-c|--copyOffsite]
# the hostname must be entered and be the first argument and is required
# the -c is optional and must be the second arguments
LOCKFILE="/tmp/restic.lock"
LOGFILE="/var/log/restic/restic_$(date +%Y%m%d-%H%M%S).log"
LATEST_RESTIC_VERSION="0.12.1"
export RESTIC_REPOSITORY="sftp://root@gauntnas.home.johnhgaunt.com:50022//mnt/restic"
export RESTIC_PASSWORD="restic"
case ${1} in
"GauntBitwarden")
BACKUP_DIRS="/etc /opt/bitwarden /var/spool/cron"
;;
"GauntNAS")
BACKUP_DIRS="/etc/version /data /root --exclude=/root/.cache"
export RESTIC_REPOSITORY="/mnt/data/backups/restic"
;;
*)
echo "Error: Hostname, ${1}, didn't match any known matches."
exit 1
;;
esac
# 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
# run the backup
restic --cleanup-cache --verbose backup ${BACKUP_DIRS} >> ${LOGFILE} 2>&1
# prune the backup
#restic --verbose forget --keep-daily 7 --keep-weekly 5 --keep-monthly 12 --keep-yearly 75 --prune >> ${LOGFILE} 2>&1
# copy the backup to offsite if requested
if [ "${2}" == "--copyOffsite" ] || [ "${2}" == "-c" ]; then
#$ restic -r /srv/restic-repo copy --repo2 /srv/restic-repo-copy --host luigi
restic --verbose copy --repo2 sftp://root@gauntbackupCRO.home.johnhgaunt.com://backup/restic --host ${1} --copy-chunker-params >> ${LOGFILE} 2>&1
restic --verbose copy --repo2 sftp://root@gauntbackupGail.home.johnhgaunt.com://backup/restic --host ${1} --copy-chunker-params >> ${LOGFILE} 2>&1
restic --verbose copy --repo2 sftp://root@gauntbackupNAS.home.johnhgaunt.com://backup/restic --host ${1} --copy-chunker-params >> ${LOGFILE} 2>&1
restic --verbose copy --repo2 sftp://root@GauntBackupGCE01.home.johnhgaunt.com://backup/restic --host ${1} --copy-chunker-params >> ${LOGFILE} 2>&1
fi
# compress the logs
gzip $(dirname ${LOGFILE})/*.log
# clean up lockfile
rm -f ${LOCKFILE}