optimizing backup cronjob execution
View article
View summary
Previously I always started the cronjobs to times, when the other job should have finished. I didn't knew excactly how the jobs are executed and therefore this was the easiest.
# /etc/cron.d/backup-jobs
#min hour dom mon dow user command
5 1 * * * root /usr/local/sbin/backup_functions.sh CRON DAILY
7 13 * * 1 root /usr/local/sbin/backup_functions.sh CRON WEEKLY
42 21 1 * * root /usr/local/sbin/backup_functions.sh CRON MONTHLY
Now I researched, to optimize the backup jobs running.
I'm using Ubuntu 20.04-LTS, package cron/focal 3.0pl1-136ubuntu1.# /etc/cron.d/backup-jobs
#min hour dom mon dow user command
@daily root /usr/local/sbin/backup_functions.sh CRON DAILY
@weekly root /usr/local/sbin/backup_functions.sh CRON WEEKLY
@monthly root /usr/local/sbin/backup_functions.sh CRON MONTHLY
The cronjobs start time is set by shortcuts @daily, @weekly, @monthly to 00:00. Where @weekly sets dow=0, to start the job on sunday and @monthly sets dom=1, to start the job on the 1st. The jobs are started with very little delay and running simultaneously. So every sunday and every 1st there are two scripts and when the 1st is on sunday there are three script running concurently.
For now,
I choose to use flock and waiting for the previous script to finish. This will not be a problem until the next 1st on a sunday, which will be 1st of May in 2022. So I don't have to worry for the next five weeks.
To finish this
I have two options, with different advantages and disadvantages.
1. Order @daily, @weekly, @monthly and waiting for the previous to finish.
* Could break on 1st is a sunday, because there would be two scripts waiting for the first to finish. Who knows, which would be the second to run.
* In the end all three scripts would be run.
2. Order @monthly, @weekly, @daily and break, if another script is running.
* Every day only one script would run.
* The script must execute the other tasks, if needed.
I currently believe option 2. to be the robuster handling and therefore prefer this.