Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin.
If I ssh in and go cd domainname.com and then sh _mybash.sh from the command line. The script runs fine. If I set up a cron to /home/username/domainname.com/_mybash.sh the script runs and attempts to backup everything under username... I don't understand.
It's probably not finding the paths of certain things.
What user do you run/cron it as?
Put set -x in the top of it to debug. Did you try placing the cd command at the top of the script.
Don't expect the escape sequences to work for color since you are not at standard input and standard output are not terminas
ls when you cron.
Use full paths to exeecutables.
Here is the latest, and stripped down version. I removed all the tput and formatting stuff since cron doesn't bother with it. A few questions, what permissions would make the most sense from a security standpoint... 755, or 700? How can I make this more secure? How could I organize the archive where there was a www and database folder that the files were placed in before archival? Since this was originally made to be run manually in command prompt, a lot of the output for cron now is sort of irrelevant... are there relevant things I could add to provide more useful info in my cron email?
On many systems a find -exec primary must be terminated by a semicolon or by a plus sign. The semicolon in your script is eaten by the shell so find won't see it. Using + instead of \; is more efficient in cases when you have more than one file to remove.
Thanks for pointing that out i thought i had changed that. How could i go about catching errors to be output in the email. Also, is it not necessary to have the .sh extension on my bash/cron script? Here is the updated script without all the output.
Do I need to be wrapping each step in some kind of if else to do error checking? Something like the following?
if [ "$?" = "0" ]; then
#some command
else
echo "Error. Couldn't do some command!" 1>&2
exit 1
fi
---------- Post updated at 01:08 PM ---------- Previous update was at 07:48 AM ----------
---------- Post updated at 02:03 PM ---------- Previous update was at 01:08 PM ----------
Sorry for the additional post but this is a work in progress and keeps changing. This is the latest... trying to do a bit of checking and handling here... really dont know what I'm doing though. Could use some advice.