-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathrotate-www-logs
More file actions
executable file
·77 lines (60 loc) · 2.79 KB
/
Copy pathrotate-www-logs
File metadata and controls
executable file
·77 lines (60 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# shellcheck source=scripts/lib/common.sh
source "$(dirname -- "$( readlink -f -- "$0"; )")/lib/common.sh"
usage(){
PrintHelp \
"[command]$(basename "${BASH_SOURCE[0]}")[/] [path][parameter]<NEW_LOG_FILENAME>[/]" \
"$(cat << EOF
Move Apache access log files into a by-month subdirectory in the format [path]<LOGS_ROOT>/apache/<YYYY-MM>[/], then gzip them.
Must be run as [user]root[/], and is generally run by the Apache [command]rotatelogs[/] subprocess as such.
EOF
)" \
"$(cat << EOF
[path][parameter]<NEW_LOG_FILENAME>[/]
The filename of the new Apache access log file created by [command]rotatelogs[/].
EOF
)"
}
if [ $# -eq 0 ] || { [ $# -eq 1 ] && { [ "$1" = "--help" ] || [ "$1" = "-h" ]; }; }; then
usage
fi
# Apache has a habit of starting this script twice, which can stomp on its own files.
for pid in $(pidof -x rotate-www-logs); do
if [ "${pid}" != $$ ]; then
# We `echo()` and `exit()` instead of `ExitWithError()` because Apache prints stderr to the log, but not stdout. We don't need this logged.
echo "rotate-www-logs is already running with PID ${pid}"
exit 1
fi
done
# Prevent the loop from entering if no matches are found for the pattern.
shopt -s nullglob
filenameBase=$(basename "$1" | sed --regexp-extended "s/\.[0-9]+$//")
directory=$(dirname "$1")
for filename in "${directory}/${filenameBase}".*; do
# When Apache calls this script, it passes the filename of the new log file it created.
# Thus, we check here to make sure we don't process and then delete the brand-new log file!
if [ "${filename}" != "$1" ]; then
# Apache log files can have data for more than one day. Pull out entries for different days into different files.
dates=$(grep --extended-regexp --only-matching "\[[0-9]{1,2}\/[a-zA-Z]{3}\/20[0-9]{2}" "${filename}" | sort --unique)
while read -r line; do
logRawDate=$(echo "${line}" | sed "s/\[//g" | sed "s/\// /g")
logDate=$(date --date="${logRawDate}" "+%Y-%m-%d")
logMonth=$(date --date="${logRawDate}" "+%Y-%m")
grepString=${line//\[/}
logFilename="www-access-${logDate}.log"
mkdir --parents "${directory}/${logMonth}"
# Is the log file already existing and gzipped?
if [ -f "${directory}/${logMonth}/${logFilename}.gz" ]; then
gunzip "${directory}/${logMonth}/${logFilename}.gz"
fi
# `ipv6loganon` is provided by the `ipv6calc` package.
grep --extended-regexp "\[${grepString}" "${filename}" | ipv6loganon --anonymize-paranoid >> "${directory}/${logMonth}/${logFilename}"
gzip --force --best "${directory}/${logMonth}/${logFilename}"
chown --preserve-root --recursive www-data:adm "${directory}/${logMonth}"
chmod --preserve-root --recursive g+w "${directory}/${logMonth}"
done <<< "${dates}"
rm "${filename}"
fi
done
# Set this for `fail2ban` to use.
ln --force --symbolic "$1" "${directory}"/current.log