Skip to content

Instantly share code, notes, and snippets.

@kalos
Last active September 10, 2020 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalos/0fcd19f665b94d2bc982415ce7680414 to your computer and use it in GitHub Desktop.
Save kalos/0fcd19f665b94d2bc982415ce7680414 to your computer and use it in GitHub Desktop.
script I use to run btrbk backup (copy in /etc/cron.hourly/)
#!/usr/bin/env bash
# alarm check if launch this script with 'onlycheck' as fist param
test "${1}" != 'onlycheck' && \
(
if /home/kalos/.bin/check_desegno_network.sh ; then
# if I'm at work, run btrbk (resume + run) using config with only external disk that I have at work
/usr/sbin/btrbk -c /etc/btrbk/btrbk-desegno.conf resume && systemd-inhibit --who="btrbk" --why="Prevent interrupting btrbk scheduled backup" /usr/sbin/btrbk -c /etc/btrbk/btrbk-desegno.conf -q run >/dev/null 2>&1
else
# run btrbk (resume + run) using principal config, with only external disk I have at home
/usr/sbin/btrbk resume && systemd-inhibit --who="btrbk" --why="Prevent interrupting btrbk scheduled backup" /usr/sbin/btrbk -q run >/dev/null 2>&1
fi
)
# create logfile for alarm and reports
LOG_DIR='/var/log/btrbk'
test ! -d ${LOG_DIR} && mkdir /var/log/btrbk
### WRITE LOG
## snapshot/backup of targets
# cycle targets
btrbk list target --format=raw | while read target; do
# remove the '"' in the the string
declare ${target//\"/}
## save targets latest snapshot/backup
# cycle latest snapshot/backup
btrbk list latest --format=raw "${target_path}" 2>/dev/null | while read latest; do
# remove the '"' in the the string
declare ${latest//\"/}
# skip target without snaphost/backup (probably not connected)
if [[ "${target_path}" = '' ]]
then
continue
fi
# use target path + name of the snap/back as filename
file_log_name="`dirname ${target_path} | tr '/' '_'`_`basename ${target_path} | cut -d. -f1`.latest"
# save date of latest snap/back
basename "${target_path}" | cut -f2 -d. > "${LOG_DIR}/${file_log_name}"
done
## save targets stats
if [[ ! `btrbk stats "${target_path}" 2>/dev/null | tail -1` = *"0 backup"* ]]
then
file_stat_name="`dirname ${target_path} | tr '/' '_'`.stats"
btrbk stats "${target_path}" 2>/dev/null > "${LOG_DIR}/${file_stat_name}"
fi
## save targets usage
# save ongly if target is online
if [[ -z "`btrbk usage "${target_path}" 2>&1 >/dev/null`" ]]
then
file_usage_name="`dirname ${target_path} | tr '/' '_'`.usage"
btrbk usage "${target_path}" 2>/dev/null > "${LOG_DIR}/${file_usage_name}"
fi
## save list of targets backups
if [[ `btrbk list backups "${target_path}" 2>/dev/null | wc -l` -gt 2 ]]
then
file_list_name="`dirname ${target_path} | tr '/' '_'`.list"
btrbk list backups "${target_path}" 2>/dev/null > "${LOG_DIR}/${file_list_name}"
fi
done
## LATEST snapshot of source
# cycle sources
btrbk list source --format=raw | while read source; do
# remove the '"' in the the string
declare ${source//\"/}
# cycle latest snapshot
btrbk list snapshots --format=raw ${source_url} 2>/dev/null | tail -1 | while read latest; do
# remove the '"' in the the string
declare ${latest//\"/}
# skip source without snaphost
if [[ "${snapshot_path}" = '' ]]
then
continue
fi
# use source path + name of the snap as filename
file_log_name="`dirname ${snapshot_path} | tr '/' '_'`_`basename ${snapshot_path} | cut -d. -f1`"
# save date of latest snap
basename ${snapshot_path} | cut -f2 -d. > "${LOG_DIR}/${file_log_name}.latest"
done
done
### ALARMS
local_root="`cat ${LOG_DIR}/_mnt_btrfs_pool_@backups_@.latest`"
local_home="`cat ${LOG_DIR}/_mnt_btrfs_pool_@backups_@home.latest`"
#minijng_root="`cat ${LOG_DIR}/_mnt_minij-ng_backups_@.latest`"
#minijng_home="`cat ${LOG_DIR}/_mnt_minij-ng_backups_@home.latest`"
#minijng_multimedia="`cat ${LOG_DIR}/_mnt_minij-ng_backups_@multimedia.latest`"
minij_root="`cat ${LOG_DIR}/_mnt_minij_backups_@.latest`"
minij_home="`cat ${LOG_DIR}/_mnt_minij_backups_@home.latest`"
# 1: file - 2: date - 3: name
alarm() {
file_date=`date -d "${1//T/ }" +'%s'`
date_threshold=`date -d "now - ${2}" +'%s'`
date_now=`date -d "now" +'%s'`
#se il backup è più vecchio di tot tempo, allarma
if [[ "${date_threshold}" -gt "${file_date}" ]] || [[ -z "${file_date}" ]]
then
echo "${3} backuo is too old!"
echo "(last backup: `date -d @${file_date}` - threshold: `date -d @${date_threshold}` - diff: $(((${date_now} - ${file_date})/86400)) days"
echo ""
# notify-send
/home/kalos/.bin/notify.sh -u critical btrbk "${3} backup is too old!"
fi
}
# allarmi su disco esterno (jb, duplicato) disabilitati per COVID
alarm "$local_root" "2 hour" "LOCAL (root)"
alarm "$local_home" "2 hour" "LOCAL (home)"
#alarm "$minijng_root" "7 days" "minij (root)"
#alarm "$minijng_home" "7 days" "minij (home)"
#alarm "$minijng_multimedia" "7 days" "minij (multimedia)"
alarm "$minij_root" "14 days" "minij (root)"
alarm "$minij_home" "14 days" "minij (home)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment