[nextcloudpi] cron jobs: send email with results


#1

Hi guys, I have created a backup script that will copy the content of ncdata to another server every 10 minutes and will not start when the preceeding run is not completed:

#!/bin/bash

#Create the Locking dir if it doesn't exist
if [[ ! -d "/lockdir/" ]]; then
    mkdir -p /lockdir/
fi

#Check if there is currently a lock in place, if so then exit, if not then create a lock
if [ -f "/lockdir/myscript.lock" ]; then
    echo "Backupscript is already running"
    exit
else
    touch /lockdir/myscript.lock
fi

/usr/bin/rsync -aAx  -e "ssh -p 5222" --delete --exclude 'nextcloud.log' "/media/drive/ncdata/" "pi@IP_SERVER:/media/data/ncdata/"

#release the lock
rm /lockdir/myscript.lock
echo "Backup completed"

Cron job

*/10 * * * * root /usr/local/bin/backup.sh

I can see the results in /var/mail/root but would like to receive an email.
Now I was wondering whats the best way to do this, would it be possible to use the email credentials I have saved in config.php for passwords resets etc?


#2

Bump, maybe someone can point me in the right direction?


#3

When you define your cronjob with
crontab -e
paste the following into the very first line and change the mail address accordingly:
MAILTO="youraddy@yourprovider.com"

Your system (unix) needs to be configured to allow sending mails.


#4

If you want to create your own E-mail, check this out:
E-mails even with attachments from Cacti --> https://github.com/GAS85/my_backup_scipts/blob/master/backupzipper.sh
E-mails without attachments --> https://github.com/GAS85/my_backup_scipts/blob/master/systembackup.sh


#5

Rather than spinning your own locking mechanism, you might be able to use run-one (which has several really cool variations):

http://manpages.ubuntu.com/manpages/trusty/man1/run-one.1.html

It’s a more robust way of making sure you only run one instance - no timing issues to worry about. :0) Obviously it works on Ubuntu, but quite possibly other distro’s too.

Respectfully,