0

I am trying to schedule some backup scripts in cron on a CentOS release 6.7 (Final).

This is the full backup, basically mounts a remote directory, checks if there is enough space available for doing the backup (it does it if the disk is at 85% or less), finally sends a mail for report and dismounts the directory

The script is located in /root/myjobs/ and its named fullbackup.sh The permissions are 777 (-rwxrwxrwx) for now

#!/bin/bash
mount -t cifs //192.168.0.202/Volume_1 /mnt/nas -o username=Backup,password=PassBackup
ocupado="$(df /mnt/nas|tail -n 1|awk '{print $4}'|cut -d "%" -f 1)"
if [ $ocupado -gt 85 ]; then
    echo "DISK IS FULL" | mail -s "SERVER" [email protected]
else
    # This is anoying but at least works, I need to create a file with the name of the backup, delete it, and then make the backup with tar
    touch /mnt/nas/Backup/full_backup_`date +"%d%b%y"`.tar.gz
    rm -f /mnt/nas/Backup/full_backup_`date +"%d%b%y"`.tar.gz
    tar -cvzpf /mnt/nas/Backup/full_backup_`date +"%d%b%y"`.tar.gz --exclude=/home/general /home
    echo "FULL BACKUP OK" | mail -s "SERVER" [email protected]
fi
umount /mnt/nas/

If I run the script manually it works fine, but if I schedule the script with cron it only sends the mail,but does not make the copy, like it does not execute the tar command.

If I execute crontab -e I see the nano editor, with this path in the top:

/tmp/crontab.jdwF9c

I think something is wrong with the path, that's why I am configuring cron editing the /etc/crontab file directly.

This is my /etc/crontab file:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

0 9 1 * * root /root/myjobs/fullbackup.sh

I have translated this question of Spanish as best I could, I hope the problem is well understood.

Thanks for your help!

EDIT 1: I interact with the server remotely. The cron runs the script and works correctly when I'm connected, but not if I close the ssh connection and I wait for the command to run at 9:00 the next day. In that case only sends the mail.

9
  • 2
    crontab -e creates a temporary copy of your crontab for you to edit which it then installs for you. The path /tmp/crontab.jdwF9c is not an issue. Jul 12, 2016 at 10:05
  • ok dword, thanks for the appreciation , then I'll ignore that detail. What could try to diagnose the fault ?
    – yb_esc
    Jul 12, 2016 at 10:10
  • 1
    Do you have SELinux enabled? If you don't know, check with getenforce. Just asking because I've been permanently traumatized by the ways of SELinux can jump in front of you making things harder. Extra security is good, but the extra hassle it can cause can be painful. Jul 12, 2016 at 12:04
  • Hi Janne, SElinux is disabled,thanks for the idea anyway
    – yb_esc
    Jul 13, 2016 at 6:47
  • It only sends the email ... Try using full paths into fullbackup.sh script.
    – Ra_
    Jul 14, 2016 at 7:55

1 Answer 1

0

Try to call tar with the full path. On my system e.g. it's /bin/tar. You can find out with which tar.

2
  • Ok, as I tried to explain the issue, the problem is when I'm not connected or have not recently been connected, so I programmed the script with full path. I will comment or will edit the test result. Thanks for the idea and the trick to know the path of tar.
    – yb_esc
    Jul 14, 2016 at 11:00
  • The line with the full path runs ok manually but fails if is runned as script, ¿quotes problem maybe? --> /bin/tar -cvzpf /mnt/nas/Backup/copia_diferencial_date +"%d%b%y".tar.gz --exclude=/home/general /home -N /mnt/nas/Backup/copia_total_01date +"%b%y".tar.gz`
    – yb_esc
    Jul 15, 2016 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .