Hi to everybody! I'm new in this forum (
) and this is my first post.
I've written a small shell script to delete old backup file on a server with Linux CentOS; all backup files are named like that:
backup_YYYYMMDD.tar.gz
where YYYY= Year, MM= Month, DD=Day
The script outputs some info in a log file. If I execute manually the script from the shell everything works well, but when cron job executes it only some info are correctly logged into the log file and i can't understand why.
I've tried some search in this forum and I read that cron manages stdin and stdout in a different way, but still I couldn't find an answer. :-(
Here is the code:
Code:
#setting input vars
backup_dir=/var/backups/
backup_days=30
# setting internal vars
curdate_string=$(date +%Y%m%d)_$(date +%H%M)
log_file=$0_$curdate_string.log
actual_timestamp=$(date +%s)
backup_days_seconds=$(($backup_days*24*60*60))
#deleting old backups
echo "--------------------------------------------------------------------------------" >> $log_file
echo OLD BACKUP DELETE >> $log_file
for current_backupname in $(ls -l *.gz | awk '{ print $9 }' | grep '[0-9]\{8\}')
do
echo "-->" Processing file \"$backup_dir$current_backupname\" >> $log_file
current_backupdate=`echo $current_backupname | grep -o '[0-9]\{8\}'`
current_timestamp=`date -d$current_backupdate +%s`
if [ $(($actual_timestamp-$current_timestamp)) -gt "$backup_days_seconds" ]
then
rm -f $backup_dir$current_backupname
echo Backup file DELETED >> $log_file
else
echo Backup file keeped >> $log_file
fi
done
When cron executes the script only the first two "echo" are correctly redirected in the $log_file and i can't understand why. :-(
Thanks very much to everybody. :-)