I setup a script on my dedicated server to backup all of my Cpanel backup files to Amazon S3, and I’ve got it running every night via cron.
It ran perfectly last night and backed everything up, but then proceeded to delete all it had backed up. It appears to have something to do with the date command because if I pull the “deletion” portion of the script out and put it into another file and run it as an echo, I can’t get the date to echo properly. I keep getting a “command not found” error:
Here’s the full code for the backup script:
#!/bin/bash
##Notification email address
_EMAIL=klawncare1212@gmail.com
ERRORLOG=/var/log/s3_backup_logs/backup.err`date +%F`
ACTIVITYLOG=/var/log/s3_backup_logs/activity.log`date +%F`
##Directory which needs to be backed up
SOURCE=/backup/cpbackup/daily
##Name of the backup in bucket
DESTINATION=`date +%F`
##Backup degree
DEGREE=4
#Clear the logs if the script is executed second time
:> ${ERRORLOG}
:> ${ACTIVITYLOG}
##Uploading the daily backup to Amazon s3
/usr/bin/s3cmd -r put ${SOURCE} s3://MK_Web_Server_Backup/${DESTINATION}/ 1>>${ACTIVITYLOG} 2>>${ERRORLOG}
ret2=$?
##Sent email alert
msg="BACKUP NOTIFICATION ALERT FROM `hostname`"
if [ $ret2 -eq 0 ];then
msg1="Amazon s3 DAILY Backup Uploaded Successfully"
else
msg1="Amazon s3 DAILY Backup Failed!!\n Check ${ERRORLOG} for more details"
fi
echo -e "$msg1"|mail -s "$msg" ${_EMAIL}
#######################
##Deleting backup's older than DEGREE days
## Delete from both server and amazon
#######################
DELETENAME=$(date --date="${DEGREE} days ago" +%F)
/usr/bin/s3cmd -r --force del s3://MK_Web_Server_Backup/${DELETENAME} 1>>${ACTIVITYLOG} 2>>${ERRORLOG}
And here is the sample code that I am testing to try and simply echo the date code above:
#!/bin/bash
##Backup degree
DEGREE=4
#######################
##Deleting backup's older than DEGREE days
## Delete from both server and amazon
#######################
DELETENAME=$(date --date="4 days ago" +%F)
echo ${DELETENAME}
What am I doing wrong? Every time I run this small test script on my CentOS linux server through SSH I get the following error:
“date –date=4 days ago: command not found”
So, it’s not having any trouble inserting the “degree” variable value. And, if I simply take and run the same command at the prompt in SSH (date --date="4 days ago" +%F), it works like a charm, outputting the data just as I would expect it to.
What am I doing wrong?
You probably are picking up different versions of the date command when you run the script from a regular terminal, vs. running it from the script, because they use different paths. Either use the full path to the version of the date command you want to use, or explicitly set the path at the beginning of the script.