1 |
dashley |
60 |
#!/bin/bash |
2 |
|
|
#------------------------------------------------------------------------------------------------- |
3 |
|
|
#$Header$ |
4 |
|
|
#------------------------------------------------------------------------------------------------- |
5 |
|
|
#This file is part of "Server Scripts, Automatic Maintenance, Cron", a set of scripts for |
6 |
|
|
#performing automatic periodic maintenance on a Linux server. |
7 |
|
|
#------------------------------------------------------------------------------------------------- |
8 |
|
|
#This source code and any program in which it is compiled/used is provided under the MIT License, |
9 |
|
|
#reproduced below. |
10 |
|
|
#------------------------------------------------------------------------------------------------- |
11 |
|
|
#Permission is hereby granted, free of charge, to any person obtaining a copy of |
12 |
|
|
#this software and associated documentation files(the "Software"), to deal in the |
13 |
|
|
#Software without restriction, including without limitation the rights to use, |
14 |
|
|
#copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
15 |
|
|
#Software, and to permit persons to whom the Software is furnished to do so, |
16 |
|
|
#subject to the following conditions : |
17 |
|
|
# |
18 |
|
|
#The above copyright notice and this permission notice shall be included in all |
19 |
|
|
#copies or substantial portions of the Software. |
20 |
|
|
# |
21 |
|
|
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
22 |
|
|
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
23 |
|
|
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
24 |
|
|
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
25 |
|
|
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
26 |
|
|
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
27 |
|
|
#SOFTWARE. |
28 |
|
|
#------------------------------------------------------------------------------------------------- |
29 |
|
|
#This is the cron script, invoked hourly, to perform automatic |
30 |
|
|
#maintenance. All stdout and stderr output is logged. |
31 |
|
|
# |
32 |
|
|
#The parameters that this script expects are: |
33 |
|
|
# |
34 |
|
|
# 1)Year (4-digit) |
35 |
|
|
# 2)Year (2-digit) |
36 |
|
|
# 3)Month (long text) |
37 |
|
|
# 4)Month (short text) |
38 |
|
|
# 5)Month (2-digit) |
39 |
|
|
# 6)Day of month (2-digit) |
40 |
|
|
# 7)Day of week (long text) |
41 |
|
|
# 8)Day of week (short text) |
42 |
|
|
# 9)Day of week (single digit) |
43 |
|
|
# 10)Hour (24-hour, 2-digit) |
44 |
|
|
# 11)Hour (12-hour, 2-digit) |
45 |
|
|
# 12)AM or PM, as a text string |
46 |
|
|
# 13)Minutes (2-digit) |
47 |
|
|
# 14)Seconds (2-digit) |
48 |
|
|
# |
49 |
|
|
#Change to a safe working directory in case something goes wrong, |
50 |
|
|
#especially a failed cd followed by a mass delete. |
51 |
|
|
cd /root/cronjob_sandbox |
52 |
|
|
# |
53 |
|
|
/etc/cron.custom/scripts/hline_thin |
54 |
|
|
echo "Automatic maintenance nominally beginning on $7, $1/$5/$6 at ${10}:${13}:${14}." |
55 |
|
|
/etc/cron.custom/scripts/hline_thin |
56 |
|
|
ACTUAL_TIMESTAMP=`date` |
57 |
|
|
echo -n "Automatic maintenance actually beginning at " |
58 |
|
|
echo -n ${ACTUAL_TIMESTAMP} |
59 |
|
|
echo "." |
60 |
|
|
/etc/cron.custom/scripts/hline_thin |
61 |
|
|
echo "This maintenance is scheduled hourly; and may include hourly, daily," |
62 |
|
|
echo "weekly, and monthly maintenance activity (depending on the hour of the" |
63 |
|
|
echo "day, the day of the week, and the day of the month)." |
64 |
|
|
/etc/cron.custom/scripts/hline_thin |
65 |
|
|
#----------------------------------------------------------------------------- |
66 |
|
|
#Establish Booleans to decide which types of maintenance will not and |
67 |
|
|
#will be performed this run. |
68 |
|
|
# |
69 |
|
|
BOOLEAN_HOURLY=1 |
70 |
|
|
echo "HOURLY maintenance WILL be performed." |
71 |
|
|
|
72 |
|
|
if [ ${10} = "01" ] ; then |
73 |
|
|
BOOLEAN_DAILY=1 |
74 |
|
|
echo "DAILY maintenance WILL be performed." |
75 |
|
|
else |
76 |
|
|
BOOLEAN_DAILY=0 |
77 |
|
|
echo "DAILY maintenance WILL NOT be performed." |
78 |
|
|
fi |
79 |
|
|
|
80 |
|
|
# |
81 |
|
|
#BOOLEAN_DAILY=1 |
82 |
|
|
#Above line for debug only. |
83 |
|
|
# |
84 |
|
|
#Note below: 6 is Saturday, 0 is Sunday. |
85 |
|
|
# |
86 |
|
|
if [ $BOOLEAN_DAILY = "1" ] && [ ${9} = "6" ] ; then |
87 |
|
|
BOOLEAN_WEEKLY=1 |
88 |
|
|
echo "WEEKLY maintenance WILL be performed." |
89 |
|
|
else |
90 |
|
|
BOOLEAN_WEEKLY=0 |
91 |
|
|
echo "WEEKLY maintenance WILL NOT be performed." |
92 |
|
|
fi |
93 |
|
|
|
94 |
|
|
if [ $BOOLEAN_DAILY = "1" ] && [ ${6} = "01" ] ; then |
95 |
|
|
BOOLEAN_MONTHLY=1 |
96 |
|
|
echo "MONTHLY maintenance WILL be performed." |
97 |
|
|
else |
98 |
|
|
BOOLEAN_MONTHLY=0 |
99 |
|
|
echo "MONTHLY maintenance WILL NOT be performed." |
100 |
|
|
fi |
101 |
|
|
|
102 |
|
|
#Debug |
103 |
|
|
#BOOLEAN_WEEKLY=1 |
104 |
|
|
|
105 |
|
|
/etc/cron.custom/scripts/hline_thin |
106 |
|
|
#----------------------------------------------------------------------------- |
107 |
|
|
#Loop through and do the hourly maintenance. This involves finding |
108 |
|
|
#all the scripts ending in "hourly" and executing them in alphabetical |
109 |
|
|
#order. |
110 |
|
|
# |
111 |
|
|
#Note on below: should investigate more reliable alternative using SED. |
112 |
|
|
if [ $BOOLEAN_HOURLY = "1" ] ; then |
113 |
|
|
HOURLY_FILES=`ls -1 /etc/cron.custom/cronjobs/*|sort|grep .hourly` |
114 |
|
|
for fname in $HOURLY_FILES |
115 |
|
|
do |
116 |
|
|
#Change to a safe working directory in case something goes wrong, |
117 |
|
|
#especially a failed cd followed by a mass delete. |
118 |
|
|
cd /root/cronjob_sandbox |
119 |
|
|
# |
120 |
|
|
echo "Executing" $fname "as part of hourly maintenance." |
121 |
|
|
/etc/cron.custom/scripts/hline_thin |
122 |
|
|
${fname} ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} |
123 |
|
|
/etc/cron.custom/scripts/hline_thin |
124 |
|
|
# |
125 |
|
|
#Change to a safe working directory in case something goes wrong, |
126 |
|
|
#especially a failed cd followed by a mass delete. |
127 |
|
|
cd /root/cronjob_sandbox |
128 |
|
|
done |
129 |
|
|
fi |
130 |
|
|
#----------------------------------------------------------------------------- |
131 |
|
|
#Prepare for the daily, weekly, and monthly maintenance. This generally |
132 |
|
|
#involves shutting daemons that might interfere with maintenance activities. |
133 |
|
|
#Find all the scripts ending in "premaint" and execute them in alphabetical |
134 |
|
|
#order. |
135 |
|
|
# |
136 |
|
|
#Note on below: should investigate more reliable alternative using SED. |
137 |
|
|
if [ $BOOLEAN_DAILY = "1" ] || [ $BOOLEAN_WEEKLY = "1" ] || [ $BOOLEAN_MONTHLY = "1" ] ; then |
138 |
|
|
PREMAINT_FILES=`ls -1 /etc/cron.custom/cronjobs/*|sort|grep .premaint` |
139 |
|
|
for fname in $PREMAINT_FILES |
140 |
|
|
do |
141 |
|
|
#Change to a safe working directory in case something goes wrong, |
142 |
|
|
#especially a failed cd followed by a mass delete. |
143 |
|
|
cd /root/cronjob_sandbox |
144 |
|
|
# |
145 |
|
|
echo "Executing" $fname "to prepare for daily, weekly, and monthly maintenance." |
146 |
|
|
/etc/cron.custom/scripts/hline_thin |
147 |
|
|
${fname} ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} |
148 |
|
|
/etc/cron.custom/scripts/hline_thin |
149 |
|
|
# |
150 |
|
|
#Change to a safe working directory in case something goes wrong, |
151 |
|
|
#especially a failed cd followed by a mass delete. |
152 |
|
|
cd /root/cronjob_sandbox |
153 |
|
|
done |
154 |
|
|
fi |
155 |
|
|
#----------------------------------------------------------------------------- |
156 |
|
|
#Do the daily maintenance. |
157 |
|
|
if [ $BOOLEAN_DAILY = "1" ] ; then |
158 |
|
|
DAILY_FILES=`ls -1 /etc/cron.custom/cronjobs/*|sort|grep .daily` |
159 |
|
|
for fname in $DAILY_FILES |
160 |
|
|
do |
161 |
|
|
#Change to a safe working directory in case something goes wrong, |
162 |
|
|
#especially a failed cd followed by a mass delete. |
163 |
|
|
cd /root/cronjob_sandbox |
164 |
|
|
# |
165 |
|
|
echo "Executing daily maintenance script" ${fname}. |
166 |
|
|
/etc/cron.custom/scripts/hline_thin |
167 |
|
|
${fname} ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} |
168 |
|
|
/etc/cron.custom/scripts/hline_thin |
169 |
|
|
# |
170 |
|
|
#Change to a safe working directory in case something goes wrong, |
171 |
|
|
#especially a failed cd followed by a mass delete. |
172 |
|
|
cd /root/cronjob_sandbox |
173 |
|
|
done |
174 |
|
|
fi |
175 |
|
|
#----------------------------------------------------------------------------- |
176 |
|
|
#Do the weekly maintenance. |
177 |
|
|
if [ $BOOLEAN_WEEKLY = "1" ] ; then |
178 |
|
|
WEEKLY_FILES=`ls -1 /etc/cron.custom/cronjobs/*|sort|grep .weekly` |
179 |
|
|
for fname in $WEEKLY_FILES |
180 |
|
|
do |
181 |
|
|
#Change to a safe working directory in case something goes wrong, |
182 |
|
|
#especially a failed cd followed by a mass delete. |
183 |
|
|
cd /root/cronjob_sandbox |
184 |
|
|
# |
185 |
|
|
echo "Executing weekly maintenance script" ${fname}. |
186 |
|
|
/etc/cron.custom/scripts/hline_thin |
187 |
|
|
${fname} ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} |
188 |
|
|
/etc/cron.custom/scripts/hline_thin |
189 |
|
|
# |
190 |
|
|
#Change to a safe working directory in case something goes wrong, |
191 |
|
|
#especially a failed cd followed by a mass delete. |
192 |
|
|
cd /root/cronjob_sandbox |
193 |
|
|
done |
194 |
|
|
fi |
195 |
|
|
#----------------------------------------------------------------------------- |
196 |
|
|
#Do the monthly maintenance. |
197 |
|
|
if [ $BOOLEAN_MONTHLY = "1" ] ; then |
198 |
|
|
MONTHLY_FILES=`ls -1 /etc/cron.custom/cronjobs/*|sort|grep .monthly` |
199 |
|
|
for fname in $MONTHLY_FILES |
200 |
|
|
do |
201 |
|
|
#Change to a safe working directory in case something goes wrong, |
202 |
|
|
#especially a failed cd followed by a mass delete. |
203 |
|
|
cd /root/cronjob_sandbox |
204 |
|
|
# |
205 |
|
|
echo "Executing monthly maintenance script" ${fname}. |
206 |
|
|
/etc/cron.custom/scripts/hline_thin |
207 |
|
|
${fname} ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} |
208 |
|
|
/etc/cron.custom/scripts/hline_thin |
209 |
|
|
# |
210 |
|
|
#Change to a safe working directory in case something goes wrong, |
211 |
|
|
#especially a failed cd followed by a mass delete. |
212 |
|
|
cd /root/cronjob_sandbox |
213 |
|
|
done |
214 |
|
|
fi |
215 |
|
|
#----------------------------------------------------------------------------- |
216 |
|
|
#Recover from the daily, weekly, and monthly maintenance. This generally |
217 |
|
|
#involves restarting daemons. Find all the scripts ending in "postmaint" |
218 |
|
|
#and execute them in alphabetical order. |
219 |
|
|
# |
220 |
|
|
#Note on below: should investigate more reliable alternative using SED. |
221 |
|
|
if [ $BOOLEAN_DAILY = "1" ] || [ $BOOLEAN_WEEKLY = "1" ] || [ $BOOLEAN_MONTHLY = "1" ] ; then |
222 |
|
|
POSTMAINT_FILES=`ls -1 /etc/cron.custom/cronjobs/*|sort|grep .postmaint` |
223 |
|
|
for fname in $POSTMAINT_FILES |
224 |
|
|
do |
225 |
|
|
#Change to a safe working directory in case something goes wrong, |
226 |
|
|
#especially a failed cd followed by a mass delete. |
227 |
|
|
cd /root/cronjob_sandbox |
228 |
|
|
# |
229 |
|
|
echo "Executing" $fname "to recover from daily, weekly, and monthly maintenance." |
230 |
|
|
/etc/cron.custom/scripts/hline_thin |
231 |
|
|
${fname} ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} ${10} ${11} ${12} ${13} ${14} |
232 |
|
|
/etc/cron.custom/scripts/hline_thin |
233 |
|
|
# |
234 |
|
|
#Change to a safe working directory in case something goes wrong, |
235 |
|
|
#especially a failed cd followed by a mass delete. |
236 |
|
|
cd /root/cronjob_sandbox |
237 |
|
|
done |
238 |
|
|
fi |
239 |
|
|
#----------------------------------------------------------------------------- |
240 |
|
|
#Change to a safe working directory in case something goes wrong, |
241 |
|
|
#especially a failed cd followed by a mass delete. |
242 |
|
|
cd /root/cronjob_sandbox |
243 |
|
|
# |
244 |
|
|
ACTUAL_TIMESTAMP=`date` |
245 |
|
|
echo -n "Automatic maintenance actually ending at " |
246 |
|
|
echo -n ${ACTUAL_TIMESTAMP} |
247 |
|
|
echo "." |
248 |
|
|
/etc/cron.custom/scripts/hline_thin |
249 |
|
|
#hline_thick no longer necessary because not concatening logs together, as |
250 |
|
|
#these scripts were changed to be more SSD-friendly. |
251 |
|
|
#/etc/cron.custom/scripts/hline_thick |
252 |
|
|
# |
253 |
|
|
#End of script. |