Notification upon successful backup bash script

General support questions
Post Reply
tsotrilos
Posts: 2
Joined: 2018/02/20 10:02:40

Notification upon successful backup bash script

Post by tsotrilos » 2018/02/20 11:38:12

Hello there.

I am running CentOS 7 on a server that hosts 3 VMs on it.

On a daily basis I have configured a bash backup script that transfers the .img files to another server that has
CentOS 7 installed also.

I found online a script that sends an email with mailx when one of the servers is running low on space and it
works like a charm. The script is the below:

THRESHOLD=70
PATHS=/var/lib/libvirt/
AWK=/bin/awk
DU=`/usr/bin/du -ks`
GREP=/bin/grep
SED=/bin/sed
CAT=/bin/cat
MAILFILE=/tmp/mailviews$$
MAILER=/bin/mail
## Change ADMIN Mail address as per the requirement ##
mailto= Targer Mail
for path in $PATHS
do
## Validate the Percentage of Disk space ##
DISK_AVAIL=`/bin/df -k / | grep -v Filesystem |awk '{print $5}' |sed 's/%//g'`
if [ $DISK_AVAIL -ge $THRESHOLD ]
then
echo "V1 Server Is Running Low On Disk Space Regarding Vms" > $MAILFILE
$CAT $MAILFILE | $MAILER -s "V1 Server Needs Your Attention" $mailto
fi
done
## END of the Script ##

I am trying to figure out how to send an email to my email upon the successful transfer of the VMs.
Is there any way to amend the above script in order to show the contents of folder?

For example, the above script checks if there is available space on the server by doing `/bin/df -k / | grep -v Filesystem |awk '{print $5}' |sed 's/%//g'` how can i make mailx to send the contents of the folder of the server that receives the .img files?

This is my first time here so please go easy on me.
In case that you require further information please ask me.

Thanks in advance

tunk
Posts: 334
Joined: 2017/02/22 15:08:17

Re: Notification upon successful backup bash script

Post by tunk » 2018/02/20 12:15:32

I have never used mailx before, but it has an "-a" option which attaches a file.
I guess you could write the info to this file and then attach it, e.g. something like:
/bin/ls -l /full/path/to/directory > /temp/mail-attachment.txt
$CAT $MAILFILE | $MAILER -s "V1 Server Needs Your Attention" -a /temp/mail-attachment.txt $mailto

tsotrilos
Posts: 2
Joined: 2018/02/20 10:02:40

Re: Notification upon successful backup bash script

Post by tsotrilos » 2018/02/20 15:23:18

and...yeap your suggestion seems that is working great. I amend the script as below:

PATHS=/var/lib/libvirt/backup/
AWK=/bin/awk
LS=`/usr/bin/ls -alh`
CAT=/bin/cat
MAILFILE=/tmp/Instructions.txt
MAILER=/bin/mail
## Change ADMIN Mail address as per the requirement ##
mailto= mail recipient address
for path in $PATHS
do
## Validate the Percentage of Disk space ##

/bin/ls -alh /var/lib/libvirt/backup/ > /tmp/mail-attachment.txt
$CAT $MAILFILE | $MAILER -s "Subject" -a /tmp/mail-attachment.txt $mailto

done
## END of the Script ##

So i manage to send the email normally with the required contents and a brief description on what to check (MAILFILE=/tmp/Instructions.txt)

Thanks a lot!

Post Reply