MastodonHelp/web/clitools/maintain/maintain.bash

71 satır
2,3 KiB
Bash
Çalıştırılabilir Dosya

#!/bin/bash
trap "shutdown" SIGHUP SIGINT SIGQUIT SIGABRT SIGALRM SIGTERM
function shutdown {
log "Interrupted."
exit 0
}
sname=`basename "${0}"`
basedir=`dirname "${0}"`
cd "${basedir}/.."
mycfgfp="../conf/my.conf"
mastcfgfp="../conf/mustard.ini"
mailcfgfp="../conf/mail.conf"
logfp="maintain.log"
crlogfp="crawler.log"
dbname=`cat "${mastcfgfp}" | grep -P '^db_name=' | sed -e 's/^db_name=//' -e 's/^"//' -e 's/"$//'`
mailmta=`cat "${mailcfgfp}" | grep -P '^mta=' | sed -e 's/^mta=//' -e 's/^"//' -e 's/"$//'`
mailfrom=`cat "${mailcfgfp}" | grep -P '^from=' | sed -e 's/^from=//' -e 's/^"//' -e 's/"$//'`
mailto=`cat "${mailcfgfp}" | grep -P '^to=' | sed -e 's/^to=//' -e 's/^"//' -e 's/"$//'`
function log {
echo `date "+%Y-%m-%d %H:%M:%S.%N"` "${1}" >> "${logfp}"
}
# $1: comando; $2 file di log per stdout e stderr del comando
function logcmd {
log "Eseguo «${1}»..."
$1 &>> $2
ec=$?
if [ "${ec}" == "0" ]; then
log "Fatto, tutto ok :-)"
else
ok=0
log "Errore: ${ec}."
fi
}
[ -f "${logfp}" ] && rm "${logfp}"
ok=1
log "[[[ Inizio esecuzione ${sname} ]]]"
#tar cJf run-`date +%Y-%m-%d--%H.%M.%S`.tar.xz run/
buff=`ls -1 run/`
[ "${buff}" != "" ] && logcmd "find run/ -type f -exec rm {} ;" "${logfp}"
logcmd "php fedipact.php" "${logfp}"
logcmd "php mustool.php updstats" "${logfp}"
if [ `date "+%d"` != "01" ]; then # usa il gracetime di default
log "Eseguo crawler.php con il gracetime di default"
logcmd "php crawler.php" "${crlogfp}"
else # il primo del mese usa un gracetime di 2 mesi
log "Eseguo crawler.php con un gracetime di 2 mesi"
logcmd "php crawler.php -g 2M" "${crlogfp}"
fi
logcmd "php mustool.php shuffle clean optimize" "${logfp}"
ofp="maintain/backups/${dbname}_backup-`date +%Y-%m-%d--%H.%M.%S`.sql"
logcmd "mysqldump --defaults-extra-file=${mycfgfp} --single-transaction -r ${ofp} ${dbname}" "${logfp}"
if [ "${ec}" != "0" ]; then
[ -f "${ofp}" ] && rm "${ofp}"
else
logcmd "xz -9 ${ofp}" "${logfp}"
fi
[ $ok -eq 1 ] && subj="maintain.bash: tutto ok :-)" || subj="maintain.bash: ci sono errori :-("
body="[[[ ${logfp} ]]]\n\n"
body+=`cat "${logfp}"`
body+="\n\n[[[ Ultime 20 righe di ${crlogfp} ]]]\n\n"
body+=`tail -n 20 "${crlogfp}"`
echo -e "${body}" | s-nail -s "${subj}" -S v15-compat -S from="${mailfrom}" -S smtp-use-starttls -S mta="${mailmta}" "${mailto}"
[ $ok -eq 1 ] && exit 0 || exit 1