-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrotation-webservers-daily.sh
executable file
·192 lines (179 loc) · 5 KB
/
logrotation-webservers-daily.sh
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env bash
#############
# Functions #
#############
function main {
VARS=`curl -s http://169.254.169.254/latest/user-data`
IFS=', ' read -r -a array <<< $VARS
ENV_BRANCH=${array[1]}
SERVICE_TYPE="webservers"
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION_SRC=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep -oP '\"region\"[[:space:]]*:[[:space:]]* \"\K[^\"]+')
case $ENV_BRANCH in
DEV)
ENV_LOGROTATE="dev"
;;
STAGE)
ENV_LOGROTATE="stage"
;;
PROD)
ENV_LOGROTATE="prod"
;;
*)
ENV_LOGROTATE="dev"
;;
esac
cat <<EOF > /etc/cron.daily/logrotate
#!/bin/sh
/usr/bin/find /var/log/ -type f -name "*.gz" -mtime +7 -exec rm -fv {} \;
/usr/sbin/logrotate /etc/logrotate.conf
EOF
mv /etc/cron.daily/logrotate /etc/cron.hourly/
service cron restart
logrotate_syslog
logrotate_nginx_php_fpm
}
function logrotate_nginx_php_fpm {
if [[ -f /etc/logrotate.d/php-fpm-7.0 ]]; then rm -f /etc/logrotate.d/php-fpm-7.0; fi
if [[ -f /etc/logrotate.d/nginx ]]; then rm -f /etc/logrotate.d/nginx; fi
if [[ -f /etc/logrotate.d/nginx.rpmnew ]]; then rm -f /etc/logrotate.d/nginx.rpmnew; fi
if ! [[ -f /etc/logrotate.d/nginx-php-fpm-s3 ]]; then
touch /etc/logrotate.d/nginx-php-fpm-s3
cat <<EOF > /etc/logrotate.d/nginx-phpfpm-s3
/var/log/php-fpm/7.0/*log {
daily
size 10M
missingok
notifempty
rotate 4
maxage 4
sharedscripts
compress
copytruncate
dateext
dateformat -%Y%m%d-%H:%M:%S
postrotate
/bin/kill -SIGUSR1 `cat /var/run/php-fpm/php-fpm-7.0.pid 2>/dev/null` 2>/dev/null || true
endscript
lastaction
INSTANCE_ID=`echo ${INSTANCE_ID}`
HOSTNAME=`hostname`
BUCKET=`echo ${ENV_LOGROTATE}`-logs
SERVICE_TYPE=`echo ${SERVICE_TYPE}`
REGION_SRC=`echo ${REGION_SRC}`
REGION_DEST=us-east-1
read DAY MONTH YEAR <<< `date "+%d %m %Y"`
FORMAT=`date "+%Y%m%d-%H:%M:%S"`
aws s3 sync /var/log/php-fpm/7.0/ "s3://\$BUCKET/\$REGION_SRC/\$SERVICE_TYPE/\${HOSTNAME}_\${INSTANCE_ID}/php-fpm/\$YEAR/\$MONTH/\$DAY/" --region \$REGION_DEST --exclude "*" --include "*.log-\$FORMAT*"
endscript
}
/var/log/nginx/*.log {
daily
size 10M
missingok
notifempty
rotate 4
maxage 4
sharedscripts
compress
copytruncate
dateext
dateformat -%Y%m%d-%H:%M:%S
postrotate
/etc/init.d/nginx reopen_logs
endscript
lastaction
INSTANCE_ID=`echo ${INSTANCE_ID}`
HOSTNAME=`hostname`
BUCKET=`echo ${ENV_LOGROTATE}`-logs
SERVICE_TYPE=`echo ${SERVICE_TYPE}`
REGION_SRC=`echo ${REGION_SRC}`
REGION_DEST=us-east-1
read DAY MONTH YEAR <<< `date "+%d %m %Y"`
FORMAT=`date "+%Y%m%d-%H:%M:%S"`
aws s3 sync /var/log/nginx/ "s3://\$BUCKET/\$REGION_SRC/\$SERVICE_TYPE/\${HOSTNAME}_\${INSTANCE_ID}/nginx/\$YEAR/\$MONTH/\$DAY/" --region \$REGION_DEST --exclude "*" --include "*.log-\$FORMAT*"
endscript
}
EOF
fi
}
function logrotate_syslog {
if [[ -f /etc/debian_version ]]; then
if [[ `cat /etc/logrotate.d/rsyslog | grep "/var/log/syslog" | wc -l` == 1 ]]; then
sed -i 's/syslog/syslog-old/g' /etc/logrotate.d/rsyslog
fi
if ! [[ -f /etc/logrotate.d/syslog-s3 ]]; then
touch /etc/logrotate.d/syslog-s3
cat <<EOF > /etc/logrotate.d/syslog-s3
/var/log/syslog {
daily
size 10M
missingok
notifempty
rotate 4
maxage 4
sharedscripts
compress
copytruncate
dateext
dateformat -%Y%m%d-%H:%M:%S
postrotate
reload rsyslog >/dev/null 2>&1 || true
endscript
lastaction
INSTANCE_ID=`echo ${INSTANCE_ID}`
HOSTNAME=`hostname`
BUCKET=`echo ${ENV_LOGROTATE}`-logs
SERVICE_TYPE=`echo ${SERVICE_TYPE}`
REGION_SRC=`echo ${REGION_SRC}`
REGION_DEST=us-east-1
read DAY MONTH YEAR <<< `date "+%d %m %Y"`
FORMAT=`date "+%Y%m%d-%H:%M:%S"`
aws s3 sync /var/log/ "s3://\$BUCKET/\$REGION_SRC/\$SERVICE_TYPE/\$INSTANCE_ID/syslog\$YEAR/\$MONTH/\$DAY/" --region \$REGION_DEST --exclude "*" --include "syslog-\$FORMAT*"
endscript
}
}
EOF
fi
elif ! [[ -f /etc/debian_version ]]; then
if [[ `cat /etc/logrotate.d/syslog | grep "/var/log/messages" | wc -l` == 1 ]] ; then
sed -i '/messages/d' /etc/logrotate.d/syslog
fi
if ! [[ -f /etc/logrotate.d/syslog-s3 ]]; then
touch /etc/logrotate.d/syslog-s3
cat <<EOF > /etc/logrotate.d/syslog-s3
/var/log/messages {
daily
size 10M
missingok
notifempty
rotate 4
maxage 4
sharedscripts
compress
copytruncate
dateext
dateformat -%Y%m%d-%H:%M:%S
postrotate
/bin/kill -HUP 'cat /var/run/syslogd.pid 2> /dev/null' 2> /dev/null || true
endscript
lastaction
INSTANCE_ID=`echo ${INSTANCE_ID}`
HOSTNAME=`hostname`
BUCKET=`echo ${ENV_LOGROTATE}`-logs
SERVICE_TYPE=`echo ${SERVICE_TYPE}`
REGION_SRC=`echo ${REGION_SRC}`
REGION_DEST=us-east-1
read DAY MONTH YEAR <<< `date "+%d %m %Y"`
FORMAT=`date "+%Y%m%d-%H:%M:%S"`
aws s3 sync /var/log/ "s3://\$BUCKET/\$REGION_SRC/\$SERVICE_TYPE/\$INSTANCE_ID/messages\$YEAR/\$MONTH/\$DAY/" --region \$REGION_DEST --exclude "*" --include "messages-\$FORMAT*"
endscript
}
EOF
fi
fi
}
#########
# Main #
########
main