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?
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.
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.
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:
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:
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.
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.
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”
Just by case I finally found simple working solution how to setup SMTP relay (everything above looks like enterprise solutions in compare). Described here:
root=username@gmail.com #you can create accounts for different users (like www-data)
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=username
AuthPass=password #this could be Application password also
FromLineOverride=YES
UseTLS=YES