Avoid deleting log files for removed vhosts
[hcoop/scripts.git] / apache-sync-logs
index b0e007b..58c24c5 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-# invoke this as root on mire
+# invoked by cron as root on each web node
 
 exec 2>&1
 
@@ -11,30 +11,73 @@ unlog
 #VERBOSE=true
 VERBOSE=false
 
-LOCAL_LOG_DIR=/var/log/apache2
+LOCAL_LOG_DIR=/var/log/apache2/user
 KEYTAB_DIR=/etc/keytabs/user.daemon
 AFS_USER_DIR=/afs/hcoop.net/user
+ERROR=no
 
-for A in $(find $LOCAL_LOG_DIR/user -mindepth 3 -maxdepth 3 -print); do
+# Iterate through logs for each user
+for A in $(find $LOCAL_LOG_DIR -mindepth 3 -maxdepth 3 -print); do
     USER=`basename $A`
     PATHBITS=`echo $USER | head -c 1`/`echo $USER | head -c 2`/$USER
-    LOG_DEST=$AFS_USER_DIR/$PATHBITS/logs/apache/
+    USER_HOME=$AFS_USER_DIR/$PATHBITS
+    LOG_SRC=$A/apache/log/$(hostname)
+    LOG_DEST=$USER_HOME/.logs/apache/$(hostname)
 
-    if [ "$VERBOSE" = "true" ]; then
+    # Skip deleted or empty log directories
+    # Possible inefficiency? (ls entire directory*400+ dirs)
+    if test ! -d "$LOG_SRC" || ! ls "$LOG_SRC"/*/*.log >/dev/null 2>&1; then
+        if test "$VERBOSE" = "true"; then
+               echo "Skipping $USER (deleted or empty log dir $LOG_SRC)"
+        fi
+        continue
+    fi
+
+    # Skip people who have unreadable log subdirectories
+    # This test is broken! --clinton
+    if test -d "$USER_HOME/.logs" && \
+        ! ls "$USER_HOME/.logs" >/dev/null 2>&1; then
+        if test "$VERBOSE" = "true"; then
+               echo "Skipping $USER (unreadable log dir $USER_HOME/.logs)"
+        fi
+        continue
+    fi
+
+    # Skip people who do not have keytabs
+    if test ! -f "$KEYTAB_DIR/$USER"; then
+        if test "$VERBOSE" = "true"; then
+               echo "Skipping $USER (missing keytab $KEYTAB_DIR/$USER)"
+        fi
+        continue
+    fi
+
+    if test "$VERBOSE" = "true"; then
         echo
         echo "=============================================================================="
-        echo "syncing logs for $USER from $A"
+        echo "syncing logs for $USER from $LOG_SRC"
         echo "  to $LOG_DEST ..."
     fi
 
-    chown -R $USER:www-data $A
-    chmod -R ug+rw $A
-
-    if [ ! -d "$LOG_DEST" ]; then
-        echo "Error: $LOG_DEST does not exist, please make it"
+    if test ! -d "$USER_HOME/.logs/apache"; then
+        echo "Error: $USER_HOME/.logs/apache does not exist, please make it"
+#        ERROR=yes
+# We will assume that people know what they are doing when they
+# delete their ~/.logs/apache directory ....
+# This is perhaps a bad assumption --clinton
+        if test "$VERBOSE" = "true"; then
+               echo "Skipping $USER (no $LOG_DEST directory)"
+        fi
+        continue
     else
-        k5start -qtU -f $KEYTAB_DIR/$USER \
-            -- su $USER -c "rsync -a $A/apache/log/ $LOG_DEST/"
-        [ "$VERBOSE" = "true" ] && echo "  done."
+       # LOG_SRC/* is important: don't remove log files just because a vhost is gone
+       k5start -qtU -f $KEYTAB_DIR/$USER -- rsync -a --no-owner --no-group --delete $LOG_SRC/* $LOG_DEST/
+       test "$VERBOSE" = "true" && echo "  done."
     fi
 done
+
+if test "$ERROR" = "yes"; then
+    exit 1
+else
+    exit 0
+fi
+