forked from littlebizzy/slickstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-cron-hourly.txt
277 lines (221 loc) · 13.6 KB
/
05-cron-hourly.txt
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash
####################################################################################################
#### author: SlickStack ############################################################################
#### link: https://slickstack.io ###################################################################
#### mirror: https://mirrors.slickstack.io/crons/05-cron-hourly.txt ################################
#### path: /var/www/crons/05-cron-hourly ###########################################################
#### destination: n/a (not a boilerplate) ##########################################################
#### purpose: SlickStack core cron job *hourly* (05/13) will run every 1 hour ######################
#### module version: Ubuntu 20.04 LTS ##############################################################
#### sourced by: root crontab ######################################################################
#### bash aliases: ss cron 05, ss cron hourly ######################################################
####################################################################################################
## NEVER MODIFY THE ROOT CRONTAB OR SS CORE CRON JOBS OR YOUR STACK WILL STOP WORKING ##
## INSTEAD ADJUST INTERVAL_SS SETTINGS IN SS-CONFIG OR EDIT CUSTOM CRON FILES ##
####################################################################################################
#### TABLE OF CONTENTS (02-Cron-Regular) ###########################################################
####################################################################################################
## TEMPLATE: cron jobs
## A. Validate (Restore) SS-Config
## B. Validate (Restore) SS-Functions
## C. Validate (Restore) SS-Check + SS-Worker
## D. Source SS-Config + SS-Functions (After Validated)
## E. Run Custom Tasks (EDIT SOURCED FILES)
## F. Touch Timestamp File
## G. Run Scheduled Tasks (DO NOT EDIT)
## H. Reset Permissions (Cron Jobs + Bash Scripts)
## I. Delete Lock File
####################################################################################################
#### A. 05-Cron-Hourly: Validate (Restore) SS-Config ###############################################
####################################################################################################
## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS
## SNIPPET: ss core cron jobs
## this attempts to restore missing or damaged ss-config using a recent intact backup ##
## however it is not fool-proof since there is no way to verify all settings ##
## validate ss-config ##
VALIDATE_SS_CONFIG=$(grep 'SS_BUILD' /var/www/ss-config)
if [[ -z "$VALIDATE_SS_CONFIG" ]]; then
SS_CONFIG_RECENT_BACKUPS=$(ls -1rta /var/www/backups/config/ss-config.bak* | tail -n1)
SS_CONFIG_BEST_MATCH=$(grep -il 'SS_BUILD' "$SS_CONFIG_RECENT_BACKUPS")
rm -rf /tmp/ss-config
mv -f "$SS_CONFIG_BEST_MATCH" /tmp/ss-config
VALIDATE_TMP_SS_CONFIG=$(grep 'SS_BUILD' /tmp/ss-config)
if [[ -n "$VALIDATE_TMP_SS_CONFIG" ]]; then
mv -f /tmp/ss-config /var/www/ss-config
chown root:root /var/www/ss-config ## must be root:root
chmod 0700 /var/www/ss-config ## 0700 means only root can execute
fi
rm -rf /tmp/ss-config
fi
####################################################################################################
#### B. 05-Cron-Hourly: Validate (Restore) SS-Functions ############################################
####################################################################################################
## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS
## SNIPPET: ss core cron jobs
## this attempts to restore damaged or outdated ss-functions from our public mirrors ##
## we perform this check before cron job tasks as they rely on ss-functions ##
## validate ss-functions ##
VALIDATE_SS_FUNCTIONS=$(grep 'SS_EOF' /var/www/ss-functions)
OUTDATED_SS_FUNCTIONS=$(find "/var/www/ss-functions" -mtime +1)
if [[ -z "$VALIDATE_SS_FUNCTIONS" ]] || [[ -n "$OUTDATED_SS_FUNCTIONS" ]]; then
rm -rf /tmp/ss-functions
wget --no-check-certificate -q -4 -t 1 -T 15 -O /tmp/ss-functions https://raw.githubusercontent.com/littlebizzy/slickstack/master/bash/ss-functions.txt
VALIDATE_TMP_SS_FUNCTIONS=$(grep 'SS_EOF' /tmp/ss-functions)
if [[ -n "$VALIDATE_TMP_SS_FUNCTIONS" ]]; then
mv -f /tmp/ss-functions /var/www/ss-functions
chown root:root /var/www/ss-functions ## must be root:root
chmod 0700 /var/www/ss-functions ## 0700 means only root can execute
else
wget --no-check-certificate -q -4 -t 3 -T 30 -O /tmp/ss-functions https://gitlab.com/littlebizzy/slickstack/-/raw/master/bash/ss-functions.txt
mv -f /tmp/ss-functions /var/www/ss-functions
chown root:root /var/www/ss-functions ## must be root:root
chmod 0700 /var/www/ss-functions ## 0700 means only root can execute
fi
rm -rf /tmp/ss-functions
fi
####################################################################################################
#### C. 05-Cron-Hourly: Validate (Restore) SS-Check + SS-Worker ####################################
####################################################################################################
## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS
## SNIPPET: ss core cron jobs
## this attempts to restore damaged or outdated ss-check and ss-worker bash scripts ##
## they are critical to maintenance tasks and keeping ss core files updated ##
## validate ss-check ##
VALIDATE_SS_CHECK=$(grep 'SS_EOF' /var/www/ss-check)
OUTDATED_SS_CHECK=$(find "/var/www/ss-check" -mtime +1)
if [[ -z "$VALIDATE_SS_CHECK" ]] || [[ -n "$OUTDATED_SS_CHECK" ]]; then
rm -rf /tmp/ss-check
wget --no-check-certificate -q -4 -t 1 -T 15 -O /tmp/ss-check https://raw.githubusercontent.com/littlebizzy/slickstack/master/bash/ss-check.txt
VALIDATE_TMP_SS_CHECK=$(grep 'SS_EOF' /tmp/ss-check)
if [[ -n "$VALIDATE_TMP_SS_CHECK" ]]; then
mv -f /tmp/ss-check /var/www/ss-check
chown root:root /var/www/ss-check ## must be root:root
chmod 0700 /var/www/ss-check ## 0700 means only root can execute
else
wget --no-check-certificate -q -4 -t 3 -T 30 -O /tmp/ss-check https://gitlab.com/littlebizzy/slickstack/-/raw/master/bash/ss-check.txt
mv -f /tmp/ss-check /var/www/ss-check
chown root:root /var/www/ss-check ## must be root:root
chmod 0700 /var/www/ss-check ## 0700 means only root can execute
fi
rm -rf /tmp/ss-check
fi
## validate ss-worker ##
VALIDATE_SS_WORKER=$(grep 'SS_EOF' /var/www/ss-worker)
OUTDATED_SS_WORKER=$(find "/var/www/ss-worker" -mtime +1)
if [[ -z "$VALIDATE_SS_WORKER" ]] || [[ -n "$OUTDATED_SS_WORKER" ]]; then
rm -rf /tmp/ss-worker
wget --no-check-certificate -q -4 -t 1 -T 15 -O /tmp/ss-worker https://raw.githubusercontent.com/littlebizzy/slickstack/master/bash/ss-worker.txt
VALIDATE_TMP_SS_WORKER=$(grep 'SS_EOF' /tmp/ss-worker)
if [[ -n "$VALIDATE_TMP_SS_WORKER" ]]; then
mv -f /tmp/ss-worker /var/www/ss-worker
chown root:root /var/www/ss-worker ## must be root:root
chmod 0700 /var/www/ss-worker ## 0700 means only root can execute
else
wget --no-check-certificate -q -4 -t 3 -T 30 -O /tmp/ss-worker https://gitlab.com/littlebizzy/slickstack/-/raw/master/bash/ss-worker.txt
mv -f /tmp/ss-worker /var/www/ss-worker
chown root:root /var/www/ss-worker ## must be root:root
chmod 0700 /var/www/ss-worker ## 0700 means only root can execute
fi
rm -rf /tmp/ss-worker
fi
####################################################################################################
#### D. 05-Cron-Hourly: Source SS-Config + SS-Functions (After Validated) ##########################
####################################################################################################
## at this point we know that ss-config and ss-functions exist or have been restored ##
## so we source them now to carry on with custom and scheduled cron job tasks ##
## source ss-config ##
source /var/www/ss-config
## source ss-functions ##
source /var/www/ss-functions
## BELOW THIS RELIES ON SS-CONFIG AND SS-FUNCTIONS
####################################################################################################
#### E. 05-Cron-Hourly: Run Custom Tasks (EDIT SOURCED FILES) ######################################
####################################################################################################
## this will run custom shell commands that you can save in a reserved filename below ##
## carefully consider server resources and best practices before customizing ##
## run 05-cron-hourly-custom ##
source "$PATH_05_CRON_HOURLY_CUSTOM"
####################################################################################################
#### F. 05-Cron-Hourly: Touch Timestamp File #######################################################
####################################################################################################
## this is a dummy timestamp file that will remember the last time this script was run ##
## it can be useful for developer reference and is sometimes used by SlickStack ##
## script timestamp ##
touch "$TIMESTAMP_05_CRON_HOURLY"
####################################################################################################
#### G. 05-Cron-Hourly: Run WP-Cron (If Enabled) ###################################################
####################################################################################################
## this snippet will call wp-cron via the server if enabled in your ss-config settings ##
## it is useful for sites with low traffic or that need strict cron management ##
## run wp-cron if set to hourly ##
if [[ "$WP_CRON_METHOD" == "server" ]] && [[ "$WP_CRON_INTERVAL" == "hourly" ]]; then
/usr/bin/php /var/www/html/wp-cron.php
fi
####################################################################################################
#### H. 05-Cron-Hourly: Run Scheduled Tasks (DO NOT EDIT) ##########################################
####################################################################################################
## the below tasks will be called if configured to run at this interval in ss-config ##
## certain tasks are automatically called if the relevant interval is missing ##
## run ss-check if set to hourly ##
if [[ "$INTERVAL_SS_CHECK" == "hourly" ]]; then
source "$PATH_SS_CHECK"
fi
## run ss-worker if set to hourly ##
if [[ "$INTERVAL_SS_WORKER" == "hourly" ]]; then
source "$PATH_SS_WORKER"
fi
## run ss-clean-database if set to hourly ##
if [[ "$INTERVAL_SS_CLEAN_DATABASE" == "hourly" ]]; then
source "$PATH_SS_CLEAN_DATABASE"
fi
## run ss-dump-database if set to hourly or if not defined (default) ##
if [[ "$INTERVAL_SS_DUMP_DATABASE" == "hourly" ]] || [[ -z "$INTERVAL_SS_DUMP_DATABASE" ]]; then
source "$PATH_SS_DUMP_DATABASE"
fi
## run ss-clean-files if set to hourly ##
if [[ "$INTERVAL_SS_CLEAN_FILES" == "hourly" ]]; then
source "$PATH_SS_CLEAN_FILES"
fi
## run ss-dump-files if set to hourly ##
if [[ "$INTERVAL_SS_DUMP_FILES" == "hourly" ]]; then
source "$PATH_SS_DUMP_FILES"
fi
## run ss-sync-staging if set to hourly ##
if [[ "$INTERVAL_SS_SYNC_STAGING" == "hourly" ]]; then
source "$PATH_SS_SYNC_STAGING"
fi
## run ss-perms if set to hourly ##
if [[ "$INTERVAL_SS_PERMS" == "hourly" ]]; then
source "$PATH_SS_PERMS"
fi
####################################################################################################
#### I. 05-Cron-Hourly: Reset Permissions (Cron Jobs + Bash Scripts) ###############################
####################################################################################################
## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS
## SNIPPET: ss core cron jobs
## we hardcode this permissions reset snippet in all ss core cron jobs for redundancy ##
## chmod 0700 means only the root/sudo users can execute the ss core scripts ##
## reset permissions ##
chown root:root /var/www/ss* ## must be root:root
chown root:root /var/www/crons/*cron* ## must be root:root
chown root:root /var/www/crons/custom/*cron* ## must be root:root
chmod 0700 /var/www/ss* ## 0700 means only root can execute
chmod 0700 /var/www/crons/*cron* ## 0700 means only root can execute
chmod 0700 /var/www/crons/custom/*cron* ## 0700 means only root can execute
####################################################################################################
#### J. 05-Cron-Hourly: Delete Lock File ###########################################################
####################################################################################################
## here we delete the lock file associated with this cron job to clear the cron queue ##
## this is technically not necessary but we do it anyway for extra security ##
## delete lock ##
rm "$LOCK_05_CRON_HOURLY"
####################################################################################################
#### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ###############
####################################################################################################
## Ref: https://bash.cyberciti.biz/guide/Setting_up_permissions_on_a_script
## Ref: https://stackoverflow.com/questions/22861580/bash-script-check-if-a-file-contains-a-specific-line
## Ref: https://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash/14201583
## Ref: https://stackoverflow.com/questions/11287861/how-to-check-if-a-file-contains-a-specific-string-using-bash
## Ref: https://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash-
## Ref: https://stackoverflow.com/questions/42377739/while-file-doesnt-contain-string-bash
## SS_EOF