2020-04-24 23:07:40 +02:00
#! /bin/bash
### GENERAL SETTINGS ###
2021-12-26 17:54:40 +01:00
DEBUG = "true" #don't send mail if in debug mode
2020-04-25 10:26:18 +02:00
timestampFolder = "index" #folder for reading timestamp
2021-12-26 17:54:40 +01:00
backupFolder = "/mnt/hdd/backups/" #directory that contains all repos
2020-04-25 10:26:18 +02:00
########################
### eMail ###
2021-12-26 17:54:40 +01:00
receiverEmail = "localhorst@mosad.xyz"
2022-02-18 11:38:02 +01:00
senderEmail = "______________@mosad.xyz"
2020-04-24 23:07:40 +02:00
########################
######### REPO SETTINGS #########
2022-02-18 11:38:02 +01:00
repos_name[ 0] = "Name"
repos_directory[ 0] = "path"
2020-04-25 10:26:18 +02:00
repos_interval[ 0] = 21600 #sec
repos_interval_tolerance[ 0] = 10 #%
2020-04-24 23:07:40 +02:00
2021-12-26 17:54:40 +01:00
#repos_name[1]="another backup repo"
#repos_directory[1]="another_repo"
2020-04-24 23:07:40 +02:00
#repos_interval[1]=21600 #sec
#repos_interval_tolerance[1]=10 #%
2022-02-18 11:38:02 +01:00
#################################
2020-04-24 23:07:40 +02:00
2021-12-26 17:54:40 +01:00
#repos_name[2]="another backup repo"
2020-04-24 23:07:40 +02:00
#repos_directory[2]="another_repo"
#repos_interval[2]=21600 #sec
#repos_interval_tolerance[2]=10 #%
#################################
function fn_convertSecondsToHumanReadable( ) {
fn_result = " $( echo $(( $1 / 86400 )) d $(( $(( $1 - $1 / 86400 * 86400 )) / 3600 )) h:$(( $(( $1 - $1 / 86400 * 86400 )) % 3600 / 60 )) m:$(( $(( $1 - $1 / 86400 * 86400 )) % 60 )) s) "
}
echo "Starting AutoBackup Watchdog"
echo " Local time: $( date) "
2020-04-25 10:26:18 +02:00
cd $backupFolder #jump to backup folder that contains the repos
2020-04-24 23:07:40 +02:00
repo_count = $(( " ${# repos_name [@] } " ))
echo " Repo Count: $repo_count "
repo_count = $(( $repo_count - 1 ))
for i in $( eval echo " {0.. $repo_count } " ) #loop through all repos
do
echo " "
echo " Checking repo: ${ repos_name [i] } "
2020-12-13 09:49:21 +01:00
2020-12-13 11:20:46 +01:00
if [ -d " ${ repos_directory [i] } " ] ; then
2020-12-13 09:49:21 +01:00
# Take action if ${repos_directory[i]} exists ###
cd ${ repos_directory [i] } #jump into repo
else
# Repo doesnt exist
echo " Repo ${ repos_directory [i] } doesnt exist! "
rm -f mail_content.txt
cat >> mail_content.txt <<EOL
<head>
<style>
h1{
text-align: center;
font-size: 60;
color: red;
margin: 30px;
}
h2{
font-size: 30;
color: black;
}
div {
margin: 30px;
font-family: "Lucida Console" , Courier, monospace;
}
.overdue{
color: red;
}
footer{
margin-top: 9em;
}
</style>
</head>
<body>
<h1>AutoBackup Failure</h1>
<h2>Directory for the automatic backup for " ${ repos_name [i] } " not found</h2>
<div>
<p>Local time: $( date) </p>
<p>Failed backup repo: ${ repos_name [i] } </p>
</div>
</body>
<footer>
2022-02-18 11:38:02 +01:00
<p>Version: 0.2.2</p>
2021-12-26 17:54:40 +01:00
<p>Author: <a href = "mailto:localhorst@mosad.xyz" >localhorst@mosad.xyz</a></p>
2020-12-13 09:49:21 +01:00
</footer>
EOL
if [ " $DEBUG " = "false" ] ;
then
2022-02-18 11:38:02 +01:00
mailsend -f $senderEmail -t $receiverEmail -sub " AutoBackup Failure ${ repos_name [i] } " -smtp mail.emailserver.science -port 587 -starttls -auth -user autobackupwatchdog@mosad.xyz -pass "__PW_DB__" -cs "utf-8" -mime-type "text/html" -msg-body "mail_content.txt"
2020-12-13 09:49:21 +01:00
rm mail_content.txt
#/etc/init.d/alarm start
fi
fi
2020-04-24 23:07:40 +02:00
timestamp = $( date -r " $timestampFolder " +%s) #get timestap of last backup aka the "index" folder
time = $( date +%s) #get local time
diff = $(( $time - $timestamp )) #compute diff time
lastBackup = $( date -d @$timestamp ) #convert unix time stamp in human readable
tolerance = $(( ${ repos_interval [i] } * ${ repos_interval_tolerance [i] } / 100 )) #compute tolerance based on percentage
total_interval = $(( ${ repos_interval [i] } + $tolerance )) #compute total interval based on (interval+tolerance)
cd .. #leave repo
echo " Last backup: $lastBackup "
if [ " $diff " -gt " $total_interval " ] ; then #test if last backup is overdue
overdue = $(( $diff - ${ repos_interval [i] } )) #stores diff time since last backup in seconds
fn_convertSecondsToHumanReadable $overdue # convert in human readable
overdue = $fn_result #stores diff time since last backup in human readable
2020-04-24 23:17:34 +02:00
fn_convertSecondsToHumanReadable ${ repos_interval [i] } #convert planned interval for repo in human readable
plannedIntervall = $fn_result #stores the planned interval for repo in human readable
2020-04-24 23:07:40 +02:00
echo -e " \e[31mOverdue: $overdue "
2020-04-24 23:17:34 +02:00
echo -e "\e[39m " #reset console color to black
2020-04-24 23:07:40 +02:00
rm -f mail_content.txt
cat >> mail_content.txt <<EOL
<head>
<style>
h1{
text-align: center;
font-size: 60;
color: red;
margin: 30px;
}
h2{
font-size: 30;
color: black;
}
div {
margin: 30px;
font-family: "Lucida Console" , Courier, monospace;
}
.overdue{
color: red;
}
footer{
margin-top: 9em;
}
</style>
</head>
<body>
<h1>AutoBackup Failure</h1>
<h2>Overdue for the automatic backup for " ${ repos_name [i] } " </h2>
<div>
<p>Local time: $( date) </p>
<p>Failed backup repo: ${ repos_name [i] } </p>
<p>Planned backup interval: $plannedIntervall </p>
<p>Last backup: $lastBackup </p>
<p>Overdue: <a class = "overdue" >$overdue </a></p>
</div>
</body>
<footer>
2022-02-18 11:38:02 +01:00
<p>Version: 0.2.2</p>
2021-12-26 17:54:40 +01:00
<p>Author: <a href = "mailto:localhorst@mosad.xyz" >localhorst@mosad.xyz</a></p>
2020-04-24 23:07:40 +02:00
</footer>
EOL
if [ " $DEBUG " = "false" ] ;
then
2022-02-18 11:38:02 +01:00
mailsend -f $senderEmail -t $receiverEmail -sub " AutoBackup Failure ${ repos_name [i] } " -smtp mail.emailserver.science -port 587 -starttls -auth -user autobackupwatchdog@mosad.xyz -pass "__PW_DB__" -cs "utf-8" -mime-type "text/html" -msg-body "mail_content.txt"
2020-04-24 23:07:40 +02:00
rm mail_content.txt
2020-05-12 09:21:08 +02:00
#/etc/init.d/alarm start
2020-04-24 23:07:40 +02:00
fi
fi
done
echo " "
echo "Finished"
2021-12-26 17:54:40 +01:00