Your IP : 216.73.216.21


Current Path : /pkg/zweb/1.0/bin/
Upload File :
Current File : //pkg/zweb/1.0/bin/zweb-log-rotate2

#!/bin/bash

info=1 # print actions taken
dryrun=1 # don't do anything, but print what would be done
debug=1 # 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=20971520 # 20MB
fi

if [ -f log_rotate_file_prefixes ]; then
    log_prefixes=$(head -1 log_rotate_file_prefixes)
else
    log_prefixes='access error php'
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)
log_files=$(find -maxdepth 5 -type f \( -name 'access.log' -o -name 'error.log' -o -name 'php.log' \) | sed "s/\.\///" | grep ^w)

for log_file in $log_files; do

    if [ $dryrun -eq 1 ] || [ $debug -eq 1 ]; then
	    echo "LOG: $log_file"
    fi

    path_to_log=$(dirname $log_file)
    web_user=$(echo $log_file | 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_prefix in $log_prefixes; do
        log="$log_prefix.log"
        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

https://nalibali.org/post-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/page-sitemap.xml 2026-11-18T09:24:11+00:00 https://nalibali.org/stories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/stories-sitemap2.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/audio_stories-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/story_cards-sitemap.xml 2025-06-06T10:27:48+00:00 https://nalibali.org/story_seeds-sitemap.xml 2025-05-19T07:35:43+00:00 https://nalibali.org/rhymes-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/activity_sheets-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/team_member-sitemap.xml 2025-05-28T09:40:43+00:00 https://nalibali.org/video_stories-sitemap.xml 2025-06-23T08:48:28+00:00 https://nalibali.org/guides-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/story_supplements-sitemap.xml 2025-09-16T12:22:20+00:00 https://nalibali.org/vacancies-sitemap.xml 2026-09-17T07:05:01+00:00 https://nalibali.org/category-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/post_tag-sitemap.xml 2026-11-06T00:00:00+00:00 https://nalibali.org/test-sitemap.xml 2025-06-13T09:36:28+00:00 https://nalibali.org/story_categories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/activity_sheets_categories-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables_categories-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks_categories-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/single_audio-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/rhyme_categories-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/guide_categories-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/author-sitemap.xml 2026-01-22T07:26:07+00:00