Why do we need these and what do they do?
#!/bin/sh
specifies that the cron “script” runs with /bin/sh
(it’s a normal shell script). This is a shebang.
test -x /usr/sbin/automysqlbackup || exit 0
checks whether /usr/sbin/automysqlbackup
exists and is executable; if it isn’t, it exits the script silently. This line is useful to avoid an error if automysqlbackup
is removed but its cron job isn’t — which happens if the package is removed, not purged. Technically, it works as follows: test -x /usr/sbin/automysqlbackup
exits with code 0 if the file is executable, 1 otherwise. ||
causes the shell to look at the exit code of the previous command; if it’s 0, the rest of the line is ignored, otherwise it’s executed; exit 0
causes the shell to exit with exit code 0 (no error).
/usr/sbin/automysqlbackup
runs automysqlbackup
.
Can I delete that automysqlbackup
file and set everything up through sudo crontab -u username -e
for my user?
Yes, you can; the package is set up correctly so that your removal will be remembered, and the file won’t be restored on package upgrades. Note that you don’t need sudo
here, crontab -e
as the user works just as well. You will however need to ensure that the user has all the necessary permissions for the backup (in MySQL and in whatever storage the backup goes to).
I would suggest considering instead whether there is any particular reason the default daily cron job isn’t appropriate for you; if there isn’t, keep it instead of setting up your own.
exit
command. I triedman exit
, but nothing was found. Doesn't the||
stand for logical OR here? Isn't#!/bin/sh
just a comment here? – JConstantine Aug 30 '21 at 08:57cron.daily
job will run once a day, using whatever configuration you’ve defined forautomysqlbackup
. It should work just fine for your purposes. – Stephen Kitt Aug 30 '21 at 09:0825 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
which is placed inside the/etc/crontab
file and actually runs theautomysqlbackup
utility daily at 6:25 AM. What isanacron
? – JConstantine Aug 30 '21 at 09:11anacron
(seeman anacron
) is a utility which ensures jobs are run at certain intervals, even if the system is powered down. See also Is it true that “cron.daily runs anacron everyhour”?, How does anacron determine daily, weekly and monthly job to run?, How does anacron work if it's not a daemon? and related posts. – Stephen Kitt Aug 30 '21 at 09:15man anacron
returnsNo manual entry for anacron
for me. Alsofind / -iname 'anacron'
returns nothing. – JConstantine Aug 30 '21 at 09:20anacron
installed (which is fine). The crontab entry tests to see whetheranacron
is installed, and if it isn’t, runs thecron.daily
entries usingrun-parts
. – Stephen Kitt Aug 30 '21 at 09:21cron.daily
forautomysqlbackup
I just have to modify the/etc/crontab
file and change25 6 * * *
to whatever values I need, right? And I want to set the daily/monthly backups as well I'll have to create/etc/cron.weekly/automysqlbackup
,/etc/cron.monthly/automysqlbackup
files and modify the corresponding lines in/etc/crontab
. – JConstantine Aug 30 '21 at 09:26cron.daily
jobs run at a different time, then yes. – Stephen Kitt Aug 30 '21 at 09:30automysqlbackup
takes care of them when run daily. – Stephen Kitt Aug 30 '21 at 09:31/etc/defaults/automysqlbackup
. – Stephen Kitt Aug 30 '21 at 09:34find /home/username/private_files/backups/* -mtime +30 -exec rm {} \;
– JConstantine Aug 30 '21 at 09:35automysqlbackup
’s features. Again, look in/etc/defaults/automysqlbackup
— you can use aPOSTBACKUP
script. – Stephen Kitt Aug 30 '21 at 09:37automysqlbackup
also rotates backups for you, so you might not need to delete anything. It keeps all monthly backups, the last five weekly backups, and the last week’s daily backups. – Stephen Kitt Aug 30 '21 at 09:58DOWEEKLY
where I can pick the day of the week, but I can't see a way to pick the day of the month to do monthly backups. Also I don't see how can I set the exact time without using cron jobs. – JConstantine Aug 30 '21 at 10:13DOMONTHLY
as well. And yes, you can’t change the time without changing the cron job; is 6:25 not appropriate? – Stephen Kitt Aug 30 '21 at 10:22DOMONTHLY
.6:25
is not appropriate because I'm currently working on a temporary machine that is not always powered on and at6:25
in the morning it is actually powered off. That's why I need to change the time. – JConstantine Aug 30 '21 at 10:28anacron
would be appropriate (instead of trying to find a time when it is guaranteed to be on). – Stephen Kitt Aug 30 '21 at 10:50automysqlbackup
in 20.04 doesn’t supportDOMONTHLY
. Monthly backups always happen on the first day of the month (and will be missed if the system isn’t running on that day). – Stephen Kitt Aug 30 '21 at 11:44cron
itself is running at the exact time the job specifies.anacron
makes up for that by keeping track of when jobs last ran, managing jobs per “time unit” (daily, weekly, monthly) instead of tracking specific times, and running them ifcron
hasn’t run them in the last day/week/month. – Stephen Kitt Aug 30 '21 at 12:06