2

I have similar questions to confusion about cron and anacron (setting up backup schedule for rsnapshot), but there weren't answered either.

Problem

My problem is to have a backup routine on daily basis on a laptop. So, my first idea was to set it in a crontab like

$ crontab -e
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:   
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# I guess as user I have no access to /var/log from this crontab, do I?
@daily ~/my-backups.sh >> ~/.log/my-backup.log

and then the script my-backups.sh should run everyday at midnight (0 0 * * *), right?

Ok, and after reading the documentation I found out about anacron. But then the problem arises as the script is not run. From the documentation I understand that if the computer was off when the daily routine was supposed to be executed by cron then anacron should fire it up on start up. But that is not happening either.

Do I need to do another setup? (From the documentation I assumed it was not needed and that it should work out of the box, but is not happening.)

Question

How can I set up cron/anacron to execute a daily script, and in case the computer was off at the time of execution, it should run the script on startup or after some time after startup.

Version using cron.daily

I modified my backup script changing to cron.daily as suggested. My test script is:

#!/bin/bash

date +%Y-%m-%d >> /var/log/backup.log

Then add it to /etc/cron.daily as a symbolic link

sudo ln -s ~/bin/my-backup /etc/cron.daily/backup

I checked, and the script and the symlink both have execute permissions.

Then, today, I started my machine around 9, and the first entries on /var/log/syslog are around that time, but there is no sign of cron.daily there.

$ cat /var/log/syslog | grep cron
Feb 16 09:05:15 box cron[981]: (CRON) INFO (pidfile fd = 3)
Feb 16 09:05:15 box cron[981]: (CRON) INFO (Running @reboot jobs)
Feb 16 09:17:01 box CRON[4574]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Feb 16 10:17:01 box CRON[6453]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Feb 16 11:17:01 box CRON[6878]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Feb 16 12:17:01 box CRON[7310]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Feb 16 13:14:01 box CRON[7582]: (root) CMD (   test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond)
Feb 16 13:17:01 box CRON[7595]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Feb 16 14:17:02 box CRON[8134]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

Also, the last time of the anacron executed was yesterday

$ sudo cat /var/spool/anacron/cron.daily 
20170215

And I check, and my script is listed in the cron.daily

$ run-parts --test --report /etc/cron.daily
/etc/cron.daily/0anacron
/etc/cron.daily/apport
/etc/cron.daily/apt-compat
/etc/cron.daily/backup
/etc/cron.daily/bsdmainutils
/etc/cron.daily/cracklib-runtime
/etc/cron.daily/dpkg
/etc/cron.daily/google-chrome
/etc/cron.daily/google-talkplugin
/etc/cron.daily/logrotate
/etc/cron.daily/man-db
/etc/cron.daily/mlocate
/etc/cron.daily/passwd
/etc/cron.daily/popularity-contest
/etc/cron.daily/update-notifier-common
/etc/cron.daily/upstart
adn
  • 261
  • I setup cron to create a .tar backup file of my laptop every morning and email it to myself in the cloud: https://askubuntu.com/questions/917562/backup-linux-configuration-scripts-and-documents-to-gmail/922493#922493 – WinEunuuchs2Unix Jan 10 '18 at 00:31

1 Answers1

1

Place you script to /etc/cron.daily and you will get exactly what you wanted.

Pilot6
  • 90,100
  • 91
  • 213
  • 324
  • Will it run as root there? Or as my user? – adn Feb 15 '17 at 11:37
  • It will run as root. – Pilot6 Feb 15 '17 at 11:38
  • I move the script to the /etc/cron.daily, and run it manually and worked. However, today after startup (after the daily hour of cron) the anacron job was not executed. I checked, and the script has no extension, but there is not info about anacron. – adn Feb 16 '17 at 16:23
  • Can you post the script to your question? You can't use path like ~/ there. You need to add full path like /home/xxxxx/..... – Pilot6 Feb 16 '17 at 16:26
  • The script right now is a test one, just to see if it is executed. See my changes. – adn Feb 16 '17 at 16:36
  • Did you ever end up resolving this? – Joost Mar 19 '18 at 17:39