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
.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