0

I'm on Ubuntu Server 20.04 LTS and what I'm trying to do is to set the cron job for automysqlbackup utility. When I installed automysqlbackup I noticed that the cron.daily/automysqlbackup was created inside the etc folder. In the automysqlbackup file there are the following lines:

#!/bin/sh
test -x /usr/sbin/automysqlbackup || exit 0
/usr/sbin/automysqlbackup

Why do we need these and what do they do?

Can I delete that automysqlbackup file and set everything up through sudo crontab -u username -e for my user?

1 Answers1

2

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.

Stephen Kitt
  • 434,908
  • What I want to achieve is to make backups everyday at certain time and delete all the backups older than 30 days. Also I wonder why I couldn't find any information on the exit command. I tried man 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:57
  • 1
    See the update. The existing cron.daily job will run once a day, using whatever configuration you’ve defined for automysqlbackup. It should work just fine for your purposes. – Stephen Kitt Aug 30 '21 at 09:08
  • And the last thing. I don't understand the following: 25 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 the automysqlbackup utility daily at 6:25 AM. What is anacron? – JConstantine Aug 30 '21 at 09:11
  • 1
    anacron (see man 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:15
  • man anacron returns No manual entry for anacron for me. Also find / -iname 'anacron' returns nothing. – JConstantine Aug 30 '21 at 09:20
  • 1
    Ah right, you don’t have anacron installed (which is fine). The crontab entry tests to see whether anacron is installed, and if it isn’t, runs the cron.daily entries using run-parts. – Stephen Kitt Aug 30 '21 at 09:21
  • So, if I want to use the script from the cron.daily for automysqlbackup I just have to modify the /etc/crontab file and change 25 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:26
  • 1
    If you want to have all your cron.daily jobs run at a different time, then yes. – Stephen Kitt Aug 30 '21 at 09:30
  • 1
    I don’t think you need anything else for weekly and monthly backups, automysqlbackup takes care of them when run daily. – Stephen Kitt Aug 30 '21 at 09:31
  • 1
    As explained in the package description, “automysqlbackup creates backup every day, week and month for all of your MySQL database, to a configured folder. There's nothing to do but to install this package, and you'll rest assured that you have a way to go back in the history of your database.” It really is an “install-and-forget” package: you don’t need to configure anything. – Stephen Kitt Aug 30 '21 at 09:32
  • 1
    If you do want to change the defaults, look in /etc/defaults/automysqlbackup. – Stephen Kitt Aug 30 '21 at 09:34
  • The thing is that I want it to run at certain time and then to delete the old backups. So I'll have to set another cron job like: find /home/username/private_files/backups/* -mtime +30 -exec rm {} \; – JConstantine Aug 30 '21 at 09:35
  • 1
    No, you don’t need another cron job for that, you can use automysqlbackup’s features. Again, look in /etc/defaults/automysqlbackup — you can use a POSTBACKUP script. – Stephen Kitt Aug 30 '21 at 09:37
  • I wonder why would they recommend running cron jobs for automysqlbackup out there on many different resources like this or this for example if automysqlbackup is a self-contained utility that can do all those things without using any cron jobs at all. – JConstantine Aug 30 '21 at 09:48
  • 1
    I can’t read those articles’ authors minds ;-). The second one is from 2012, perhaps the package changed since then. The first one discusses the AutoMySQLBackup tool itself, not the package — it’s the Debian (and Ubuntu) package which sets up the cron jobs for you. – Stephen Kitt Aug 30 '21 at 09:57
  • 1
    Incidentally, automysqlbackup 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:58
  • There's DOWEEKLY 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:13
  • 1
    There’s DOMONTHLY 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:22
  • Accroding to the documentation there's no DOMONTHLY. 6:25 is not appropriate because I'm currently working on a temporary machine that is not always powered on and at 6:25 in the morning it is actually powered off. That's why I need to change the time. – JConstantine Aug 30 '21 at 10:28
  • 1
    The man page is unfortunately incomplete. If your system isn’t always on, anacron would be appropriate (instead of trying to find a time when it is guaranteed to be on). – Stephen Kitt Aug 30 '21 at 10:50
  • 1
    Apologies, the version of automysqlbackup in 20.04 doesn’t support DOMONTHLY. 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:44
  • So, the manually created cron jobs won't work as well if the system is off and the only way to deal with that is to use anacron, right? Thank you for all the information you've provided. You helped a lot! And sorry for such an extended discussion in the comments. – JConstantine Aug 30 '21 at 11:57
  • 1
    Yes, cron jobs only run if cron 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 if cron hasn’t run them in the last day/week/month. – Stephen Kitt Aug 30 '21 at 12:06