Search code examples
linuxbashcronrsyncmsmtp

What causes multiple Mails when using Cron with Bash Script


I've made a little bash script to backup my nextcloud files including my database from my ubuntu 18.04 server. I want the backup to be executed every day. When the job is done I want to reseive one mail if the job was done (additional if it was sucessful or not). With the current script I reseive almost 20 mails and I can't figure out why. Any ideas?

My cronjob looks like this:

* 17 * * * "/root/backup/"backup.sh >/dev/null 2>&1

My bash script

#!/usr/bin/env bash

LOG="/user/backup/backup.log"

exec > >(tee -i ${LOG})

exec 2>&1

cd /var/www/nextcloud

sudo -u www-data php occ maintenance:mode --on

mysqldump --single-transaction -h localhost -u db_user --password='PASSWORD' nextcloud_db > /BACKUP/DB/NextcloudDB_`date +"%Y%m%d"`.sql

cd /BACKUP/DB
ls -t | tail -n +4 | xargs -r rm --


rsync -avx --delete /var/www/nextcloud/ /BACKUP/nextcloud_install/
rsync -avx --delete --exclude 'backup' /var/nextcloud_data/ /BACKUP/nextcloud_data/

cd /var/www/nextcloud
sudo -u www-data php occ maintenance:mode --off

echo "###### Finished backup on $(date) ######"


mail -s "BACKUP" [email protected] < ${LOG}

Solution

  • Are you sure about the CRON string? For me this means "At every minute past hour 17".

    Should be more like 0 17 * * *, right?