-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathoptimise-images-wpcron.sh
executable file
·109 lines (102 loc) · 3.28 KB
/
optimise-images-wpcron.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
#! /bin/bash
########################################################################
# written by George Liu (eva2000) centminmod.com
# wrapper script to optimise-images.sh for
# batch optimise images for wordpress /upload directory via cronjob
# using optimise-images.sh optimise-cron-age or optimise-cron mode
# outlined at https://github.com/centminmod/optimise-images#unattended-subdirectory-runs
#
# instructions set WPUPLOAD_DIR path to full path to your /wp-content/upload/
########################################################################
DT=$(date +"%d%m%y-%H%M%S")
VER='0.3'
DEBUG='n'
WEBP='y'
WPUPLOAD_DIR='/home/nginx/domains/domain.com/public/wp-content/upload'
WPUPLOAD_USER='nginx'
WPUPLOAD_GROUP='nginx'
OPTIMISE_IMAGESCRIPT='/root/tools/optimise-images/optimise-images.sh'
########################################################################
if [[ ! -d "$WPUPLOAD_DIR" ]]; then
echo
echo "Error WPUPLOAD_DIR defined at:"
echo "$WPUPLOAD_DIR"
echo "does not exist"
exit
echo
fi
webp_check() {
if [[ "$WEBP" = [yY] ]]; then
if [ -f "$OPTIMISE_IMAGESCRIPT" ]; then
sed -i "s|IMAGICK_WEBP='n'|IMAGICK_WEBP='y'|" "$OPTIMISE_IMAGESCRIPT"
fi
else
if [ -f "$OPTIMISE_IMAGESCRIPT" ]; then
sed -i "s|IMAGICK_WEBP='y'|IMAGICK_WEBP='n'|" "$OPTIMISE_IMAGESCRIPT"
fi
fi
}
cron_run() {
monthly=$1
webp_check
MONTH="$(date +"%Y\/%m")"
if [[ "$monthly" = 'monthly' ]]; then
WPUPLOAD_DIRLIST=$(find "$WPUPLOAD_DIR" -maxdepth 2 -mindepth 2 -type d -type d \( ! -wholename "$WPUPLOAD_DIR" \) | grep -E '[0-9]{4}' | grep $MONTH | sort)
else
WPUPLOAD_DIRLIST=$(find "$WPUPLOAD_DIR" -maxdepth 2 -mindepth 2 -type d -type d \( ! -wholename "$WPUPLOAD_DIR" \) | grep -E '[0-9]{4}' | sort)
fi
echo "$WPUPLOAD_DIRLIST" | while read d;
do
if [[ "$(ls -Al $d | head -n1 | grep -o 'total 0')" != 'total 0' ]]; then
echo "optimise directory: $d";
echo "$OPTIMISE_IMAGESCRIPT optimise-cron $d";
$OPTIMISE_IMAGESCRIPT optimise-cron $d;
chown -R ${WPUPLOAD_USER}:${WPUPLOAD_GROUP} $d;
else
echo "skipping empty $d"
fi
done
# chown -R ${WPUPLOAD_USER}:${WPUPLOAD_GROUP} "$WPUPLOAD_DIR"
}
cron_age() {
monthly=$1
webp_check
MONTH="$(date +"%Y\/%m")"
if [[ "$monthly" = 'monthly' ]]; then
WPUPLOAD_DIRLIST=$(find "$WPUPLOAD_DIR" -maxdepth 2 -mindepth 2 -type d -type d \( ! -wholename "$WPUPLOAD_DIR" \) | grep -E '[0-9]{4}' | grep $MONTH | sort)
else
WPUPLOAD_DIRLIST=$(find "$WPUPLOAD_DIR" -maxdepth 2 -mindepth 2 -type d -type d \( ! -wholename "$WPUPLOAD_DIR" \) | grep -E '[0-9]{4}' | sort)
fi
echo "$WPUPLOAD_DIRLIST" | while read d;
do
if [[ "$(ls -Al $d | head -n1 | grep -o 'total 0')" != 'total 0' ]]; then
echo "optimise directory: $d";
echo "$OPTIMISE_IMAGESCRIPT optimise-cron-age $d";
$OPTIMISE_IMAGESCRIPT optimise-cron-age $d;
chown -R ${WPUPLOAD_USER}:${WPUPLOAD_GROUP} $d;
else
echo "skipping empty $d"
fi
done
# chown -R ${WPUPLOAD_USER}:${WPUPLOAD_GROUP} "$WPUPLOAD_DIR"
}
case "$1" in
cron )
cron_run
;;
cron-age )
cron_age
;;
cron-monthly )
cron_run monthly
;;
cron-age-monthly )
cron_age monthly
;;
* )
echo "Usage:"
echo
echo "$0 {cron|cron-age|cron-monthly|cron-age-monthly}"
echo
;;
esac