[nextcloudpi] cron jobs: send email with results


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:


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

#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"
    touch /lockdir/myscript.lock

/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?


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


When you define your cronjob with
crontab -e
paste the following into the very first line and change the mail address accordingly:

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


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


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


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.