| Current Path : /pkg/zweb/1.0/bin/ |
| Current File : //pkg/zweb/1.0/bin/zweb-log-rotate2~ |
#!/bin/bash
info=1 # print actions taken
dryrun=0 # don't do anything, but print what would be done
debug=0 # print debugging detail
export PATH=/cmd:$PATH
cd /etc/zweb
if [ -f web_root ]; then
web_root=$(head -1 web_root)
else
web_root='/srv/web'
fi
if [ -f log_rotate_max_bytes ]; then
max_bytes=$(head -1 log_rotate_max_bytes)
else
max_bytes=10485760 # 10MB
fi
if [ -f log_rotate_max_archives ]; then
max_archives=$(head -1 log_rotate_max_archives)
else
max_archives=5
fi
if [ -f log_rotate_pause ]; then
pause=$(head -1 log_rotate_pause)
else
pause=60
fi
cd $web_root
access_logs=$(find -maxdepth 5 -name 'access.log' | sed "s/\.\///" | grep ^w)
for access_log in $access_logs; do
if [ $dryrun -eq 1 ] || [ $debug -eq 1 ]; then
echo "LOG: $access_log"
fi
path_to_log=$(dirname $access_log)
web_user=$(echo $access_log | cut -d '/' -f1)
message="Log file rotation report:\n\nYou may wish to download archives before they are automatically deleted."
cd "$web_root/$path_to_log"
for log in access.log access-ssl.log error.log error-ssl.log; do
if [ -f $log ]; then
bytes=$(stat -c%s $log)
if [ $debug -eq 1 ] || [ $dryrun -eq 1 ]; then
megabytes=$(echo $bytes/1024/1024 | bc)
echo -e "$path_to_log/$log\n size = $bytes bytes ($megabytes MB)"
fi
if [ $bytes -ge $max_bytes ]; then
time_stamp=$(date +%Y%m%d-%H:%M)
if [ $dryrun -eq 1 ]; then
echo " mv $log $log-$time_stamp"
else
[[ $debug -eq 1 ]] && echo " mv $log $log-$time_stamp"
mv $log $log-$time_stamp
fi
[[ $debug -eq 1 ]] && echo " new name = $log-$time_stamp"
id $web_user &> /dev/null
if [ $? -eq 0 ]; then
if [ -f $log-$time_stamp ]; then
chown $web_user:$web_user $log-$time_stamp
fi
fi
if [ $dryrun -eq 1 ]; then
echo " notify nginx"
else
[[ $debug -eq 1 ]] && echo " notify nginx"
/cmd/svc -1 /svc/nginx
fi
sleep 1
gzip_retval=99
if [ $dryrun -eq 1 ]; then
echo " gzip $log-$time_stamp"
else
[[ $debug -eq 1 ]] && echo " archive = gzip $log-$time_stamp.gz"
gzip -c $log-$time_stamp > $log-$time_stamp.gz
gzip_retval=$?
fi
if [ $gzip_retval -eq 0 ] && [ $info -eq 1 ]; then
message="$message\n\nlog $web_root/$path_to_log/$log"
fi
if [ $gzip_retval -eq 0 ] || [ $dryrun -eq 1 ]; then
archives=$(ls | grep -E "$log-[1-9][0-9]{7}-[0-9]{2}:[0-9]{2}.gz$" | sort -n -r)
if [ $? -eq 0 ]; then
if [ -f $log-$time_stamp.gz ]; then
message="$message\n\ncreate $log-$time_stamp.gz"
[[ -f $log-$time_stamp ]] && rm $log-$time_stamp
fi
archive_count=$(echo $archives | wc -l)
if [ $debug -eq 1 ] || [ $dryrun -eq 1 ]; then
echo " archives = $archive_count"
fi
if [ $archive_count > $max_archives ]; then
count=0
for archive in $archives; do
chown $web_user:$web_user "$archive"
count=$((count+1))
if [ $count -gt $max_archives ]; then
if [ $dryrun -eq 1 ] || [ $info -eq 1 ] || [ $debug -eq 1 ]; then
message="$message\ndelete $archive"
fi
if [ $dryrun -eq 0 ]; then
rm $archive
fi
else
if [ $debug -eq 1 ] || [ $dryrun -eq 1 ] || [ $info -eq 1 ]; then
message="$message\nkeep $archive"
fi
fi
done
fi
fi
fi
[[ $debug -eq 1 ]] && echo " sleep $pause"
sleep $pause
fi
fi
done
echo -e "To: $webmasters\nFrom: help@zootzone.com\nSubject: INFO: Log rotation [$web_user@$web_host]\n\n$message"
done