[nextcloudpi] cron jobs: send email with results

ncp
nc13

#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,


#6

Thanks for all your replies, I just tried some of the solutions and I guess I am a little bit further then I was before.

@Schmu:
I added my emailaddress and crontab line to crontab -e and also tried crontab -u www-data -e and I can see some mail being generated but the mail is not send:
postqueue -p shows:

6847E1F44D      615 Wed Sep 19 11:59:01  root@raspberrypi.Home
                (connect to mx.domain.nl[SOME:IP]:25: Connection timed out)
                                         Email@domain.nl

@gas85: I have created this script based on your links

#!/bin/bash
EMAILFILE=/tmp/zipping.email
recipients="email@domain.nl"
#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 22" --delete --exclude 'nextcloud.log' "/media/drive/ncdata/" "pi@IP_SERVER:/media/data/Schrijven/NC/ncdata/"

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

touch $EMAILFILE

#Email Header
echo "To: email@domain.nl" > $EMAILFILE
echo "FROM: noreplay@nobody.net" >> $EMAILFILE
echo "Subject: test" >> $EMAILFILE
echo "MIME-Version: 1.0" >> $EMAILFILE
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"' >> $EMAILFILE
echo >> $EMAILFILE
echo "Content-Type: text/html" >> $EMAILFILE
echo "Content-Disposition: inline" >> $EMAILFILE
echo "" >> $EMAILFILE
[ -s file.name ] && echo "Other info: " >> $EMAILFILE
echo "" >> $EMAILFILE


#send email
cat $EMAILFILE | /usr/sbin/sendmail $recipients

#remove temporary files

rm $EMAILFILE
exit 0

But I get the same results as above. So I guess the scripts and crontab are working fine and something is wrong with the email configuration.


#7

To be honest E-Mail configuration is really pain for me… I try different setups, but did not get all 100% working. There are at least 2 different ways to configure it:

  1. You just send your E-Mail directly from your CLI as noreplay@nobody.net (that what you have here). Some domains (as google) will reject it and you need to read about “sendmail+DKIM+SPF+DMARC” to verify your sender. Some domains have trouble with TLS and it should be configured separate.
    I’m using this one and sending email to 3 different domains. Sometimes one or two of them are never appears.
  2. You send your E-Mail via relay (e.g. google) as YOU_GMAIL_USER@gmail.com. Idea is similar to Thunderbird, or Outlook. I did not get it working on google, but it works on other domains for me. In any way after few days I was blocked as spammer and have to contact administrators. After this everything was fine.

For troubleshooting try to run (as I remember) sendmail -v from CLI when you sending E-Mail to see verbose output.

I’m not CLI E-Mail expert, hope it helps you to get it working.


#8

Thanks for your reply Gas, but I am not able to get it work and I am running out of options.

Anyone else that could help me? I also noticed that fail2ban is not able to send emails.


#9

For now I have found a solution in NC14 by creating a notification in nextcloud so I know a script has run succesfully.
I have added below line in my bash script.
sudo -u www-data php /var/www/nextcloud/occ notification:generate admin “Backup” -l “Backup was successful”