I'm running a weekly backup every Sunday and it's working fine. Additionally I'm running a new monthly backup every 1st of each month, meaning if the 1st of the month is a Sunday, I got double backups. My crontab file :
* 0 1 * * backup_monthly.sh
* 0 2-31 * 0 backup_weekly.sh
My expectations are that backup_monthly is running the first of each month no matter what, and backup_weekly, every Sunday except Sunday 1st
Problem is that backup_weekly is running daily in this scenario
I did some research didn't find any occurrence with a negative condition.
Thanks if you have some idea
0 0 * * * ([ "$(date +\%d)" = "01" ] || [ "$(date +\%a)" = "Sun" ]) && backup_monthly.sh
What does it mean?
0 0 * * *
means the cron job will run at 12:00 AM (midnight) every day.
([ "$(date +\%d)" = "01" ] || [ "$(date +\%a)" = "Sun" ])
consists of two parts.
[ "$(date +\%d)" = "01" ]
checks if the current day of the month is 01
.
[ "$(date +\%a)" = "Sun" ]
checks if the current day of the week is Sun
.
&& backup_monthly.sh
specifies the command that will be executed.
So, the cron job is scheduled to run every day at 12:00 AM, but the backup command will be executed only if it's either the
1st day
of the month orSunday
.