-
Notifications
You must be signed in to change notification settings - Fork 0
/
MODXUpgradeScript
206 lines (175 loc) · 6.33 KB
/
MODXUpgradeScript
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
#!/bin/bash
# MODX Update Script
# -------------------
# This shell script automate the upgrade of a MODX Revolution installation
# on command line (CLI) on a webserver.
# Is not bullet proof and has to be understood and adapted to each user.
# I wrote it because upgrading 60 Revolution website is long and boring.
# With it, you can update a Revolution Website in 8 seconds without mouse
#
# BE SURE TO HAVE FONCTIONNAL BACKUP BEFORE USING THIS SCRIPT (database dump AND files)
#
# Thanks to Christian Hanvey for the inspiration https://gist.github.com/christianhanvey/5592012
# --------------
# Config section
# --------------
SOURCE_DIR="/root/utils"
MODX_PACKAGE_MAX_AGE=86400
MODX_PACKAGE_LAST_FILENAME="modx-lastest.zip"
MODX_LAST_DIR="modx-lastest"
# ---------------
# Start of script
# ---------------
# Set to exit on error
set -e
# Define some colours
red='\e[0;31m'
green='\e[0;32m'
yellow='\e[1;33m'
litegreen='\e[1;32m'
purple='\e[0;35m'
NC='\e[0m' # No Color
# -------------------
# FUNCTION : Cleaning
# -------------------
# 1/ Remove all packages, sources directory and symlink of MODX in the $SOURCE_DIR
function clean_modx {
echo -e "${litegreen}-------------------- ${NC}"
echo -e "${litegreen}Cleaning old package ${NC}"
echo -e "${litegreen}-------------------- ${NC}"
# Delete old package, directories and symlink
rm -fr $SOURCE_DIR/{modx-2*,$MODX_LAST_DIR}
rm -f $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME
echo -e "-> old MODX packages and directories removed from $SOURCE_DIR/ ${green}[OK]${NC}"
echo ""
}
# -------------------
# FUNCTION : Get MODX
# -------------------
# 1/ Check if package is older than a day before downloading it again
# 2/ Remove older source package and directories
# 3/ Download last MODX version and extract it to $SOURCE_DIR
# 4/ Make a symlink from $SOURCE_DIR/modx-2.3.1-pl to $SOURCE_DIR/modx-lastest
function get_modx {
echo -e "${litegreen}------------${NC}"
echo -e "${litegreen}Getting MODX${NC}"
echo -e "${litegreen}------------${NC}"
# Downloading only if not exist or it's to older
if [ -f $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME ]
then
MODX_PACKAGE_LAST_DOWNLOADED_DATE=`stat -c %X $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME`
CURRENT_DATE=`date +%s`
MODX_PACKAGE_CURRENT_AGE=$(( ($CURRENT_DATE - $MODX_PACKAGE_LAST_DOWNLOADED_DATE) ))
else
MODX_PACKAGE_CURRENT_AGE=86400000
fi
if [ $MODX_PACKAGE_CURRENT_AGE -gt $MODX_PACKAGE_MAX_AGE ]
then
# Function call to cleaning old package
clean_modx
# Getting last MODX package
wget -O $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME http://modx.com/download/latest
echo -e "-> download MODX package ${green}[OK]${NC}"
# Unzip the package on the Source Directory
unzip -o -q $SOURCE_DIR/$MODX_PACKAGE_LAST_FILENAME -d $SOURCE_DIR
echo -e "-> extract MODX package inside $SOURCE_DIR/ ${green}[OK]${NC}"
# Finding the name of the lastest created directory during the extraction
LAST_CREATED_DIR=$(find $SOURCE_DIR/modx-2* -type d -prune -exec ls -d {} \; |tail -1)
# Symbolic link is created to set an unvariable path to the last version of MODX
ln -s $LAST_CREATED_DIR $SOURCE_DIR/$MODX_LAST_DIR
echo -e "-> symbolic link to $SOURCE_DIR/$MODX_LAST_DIR ${green}[OK]${NC}"
echo ""
else
echo -e "${green}[INFO]${NC} The MODX package is very fresh (less than 24h). Not necessary to download it again."
echo ""
fi
}
# -------------------
# FUNCTION : Put MODX
# -------------------
# 1/ Check if we are in the right place (the root of one Revo Website). The test is made for my server
# 2/ If yes, copy new MODX files to the current directory (the project which has to be upgraded)
# 3/ Setting correct owner to the files inside the directory project to upgrade
function put_modx {
echo -e "${litegreen}----------------------------------${NC}"
echo -e "${litegreen}Copy new MODX files on the project${NC}"
echo -e "${litegreen}----------------------------------${NC}"
# Directory verification : we're checking the owner of the directory where we are
# It's specidic to my server... so adapt to yours
# If we are not on a correct directory project, we stop
PROJECT_DIR_OWNER=$(ls -ld .|awk '{ print $3 }')
if [ "$(echo $PROJECT_DIR_OWNER | grep -E "vu([0-9]{4})")" ]
then
echo ""
echo "Copying the last files of MODX in ${PWD}"
cp -Rf /root/utils/modx-lastest/* ./
echo -e " -> done ${green}[OK]${NC}"
echo ""
echo "Setting correct owner to the files inside ${PWD}"
chown -R $PROJECT_DIR_OWNER.www-data ./
echo -e " -> done ${green}[OK]${NC}"
echo ""
else
echo "CANCELATION : you are not in a correct project directory !!"
exit
fi
}
# -----------------------
# FUNCTION : Upgrade MODX
# -----------------------
# 1/ The fabulous CLI to upgrade in 7 seconds. I'm using an optionnal config-modx.xml file just to set the language
function up_modx {
echo -e "${litegreen}------------------------ ${NC}"
echo -e "${litegreen}Running the MODX upgrade ${NC}"
echo -e "${litegreen}------------------------ ${NC}"
echo "Upgrading in progress..."
/usr/bin/php ./setup/index.php --installmode=upgrade --config=/root/bin/config-modx.xml
}
# ============
# Introduction
# ============
echo ''
echo -e "${litegreen}* * * * * * * * * * ${NC}"
echo -e "${litegreen}MODX UPGRADE SCRIPT ${NC}"
echo -e "${litegreen}* * * * * * * * * * ${NC}"
echo ''
echo -e "What do you want to do ?"
echo -e "------------------------"
echo -e "- get : getting the last MODX Revolution package and unpack it on $SOURCE_DIR/$MODX_LAST_DIR"
echo -e "- put : copy $SOURCE_DIR/$MODX_LAST_DIR to ${yellow}${PWD}${NC}"
echo -e "- up : run upgrade command in ${yellow}${PWD}${NC}"
echo -e "- put-up : copy AND run upgrade command in ${yellow}${PWD}${NC}"
echo -e "- clean : remove every MODX sources in $SOURCE_DIR/"
echo ''
# ===========
# The choices
# ===========
echo -e "You are in ${PWD}"
PS3='Please enter your choice : '
options=("get" "put" "up" "put-up" "clean" "Quit")
select opt in "${options[@]}"
do
case $opt in
"get")
get_modx;;
"put")
put_modx;;
"up")
up_modx
exit
;;
"put-up")
put_modx
up_modx
exit
;;
"clean")
clean_modx
exit
;;
"Quit")
break
;;
*) echo -e "${red}[ERROR]${NC} Invalid option";;
esac
done