I've some scripts that run perfect if you run them from a terminal, but once you add them to crontab it's like the bash doesn't execute properly and doesn't continue with the first IF statement and the script looks to stop.
#!/bin/bash
## File: opt_files_backup.sh
## Author: Raul Sanchez
## Last update: 09/12/2015
## Description:
## Includes the configuration file for the FTP connection
## Available variables (FTP): $ftp_user, $ftp_pass, $ftp_server, $ftp_source_directory, $ftp_target_directory
## Available variables (mail): $mail_recipients
## Available variables (Logging): $log_files
source config.sh
## Creates a tar.gz backup with the format opt_files_YYYYmmdd.tar.gz
if tar czfP /opt/backup-scripts/src/files/opt_files-`date +%Y%m%d%H%M`.tar.gz /opt --exclude='/opt/backup-scripts';
then
echo -e "`date` - Files Backup OK" >> $log_files
## Remove files older than 7 days
if find /opt/backup-scripts/src/files/*.tar.gz -mtime +7 -exec rm {} \;
then
##------ Connect to remote server and synchronize ------##
## ftp::ssl-allow is false by default on /root/.lftprc
if lftp -e "mirror --reverse --delete --verbose $ftp_source_directory $ftp_target_directory; exit" -u $ftp_user,$ftp_pass $ftp_server;
then
echo -e "`date` - Files Backup Mirror Transfer OK" >> $log_files
##------ Send email notification to Sys Admin ------##
echo "The task completed successfully at `date`" | mail -e -s "CRONTAB task #2 OPT Files Mirror" $mail_recipients
else
echo -e "`date` - Files Backup Mirror Transfer KO" >> $log_files
exit 1
fi
fi
else
echo -e "`date` - Files Backup Mirror Backup KO" >> $log_files
##------ Send email notification failure to Sys Admin ------##
echo "The task didn't completed at `date`, something went wrong. Please check $log_files for more information." | mail -e -s "CRONTAB task #2 OPT Files Mirror Failed!" $mail_recipients
exit 1
fi
As I've said, this script works perfectly if you run it from a terminal, but it doesn't with crontab. The cronjob for this script is:
0 7,10,13,16 * * 1-5 /opt/backup-scripts/scripts/opt_files_backup.sh
I've changed the SHELL in /etc/crontab from SHELL=/bin/sh to /bin/bash and still doesn't work.
Could anyone help me to find what's going wrong with my script and crontab?
Regards.