0

I created the following cron job under /etc/cron.daily/backup-vivashost

#!/bin/bash

DATE=`date +%Y%m%d`

mkdir /var/lib/html-backup/www-$DATE/
cp -R /var/www/html/* /var/lib/backups/html-backup/www-$DATE/
find /var/lib/bakcups/html-backup/* -type f -mtime +5 -delete

One day after i created this there is no copy of /var/www/html/ to /var/lib/backups/html-backup/

Is it because the shell is not runing under sudo su root permissions or ?

Can you please tell me a reason why it is not working ?

Thanks!

  • It should be cron.daily, not daily.cron. – Jos Nov 6 '15 at 14:54
  • It's typo. The correct one is like you mentioned. – Venelin Vasilev Nov 6 '15 at 14:58
  • Did the www-$DATE directory get created? – Jos Nov 6 '15 at 15:03
  • And you're sure, that the modification time is changed with your cp command? Run the job again without the deletion and check it. – A.B. Nov 6 '15 at 15:08
  • 1
    To preserve the timestamps use cp -pR – A.B. Nov 6 '15 at 15:14
0

First, correct the path names in the script file /etc/cron.daily/backupvivashost and apply A.B's suggestions:

#!/bin/bash

DATE=$(date +%Y%m%d)
mkdir /var/lib/backups/html-backup/www-$DATE/
cp -pR /var/www/html/* /var/lib/backups/html-backup/www-$DATE/ 
find /var/lib/backups/html-backup -mtime +5 -delete

Then, make the file executable:

sudo chmod +x /etc/cron.daily/backupvivashost

Then execute the file to see if it works:

sudo /etc/cron.daily/backupvivashost

If it works, see if it will also run when called by crontab. The script needs sudo privileges so it should be called by the root crontab.

sudo crontab -e

will do that. If that doesn't work, there is something wrong with the default editor, or with the file /var/spool/cron/crontabs/root. To specify nano as your editor, do

export EDITOR=/usr/bin/nano

When you manage to open the crontab with the editor, enter dates and times to have your script executed. If you want to run it at 17:00, enter:

0 17 * * * /etc/cron.daily/backupvivashost > /home/user/logfile 2>&1

This way, you will get to see what goes wrong in the file /home/user/logfile. When that works as well, remove the line from the crontab, as the script will now be run daily anyway.

  • When i do sudo crontab -e i get output on the next row 602 and that's all. I have to close the terminal and open it again to start typing any kind of commands. It is so weird... – Venelin Vasilev Nov 6 '15 at 16:33
  • Move the file /var/spool/cron/crontabs/root out of the way, or delete it if you are sure it doesn't contain any useful information. Then try sudo crontab -e again. – Jos Nov 6 '15 at 16:39
  • It contains the following information: pastebin.com/JmYywPRU . It is important to be there. Can i add another line so i can make this cron job executable in a specific hour every day ? – Venelin Vasilev Nov 6 '15 at 16:49
  • No, you can't simply edit that file. What crontab does is: it lets you edit the file, then after you save it, it processes its contents. There is no other way to do the latter part. Anyway as it is a valid crontab file, the error must be that your default editor is not called. – Jos Nov 6 '15 at 16:52
  • About the not-functioning of your crontab -e command and the "602" line that results, may I ask you to post a separate question on this site? When that is solved, this answer here should be more or less complete. I have a couple of things you could try. – Jos Nov 6 '15 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.