· 6 years ago · Apr 03, 2019, 03:36 PM
1#!/bin/sh
2
3# joomlaupdate
4# Updates your Joomla 2.5/3.1 website to the latest version.
5#
6# Usage: joomlaupdate [-s] [-b] [-h] [-l]
7#
8# Default action is verbose on, no backup.
9# -s Silent. Do not display any informational messages.
10# -b Backup. Create a backup before updating.
11# -l Log. Write messages to logile /var/log/joomlaupdate.log.
12# -h Help. Display this info.
13# Run joomlaupdate at the root of your website, where the configuration.php is.
14#
15# Copyright 2013 Rene Kreijveld - email@renekreijveld.nl
16#
17# This program is free software; you may redistribute it and/or modify it.
18#
19# Necessary tools for this script: wget, unzip, tar and perl. Install these if they are not available.
20#
21# Version history
22# 1.0: - Initial version.
23# 1.1: - Added support for Joomla 3.0 and 3.1.
24# - Better logging, added -l parameter.
25# - Clode cleanup.
26# 1.2: - Added backup path (suggestion by Remco Janssen).
27# 1.3: - Removed support for Joomla 3.0.
28# - Fixed bug of not all SQL updates being run (thanks Xavier Pallicer).
29# 1.4: - Added updating of #__schemas table. This was missing (thanks Xavier Pallicer).
30# 1.5: - Added updating of #__extensions table. This was missing (thanks Xavier Pallicer).
31#
32
33# general variables
34version=1.5
35logfile=/var/log/joomlaupdate.log
36backuppath=../
37
38# find mysql socket
39if [ -S /var/lib/mysql/mysql.sock ]; then
40 mysock=/var/lib/mysql/mysql.sock
41elif [ -S /var/run/mysqld/mysqld.sock ]; then
42 mysock=/var/run/mysqld/mysqld.sock
43elif [ -S /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock ]; then
44 mysock=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
45elif [ -S /tmp/mysql.sock ]; then
46 mysock=/tmp/mysql.sock
47fi
48
49# display usage information
50usage() {
51 echo "joomlaupdate version $version, written by Rene Kreijveld."
52 echo " "
53 echo "Usage: joomlaupdate [-s] [-b] [-l] [-h]"
54 echo " "
55 echo "Default action is verbose on, no backup."
56 echo "-s Silent. Do not display any informational messages."
57 echo "-b Backup. Create a backup before updating."
58 echo "-l Log. Write messages to logile /var/log/joomlaupdate.log."
59 echo "-h Help. Display this info."
60 echo " "
61 echo "Run joomlaupdate at the root of your website, where the configuration.php is."
62 echo " "
63 exit 0
64}
65
66# process the arguments
67while getopts sblh opt
68do
69 case "$opt" in
70 s) silent="yes";;
71 b) backup="yes";;
72 l) logging="yes";;
73 h) usage;;
74 \?) usage;;
75 esac
76done
77
78# echo out messages to screen and/or logfile
79eout() {
80 mesg=$1
81 stdout=$2
82
83 # if logfile doesn't exist, create it first
84 if [ ! -f "$logfile" ]; then
85 touch $logfile
86 fi
87
88 # if not silent display message
89 if [ "$silent" != "yes" ]; then
90 if [ ! -z "$stdout" ]; then
91 echo "$mesg"
92 fi
93 fi
94
95 # if logging enabled message to logfile
96 if [ "$logging" == "yes" ]; then
97 echo "$mesg" >> $logfile
98 fi
99}
100
101# grab information from Joomla 2.5/3.1 configuration.
102do_joomla2531() {
103 sitename=`grep '$sitename =' configuration.php | cut -d \' -f 2 | sed 's/ /_/g'`
104 versr=`grep '$RELEASE' libraries/cms/version/version.php | cut -d \' -f 2`
105 versd=`grep '$DEV_LEVEL' libraries/cms/version/version.php | cut -d \' -f 2`
106 verss=`grep '$DEV_STATUS' libraries/cms/version/version.php | cut -d \' -f 2`
107 database=`grep '$db =' configuration.php | cut -d \' -f 2`
108 dbuser=`grep '$user =' configuration.php | cut -d \' -f 2`
109 password=`grep '$password =' configuration.php | cut -d \' -f 2`
110 host=`grep '$host =' configuration.php | cut -d \' -f 2`
111 prefix=`grep '$dbprefix =' configuration.php | cut -d \' -f 2`
112}
113
114# cleanup all temporary files
115do_cleanup() {
116 if [ -f list1.xml ]; then
117 rm -f list1.xml
118 fi
119 if [ -f list2.xml ]; then
120 rm -f list2.xml
121 fi
122 if [ -f update.zip ]; then
123 rm -f update.zip
124 fi
125 if [ -f tmp/sqlupdate.sql ]; then
126 rm -f tmp/sqlupdate.sql
127 fi
128 if [ -f $backuppath$database.sql ]; then
129 rm -f $backuppath$database.sql
130 fi
131 if [ -f joomla.xml ]; then
132 rm -f joomla.xml
133 fi
134}
135
136# create a full backup of the joomla website
137do_backup() {
138 # dump the database to a .sql file
139 eout "Backup requested." 1
140 eout "Creating database backup." 1
141
142 # check if sql dumpfile already exists
143 if [ -f $backuppath$database.sql ]; then
144 eout "Database backup file $backuppath$database.sql already exists. Exiting." 1
145 do_cleanup
146 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
147 eout "$datestamp: End joomlaupdate."
148 exit 1
149 fi
150
151 # create mysqldump
152 if mysqldump --skip-opt --add-drop-table --add-locks --create-options --disable-keys --lock-tables --quick --set-charset --host=$host --user=$dbuser --password=$password --socket=$mysock $database > $backuppath$database.sql; then
153 eout "$backuppath$database.sql created." 1
154 else
155 eout "Error creating database dump, exiting." 1
156 do_cleanup
157 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
158 eout "$datestamp: End joomlaupdate."
159 exit 1
160 fi
161
162 # check if tgz backupfile doesn't already exist
163 if [ -f $backuppath$sitename.tgz ]; then
164 eout "Backup file $backuppath$sitename already exists. Exiting." 1
165 do_cleanup
166 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
167 eout "$datestamp: End joomlaupdate."
168 exit 1
169 fi
170
171 # create the .tgz backup
172 eout "Creating files backup." 1
173 tar czf $backuppath$sitename.tgz .htaccess *
174 eout "Your website backup is ready in $backuppath$sitename.tgz." 1
175 eout " " 1
176}
177
178get_joomla_info() {
179 # Grab owner and group of current configuration.php
180 owner=`ls -l configuration.php | awk '{print$3}'`
181 group=`ls -l configuration.php | awk '{print$4}'`
182
183 # Testing for Joomla version 2.5 or 3.1
184 if [ -f libraries/cms/version/version.php ]; then
185 release=`grep '$RELEASE' libraries/cms/version/version.php | cut -d \' -f 2`
186 if [ "$release" == "2.5" ]; then
187 # grab information about this 2.5 website
188 do_joomla2531
189 fi
190
191 if [ "$release" == "3.1" ]; then
192 # grab information about this 3.1 website
193 do_joomla2531
194 fi
195 fi
196}
197
198datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
199if [ "$logging" == "yes" ]; then
200 eout " "
201 eout "$datestamp: Start joomlaupdate."
202fi
203
204eout "joomlaupdate version $version, written by Rene Kreijveld." 1
205eout " " 1
206
207# if configuration.php present then proceed
208if [ -f configuration.php ]; then
209 # display information about current website
210 get_joomla_info
211 eout "This Joomla! website:" 1
212 eout "Sitename : $sitename" 1
213 eout "Version : $versr.$versd" 1
214 eout "DB Name : $database" 1
215 eout "DB User : $dbuser" 1
216 eout "DB Password : $password" 1
217 eout "DB Host : $host" 1
218 eout "DB Prefix : $prefix" 1
219 eout "Path : `pwd`" 1
220 eout "Owner : $owner" 1
221 eout "Group : $group" 1
222 eout " " 1
223
224 # store old development version
225 oldversd=$versd
226 oldversfull="$versr.$versd"
227
228 # get update list from joomla.org
229 updatelist=`grep "<server" -m 1 administrator/manifests/files/joomla.xml | awk -F"<server type=\"collection\">" '{print$2}' | awk -F"</server>" '{print$1}'`
230 wget $updatelist -q -O list1.xml >/dev/null 2>/dev/null
231 updatelist="unknown"
232
233 # grab information about update file and latest version number
234 if [ "$versr" == "2.5" ]; then
235 updatelist=`grep "targetplatformversion=\"2.5\"" -m 1 list1.xml | awk -F"detailsurl=\"" '{print$2}' | awk -F"\" />" '{print$1}'`
236 newversion=`grep "targetplatformversion=\"2.5\"" -m 1 list1.xml | awk -F"version=\"" '{print$2}' | awk -F"\"" '{print$1}'`
237 fi
238 if [ "$versr" == "3.1" ]; then
239 updatelist=`grep "targetplatformversion=\"3.1\"" -m 1 list1.xml | awk -F"detailsurl=\"" '{print$2}' | awk -F"\" />" '{print$1}'`
240 newversion=`grep "targetplatformversion=\"3.1\"" -m 1 list1.xml | awk -F"version=\"" '{print$2}' | awk -F"\"" '{print$1}'`
241 fi
242
243 # this website is not 2.5/3.1, exiting
244 if [ "$updatelist" == "unknown" ]; then
245 eout "This Joomla version cannot be updated. Exiting." 1
246 do_cleanup
247 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
248 eout "$datestamp: End joomlaupdate."
249 exit 0
250 fi
251
252 # if current version number equals latest version number no update is necessary
253 if [ "$versr.$versd" == "$newversion" ]; then
254 eout "This Joomla website is already up-to-date. Exiting." 1
255 do_cleanup
256 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
257 eout "$datestamp: End joomlaupdate."
258 exit 0
259 else
260 # if backup needed, create backup first
261 if [ "$backup" == "yes" ]; then
262 do_backup
263 fi
264
265 eout "This website will be updated to version $newversion." 1
266 wget $updatelist -q -O list2.xml >/dev/null 2>/dev/null
267
268 # get url of update zipfile
269 updatefile=`grep "x_to_$newversion" -m 1 list2.xml | awk -F">" '{print$2}' | awk -F"</downloadurl" '{print$1}'`
270 eout "Downloading updatefile: $updatefile." 1
271
272 # download update zipfile
273 wget $updatefile -O update.zip >/dev/null 2>/dev/null
274
275 # Updating files
276 eout "Extracting zipfile, updating files." 1
277
278 # update files
279 unzip -q -o update.zip
280
281 # retrieve new version info
282 get_joomla_info
283
284 # set rights
285 eout "Setting file ownership." 1
286 chown -R $owner:$group .htaccess *
287
288 # execute all mysql updates frm previous version to current version
289 oldversd=`expr $oldversd + 1`
290 eout " " 1
291 for (( u=oldversd; u<=versd; u++ )); do
292 for sqlfile in `ls administrator/components/com_admin/sql/updates/mysql/$versr.$u*sql`; do
293 eout "Running SQL update $sqlfile." 1
294
295 # copy sql update file to temporary directory
296 cp $sqlfile tmp/sqlupdate.sql
297
298 # modify prefix so it matches for this website
299 perl -pi -e "s/#__/$prefix/g" tmp/sqlupdate.sql
300
301 # run sql updates
302 if mysql --host=$host --user=$dbuser --password=$password --socket=$mysock $database < tmp/sqlupdate.sql
303 then
304 eout "SQL update completed." 1
305 rm -f tmp/sqlupdate.sql
306 else
307 eout "Error running SQL update. Exiting." 1
308 do_cleanup
309 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
310 eout "$datestamp: End joomlaupdate."
311 exit 1
312 fi
313 done
314 done
315
316 # fix joomla version in #__schema and #__extension tables
317 sql="UPDATE #__schemas SET version_id = \"$versr.$versd\" WHERE extension_id = 700;"
318 echo $sql | sed -e "s/#__/$prefix/" > tmp/sqlupdate.sql
319 sql="UPDATE #__extensions SET manifest_cache = REPLACE(manifest_cache, '\"version\":\"$oldversfull\"', '\"version\":\"$versr.$versd\"') WHERE extension_id = 700;"
320 echo $sql | sed -e "s/#__/$prefix/" >> tmp/sqlupdate.sql
321
322 if mysql --host=$host --user=$dbuser --password=$password --socket=$mysock $database < tmp/sqlupdate.sql
323 then
324 eout "Succesfully updated #__schemas and #__extension tables." 1
325 rm -f tmp/sqlupdate.sql
326 else
327 eout "Error updating #__schemas and #__extensions tables. Exiting." 1
328 do_cleanup
329 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
330 eout "$datestamp: End joomlaupdate."
331 exit 1
332 fi
333
334 eout " " 1
335 eout "Cleaning up temporary files." 1
336 do_cleanup
337
338 # update and display joomla info
339 eout " " 1
340 eout "Joomla update finished. Website info:" 1
341 eout "Sitename : $sitename" 1
342 eout "Version : $versr.$versd" 1
343 eout "Do not forget to update your language files." 1
344
345 # finished
346 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
347 eout "$datestamp: End joomlaupdate."
348 fi
349else
350 eout "File configuration.php not found. Are you at the root of the site?" 1
351 datestamp=`date +"%d-%m-%Y, %H:%M:%S"`
352 eout "$datestamp: End joomlaupdate."
353 eout " "
354 exit 1
355fi