70 lines
1.4 KiB
Bash
Executable file
70 lines
1.4 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# cron: 0 1 * * * rdiffbak.sh &
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
# -------------
|
|
# CONFIG
|
|
# -------------
|
|
|
|
MAILTO="me@example.com"
|
|
MAILSUB="backup report"
|
|
# name of the entry in ~/.mailrc
|
|
MAILSRV="example"
|
|
|
|
SRC="/home/foobar"
|
|
DST="/data/foobar"
|
|
LOG="/data/bkplog"
|
|
MAXAGE="1M"
|
|
|
|
RDIFF="/usr/bin/rdiff-backup"
|
|
OPTIONS="--exclude-fifos --exclude-sockets --print-statistics"
|
|
|
|
TIMESTAMP=$(date +%Y-%m-%d_%H%M)
|
|
LOGFILE="${LOG}/${TIMESTAMP}_backup.log"
|
|
|
|
mkdir -p "$LOG" || exit 1
|
|
function mailme() {
|
|
# ~/.mailrc needs to be configured
|
|
if [[ -f "${LOGFILE}" ]]; then
|
|
echo " -- EOF -- " >> "${LOGFILE}"
|
|
# shellcheck disable=SC2002
|
|
cat "${LOGFILE}" | mail -A "$MAILSRV" -s "${MAILSUB}" ${MAILTO}
|
|
fi
|
|
}
|
|
|
|
# ---------------
|
|
# BACKUP
|
|
# ---------------
|
|
|
|
echo " -- $TIMESTAMP -- " >> "${LOGFILE}"
|
|
|
|
if [[ ! -d "$SRC" ]]; then
|
|
echo "$TIMESTAMP Invalid SRC, aborting... " >> "${LOGFILE}"
|
|
mailme
|
|
exit 1
|
|
fi
|
|
if [[ ! -d "$DST" ]]; then
|
|
echo "$TIMESTAMP Invalid DST, aborting... " >> "${LOGFILE}"
|
|
mailme
|
|
exit 1
|
|
fi
|
|
|
|
echo "Backup: $RDIFF $OPTIONS $SRC $DST" >> "${LOGFILE}"
|
|
|
|
$RDIFF "$OPTIONS" "$SRC" "$DST" >> "${LOGFILE}"
|
|
RDRES=$?
|
|
|
|
# It went well, remove stuff older than MAXAGE
|
|
if [[ $RDRES -eq 0 ]]; then
|
|
$RDIFF --force --remove-older-than "$MAXAGE" "$DST" >> "${LOGFILE}"
|
|
else
|
|
echo "rdiff backup failed, exit: $RDRES" >> "${LOGFILE}"
|
|
mailme
|
|
exit 1
|
|
fi
|
|
|
|
find "${LOG}" -type f -mtime +30 -delete
|
|
mailme
|
|
exit 0
|