/
save-typo3.sh
306 lines (282 loc) · 9.4 KB
/
save-typo3.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
##########################################################################
# (c) 2020 Yohann CERDAN <cerdanyohann@yahoo.fr>
# All rights reserved
#
# This program is free software : you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
##########################################################################
##########################################################################
# Usage examples :
# ./save-typo3.sh
#
#### backup from a passed typo3 directory
# ./save-typo3.sh -p "/home/html/package/"
#
#### backup to a specify directory
# ./save-typo3.sh -o "/home/html/package6/"
#
#### backup with a special name for sql file
# ./save-typo3.sh -sql "dump.sql"
#
#### backup database only
# ./save-typo3.sh -f -dbonly
#
#### backup without confirmation
# ./save-typo3.sh -f
##########################################################################
# Decode all args
function decodeArgs() {
while [ $# -gt 0 ]
do
case $1 in
"-f")
shift
force=1
;;
"-dbonly")
shift
dbonly=1
;;
"-p")
shift
if [ "$1" != "" ]
then
cd $1
else
echo "The path to the site root is invalid"
fi
shift
;;
"-o")
shift
if [ "$1" != "" ]
then
path_save=$1
else
echo "The path of the save dir is invalid"
fi
shift
;;
"-sql")
shift
if [ "$1" != "" ]
then
path_sql=$1
else
echo "The filename of the sql save file is invalid"
fi
shift
;;
"-h")
shift
echo "-----------------------------------------------------------------------"
echo "TYPO3 backup"
echo "-----------------------------------------------------------------------"
echo "All the parameters are optionnals :"
echo "-f : force save without confirmation"
echo "-dbonly : only save database without compression"
echo "-p <path> : path of the site root directory"
echo "-o <output> : path of the save file directory with final /"
echo "-sql <sql> : filename of the sql file .sql"
exit
;;
*)
shift
;;
esac
done
}
# Check for dependencies
function checkDependency() {
if ! hash $1 2>&-;
then
echo "Failed!"
echo "This script requires '$1' but it can not be found. Aborting."
exit 1
fi
}
date
echo -n "Checking dependencies..."
checkDependency "grep"
checkDependency "sed"
checkDependency "date"
checkDependency "du"
checkDependency "mysql"
checkDependency "mysqldump"
checkDependency "tar"
checkDependency "rm"
checkDependency "awk"
echo "Succeeded."
# force
force=0
dbonly=0
path_save=''
path_sql=''
decodeArgs $*
# DB configuration
if [ -f typo3conf/LocalConfiguration.php ]
then
# v6
path_localconf='typo3conf/LocalConfiguration.php'
typo_db_username=$(grep "'username' => *" $path_localconf | sed -e "s/\s*'username'\s*=>\s*'\(.*\)'\s*,/\1/");
typo_db_password=$(grep "'password' => *" $path_localconf | sed -e "s/\s*'password'\s*=>\s*'\(.*\)'\s*,/\1/");
typo_db_host=$(grep "'host' => *" $path_localconf | sed -e "s/\s*'host'\s*=>\s*'\(.*\)'\s*,/\1/");
typo_db=$(grep "'database' => *" $path_localconf | sed -e "s/\s*'database'\s*=>\s*'\(.*\)'\s*,/\1/");
# v8
if [ -z "$typo_db_username" ]
then
typo_db_username=$(grep "'user' => *" $path_localconf | sed -e "s/\s*'user'\s*=>\s*'\(.*\)'\s*,/\1/");
fi
if [ -z "$typo_db" ]
then
typo_db=$(grep "'dbname' => *" $path_localconf | sed -e "s/\s*'dbname'\s*=>\s*'\(.*\)'\s*,/\1/");
fi
else
# v4
path_localconf='typo3conf/localconf.php'
typo_db_username=$(grep "typo_db_username =*" $path_localconf | sed 's/$typo_db_username = '\''\([^\;]*\)'\'';.*/\1/');
typo_db_password=$(grep "typo_db_password =*" $path_localconf | sed 's/$typo_db_password = '\''\([^\;]*\)'\'';.*/\1/');
typo_db_host=$(grep "typo_db_host =*" $path_localconf | sed 's/$typo_db_host = '\''\([^\;]*\)'\'';.*/\1/');
typo_db=$(grep "typo_db =*" $path_localconf | sed 's/$typo_db = '\''\([^\;]*\)'\'';.*/\1/');
fi
# TYPO3 infos
if [ -f typo3/sysext/core/Classes/Information/Typo3Version.php ]
then
# v10
path_config_default='typo3/sysext/core/Classes/Information/Typo3Version.php'
typo_version=$(grep "const VERSION =" $path_config_default | sed "s/.*const VERSION = '\(.*\)';/\1/");
elif [ -f typo3/sysext/core/Classes/Core/SystemEnvironmentBuilder.php ]
then
# v6-v9
path_config_default='typo3/sysext/core/Classes/Core/SystemEnvironmentBuilder.php'
typo_version=$(grep "define('TYPO3_version',*" $path_config_default | sed "s/.*'TYPO3_version', '\(.*\)');/\1/");
else
# v4
path_config_default='t3lib/config_default.php'
typo_version=$(grep "TYPO_VERSION =*" $path_config_default | sed 's/$TYPO_VERSION = '\''\([^\;]*\)'\'';.*/\1/');
fi
# informations
day_date=$(date +"%Y%m%d")
dir_size=$(du -sh . | sed 's/\.//')
db_size=$(mysql -h$typo_db_host -u$typo_db_username -p$typo_db_password -D$typo_db -e'show table status;' | awk '{sum=sum+$7+$9;} END {print sum/1024/1024}')
# save file .tar.gz
if [ "$path_save" != "" ]
then
filename=$path_save
fi
filename+='export_'$typo_db'-'$day_date'.tar.gz'
# sql file sql
if [ "$path_sql" != "" ]
then
filenamesql=$path_sql
else
filenamesql='export_'$typo_db'-'$day_date'.sql'
fi
echo "-----------------------------------------------------------------------"
echo "Informations"
echo "-----------------------------------------------------------------------"
echo "Date : $day_date"
echo "Website size : $dir_size"
echo "Size of the DB : "$db_size"M"
echo "TYPO3 version : $typo_version"
echo "PATH_site : "$(pwd)
echo "Tar file : $filename"
echo "SQL file : $filenamesql"
echo "-----------------------------------------------------------------------"
echo "Check informations in '$path_localconf'"
echo "-----------------------------------------------------------------------"
echo "typo_db_host : $typo_db_host"
echo "typo_db_username : $typo_db_username"
echo "typo_db : $typo_db"
# force
if [ $force = 0 ]
then
echo
echo -n "Do you want to backup the website? (y or n) : "
read exportok
if [ $exportok != "y" ]
then
exit
fi
fi
# configure tables wich datas will not be imported
ignoretables[0]="cache_extensions"
ignoretables[1]="cache_hash"
ignoretables[2]="cache_imagesizes"
ignoretables[3]="cache_md5params"
ignoretables[4]="cache_pages"
ignoretables[5]="cache_pagesection"
ignoretables[6]="cache_sys_dmail_stat"
ignoretables[7]="cache_treelist"
ignoretables[8]="cache_typo3temp_log"
ignoretables[9]="cf_cache_hash"
ignoretables[10]="cf_cache_hash_tags"
ignoretables[11]="cf_cache_pages"
ignoretables[12]="cf_cache_pages_tags"
ignoretables[13]="cf_cache_pagesection"
ignoretables[14]="cf_cache_pagesection_tags"
ignoretables[15]="cf_cache_rootline"
ignoretables[16]="cf_cache_rootline_tags"
ignoretables[17]="cf_extbase_datamapfactory_datamap"
ignoretables[18]="cf_extbase_datamapfactory_datamap_tags"
ignoretables[19]="cf_extbase_object"
ignoretables[20]="cf_extbase_object_tags"
ignoretables[21]="cf_extbase_reflection"
ignoretables[22]="cf_extbase_reflection_tags"
ignoretables[23]="cf_extbase_typo3dbbackend_tablecolumns"
ignoretables[24]="cf_extbase_typo3dbbackend_tablecolumns_tags"
ignoretables[25]="cachingframework_cache_hash"
ignoretables[26]="cachingframework_cache_hash_tags"
ignoretables[27]="cachingframework_cache_pages"
ignoretables[28]="cachingframework_cache_pages_tags"
ignoretables[29]="cachingframework_cache_pagesection"
ignoretables[30]="cachingframework_cache_pagesection_tags"
ignoretables[31]="cf_adminpanel_requestcache"
ignoretables[32]="cf_adminpanel_requestcache_tags"
ignoretables[33]="cf_cache_imagesizes"
ignoretables[34]="cf_cache_imagesizes_tags"
ignoretableslist=$(printf " --ignore-table=$typo_db.%s" "${ignoretables[@]}")
ignoretableslist=${ignoretableslist:1}
echo
echo "-----------------------------------------------------------------------"
echo "Dump the DB $typo_db..."
echo "-----------------------------------------------------------------------"
mysqldump -d -h$typo_db_host -u$typo_db_username -p$typo_db_password $typo_db > $filenamesql
mysqldump -nt $ignoretableslist -h$typo_db_host -u$typo_db_username -p$typo_db_password $typo_db >> $filenamesql
if [ $dbonly = 1 ]
then
echo "-----------------------------------------------------------------------"
echo -n "Backup success: "
echo $filenamesql
echo "-----------------------------------------------------------------------"
exit
fi
echo "-----------------------------------------------------------------------"
echo "Compress the files and DB..."
echo "-----------------------------------------------------------------------"
tar cfz $filename * .htaccess
echo "-----------------------------------------------------------------------"
echo "Delete $filenamesql..."
echo "-----------------------------------------------------------------------"
rm $filenamesql
echo "-----------------------------------------------------------------------"
echo -n "Backup success: "
if [ "$path_save" != "" ]
then
echo $filename
else
echo $(pwd)"/"$filename
fi
echo "-----------------------------------------------------------------------"
date