views:

115

answers:

2

this is a small addition to the previous script, and this time I would like to log details for the backup.

script /tmp/commit-push-log

# add all files to the repository
for REPOSITORY in $@ 
do

    cd $REPOSITORY

    # commit the changes
    hg commit -A -m "Commit changes `date`"

    # push the changes to the remote repository
    if hg push 
    then
        logger hg push completed without failure
    else
        logger hg push fails
    fi

done

exit

cat /tmp/commit-push-log | logger

rm /tmp/commit-push-log

the problem is that i don't see any mercurial messages in the log. What can go wrong in my script?

+1  A: 

my current version

for REPOSITORY in $@ 
do

    # new temp file
    OUTPUT_LOG=`tempfile`
    echo -n > $OUTPUT_LOG

    # checks whether $REPO is a repo
    if [ ! -d $REPOSITORY/.hg ]; then
      echo "Not a repository: $REPOSITORY"
      exit 1;
    fi

    # change to that dir
    cd "$REPOSITORY"

    logger "Repository: $REPOSITORY"

    # commit the changes
    hg commit -A -m "Commit changes `date`" 2>&1 >> $OUTPUT_LOG

    # push the changes to the remote repository
    if hg push 2>&1 >> $OUTPUT_LOG
    then
 logger hg push completed without failure
    else
 logger hg push fails
 exit 1;
    fi

    # log the contents and delete the tempfile
    cat $OUTPUT_LOG | logger

    rm -f $OUTLOG_LOG

done

exit 0
Jeffrey04
You didn't quote $OUTPUT_LOG properly and 10th line $REPOSITORY/.hg is not quoted. Try `export TMPDIR="/tmp/space test"; mkdir "$TMPDIR` ; ./Your_script` # to see what I mean. +1 for `tempfile`, didn't know that one.
Reef
+1  A: 
  1. You should not use static tmp filenames. Use mktemp, it's far safer.
  2. You should cd "$REPOSITORY" instead of "cd $REPOSITORY" or things will get funny when REPOSITORY will contain any spaces or special characters.
  3. You should not write automated commit comments. See here for the great article on this topic.
  4. hg probably outputs errors to stderr. Use hg commit -A -m "$comment" 2>&1 and hg push 2>&1
Reef
the script mainly backups /var/log and /etc for monitoring purpose from external servers, so automated commit comments should be enough for the purpose
Jeffrey04