Script help
Hello, and Happy New Year, to all.
I've created a bash script to back up certain critical directories using rsync. It works fine, for the most part, but I have encountered a couple of problems: 1) I want the script to test for the presence of the backup destination directories. If they exist already, then send emails to that effect and exit. However, I'm not sure how to test for the separate directories before exiting (hope that's clear). Here's my code snippet: # Create backup directories. If they already exist then exit. # if [ -d $DESTDIR/$DATE ] ; then echo "$DESTDIR/$DATE already exists, so backup must already be done" \ |mailx -s "Backup Summary `date +"%m/%d/%y %r"`" $EMAIL exit elif [ ! -d $DESTDIR/$DATE ] ; then mkdir $DESTDIR/$DATE fi if [ -d $DESTDIR2/$DATE ] ; then echo "$DESTDIR2/$DATE already exists, so backup must already be done" \ |mailx -s "Backup Summary `date +"%m/%d/%y %r"`" $EMAIL exit elif [ ! -d $DESTDIR2/$DATE ] ; then mkdir $DESTDIR2/$DATE fi As you can see, if the first argument is true, the second is never tested because we've already exited. How can I make the script test for both, and send (an) email(s) before exiting? 2) I want directories older than 14 days to be deleted. The script works if I run it manually, but not if run from cron. Here's the code snippet: if [ -d $DESTDIR ]; then find $DESTDIR -maxdepth 1 -mtime +$DAYSOLD -exec rm -Rf {} \; fi if [ -d $DESTDIR2 ]; then find $DESTDIR2 -maxdepth 1 -mtime +$DAYSOLD -exec rm -Rf {} \; fi I've seen posts to the effect that the full path must be used, rather than a variable, if the script is to work from cron. But, does anyone know of any way I can make this work using variables? Many thanks for your help! Diggy |
Hi,
do not run your test in 2 statements, create one statement using logical AND (||) some like this: if [ -f $DESTINATION1 ] || [ -f DESTINATION2 ]; then echo BACKUP_EXISTS; exit 1; else echo NO_BACKUP_DO_SOMETHING; fi This will prevent you from exiting during your first test before second test is done. There is also a logical OR (&&) Hope i could help. |
Thanks so much! That makes sense. But, as I think more about it, I realize that my logic is flawed. If one backup destination directory exists, but the other doesn't, then my script should ignore any further commands having to do with existing directory, but continue with commands for the non-existing one. Can I build upon what you've suggested to accomplish that?
Diggy |
Quote:
Code:
if [ sometest ]; then |
Thanks for your reply, openSauce.
If I remove the exit command from my if block, what happens if the directories are already created (thus indicating that the script/backup was already run)? I wouldn't want the backup to be done again. Hence the test. As for initializing my variables, at the top of my delete directory script: DESTDIR=/backup-external #where to back up to - destination DESTDIR2=/public/nightly_backup #where to back up to - destination DAYSOLD="13" #delete backup directories more than x+1 days old Again, if I run the script manually, works a treat. But from cron, no joy. Your continued help is very much appreciated. Diggy |
Sorry, forgot to mention, your backup should be in the else block. So the general logic is
Code:
if [ backup already done ]; then ================================================= When you run from cron, are you calling exactly the same script which works if you run it manually, or are you just running Code:
if [ -d $DESTDIR2 ]; then If you are running the same script (as you should be), try redirecting std out and std err to a file and see what error messages you get. It could be a permissions issue, or perhaps the script is not in cron's $PATH? |
PS: mesiol mixed up and and or. And is &&, or is ||.
|
I wish I knew how to create a function, and to put the script in cron's path (the latter might be the solution to my problem. I'll have to RTFM :-) ). As a starter, and not to be lazy, might you share how to do these with me?
Many thanks. Diggy |
Quote:
Code:
if [ -f $DESTINATION1 ] || [ -f DESTINATION2 ]; Also, the colons are not needed. They are redundant. You only need then when you want to concatenate two sentences on a single line. So, I'd either do: Code:
if [ -f $DESTINATION1 ] || [ -f DESTINATION2 ] Code:
if [ -f $DESTINATION1 ] || [ -f DESTINATION2 ]; then I also suggest declaring functions, for both the backup and the exit+send mail functionalities. |
Quote:
For now, the relevant line in crontab should be something like this. Code:
* * * * * /home/[your_user_name]/delete_dir_script.sh >/home/[your_user_name]/delete_dir_log 2>&1 cheers OS |
I guess in need i need to do this:
If exists a and b, then email and exit. If exists a but not b, then backup b, email, and exit. If does not exist a but exists b, then backup a, email, and exit. If does not exist a or b, then backup a and b, email, and exit. Just not sure how to code that. Diggy |
Quote:
Code:
function foo() { |
i92guboj,
Ah, suberb. But, does the function keep either backup (or both backups) from happening if they're already complete (and by complete, my script simply checks for the existence of the destination directories)? Diggy PS - Thanks, openSauce. I'll send script output to a file, as you suggest, and report results (can't run script until tonight, though). |
Quote:
The checks are still done out of the function, on the if-then-else blocks. |
Sorry, I should have included the entire script for clarity. Here's the remainder:
# Backup source directories to destination directories for d in $LIST; do rsync -az $SRCDIR/$d/ $DESTDIR/$DATE/$d rsync -az $SRCDIR/$d/ $DESTDIR2/$DATE/$d \ |mail -s "Backup Summary `date +"%m/%d/%y %r"`" $EMAIL done # Email results of backup if [ $? -eq 0 ]; then mail -s "Backup: SUCCESS" $EMAIL << EOF Your backup from $SRCDIR to $DESTDIR completed successfully. EOF else mail -s "Backup: FAILURE" $EMAIL << EOF Your backup from $SRCDIR to $DESTDIR did not complete. EOF fi exit Sorry to be so dense, but will the function keep the above from running if the destination directory or directories already exist. It doesn't appears so to me. Diggy |
All times are GMT -5. The time now is 09:13 PM. |