-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsk_importer_beta0.4.sh
418 lines (396 loc) · 12.8 KB
/
sk_importer_beta0.4.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/bin/bash
# SK-importer
# skamasle.com | @skamasle
# Run at your own risk
# Import cPanel backup Into VestaCP
# beta 0.4.5 | fix permisions
# Add skip database if alredy exists
# Cron still not working...
# 28 may 2016
# Ask or report bugs on twitter @skamasle, mail: [email protected]
# This script dont restore databases if you dont have one user asigned to database
# This script dont work yet if your cPanel has database prefix disabled.
# This script get all your databases and only export "localhost" databases
# so this script dont take "remote cPanel mysql hosts"
# If your cPanel has remote mysql data bases and it connect to orther host you may change "conect_to", but
# I cant warranty that work yet.
# This script dont restore mail password because cPanel use shadown and vestacp use md5, so
# This script will assing new password for mail account and show you at the end of restore.
##
# Put this to 0 if you want use bash -x to debug it
sk_debug=1
sk_vesta_package=default
#
# Only for gen_password but I dont like it, a lot of lt
# maybe will use it for orther functions :)
source /usr/local/vesta/func/main.sh
sk_file=$1
sk_tmp=sk_tmp
sk_file_name=$(ls $sk_file)
tput setaf 2
echo "Checking provided file..."
tput sgr0
if file $sk_file |grep -q -c "gzip compressed data, from Unix" ; then
tput setaf 2
echo "OK - Gziped File"
tput sgr0
if [ ! -d /root/$sk_tmp ]; then
echo "Creating tmp.."
mkdir /root/$sk_tmp
fi
echo "Extracting backup..."
if [ "$sk_debug" != 0 ]; then
tar xzvf $sk_file -C /root/$sk_tmp 2>&1 |
while read sk_extracted_file; do
ex=$((ex+1))
echo -en "wait... $ex files extracted\r"
done
else
tar xzf $sk_file -C /root/$sk_tmp
fi
if [ $? -eq 0 ];then
tput setaf 2
echo "Backup extracted whitout errors..."
tput sgr0
else
echo "Error on backup extraction, check your file, try extract it manually"
echo "Remove tmp"
rm -rf /root/$sk_tmp
exit 1
fi
else
echo "Error 3 not-gzip - no stantard cpanel backup provided of file not installed ( Try yum install file, or apt-get install file )"
rm -rf /root/$sk_tmp
exit 3
fi
cd /root/$sk_tmp/*
sk_importer_in=$(pwd)
echo "Access tmp directory $sk_importer_in"
echo "Get prefix..."
sk_dead_prefix=$(cat meta/dbprefix)
if [ $sk_dead_prefix = 1 ]; then
echo "Error 666 - I dont like your prefix, I dont want do this job"
exit 666
else
echo "I like your prefix, start working"
fi
echo "Remove and move some files.."
rm mysql/openfileslimit -f
mv mysql/roundcube.sql .
main_domain1=$(grep main_domain userdata/main |cut -d " " -f2)
if [ "$(ls -A mysql)" ]; then
sk_cp_user=$(ls mysql |grep sql | grep -v roundcube.sql |head -n1 |cut -d "_" -f1)
if [ -z "$sk_cp_user" ]; then
sk_cp_user=$(grep "user:" userdata/$main_domain1 | cut -d " " -f2)
fi
echo "$sk_cp_user" > sk_db_prefix
tput setaf 2
echo "Get user: $sk_cp_user"
tput sgr0
sk_restore_dbs=0
else
sk_restore_dbs=1
# get real cPanel user if no databases exist
sk_cp_user=$(grep "user:" userdata/$main_domain1 | cut -d " " -f2)
fi
# So get real user, may be we need it before
sk_real_cp_user=$(grep "user:" userdata/$main_domain1 | cut -d " " -f2)
if /usr/local/vesta/bin/v-list-users | grep -q -w $sk_cp_user ;then
echo "User alredy exist on your server, maybe on vestacp or in your /etc/passwd"
echo "**"
echo "Grep your /etc/password"
grep -q -w $sk_cp_user /etc/password
echo "**"
echo "Stop Working, clening..."
rm -rf /root/$sk_tmp
exit 21
else
echo "Generate password aleatory password for $sk_cp_user and create Vestacp Account ..."
sk_password=$(gen_password)
/usr/local/vesta/bin/v-add-user $sk_cp_user $sk_password administrator@$main_domain1 $sk_vesta_package $sk_cp_user $sk_cp_user
if [ $? != 0 ]; then
tput setaf 2
echo "Stop Working... Cant create user...if is fresh install of vestacp try reboot or reopen session check bug https://bugs.vestacp.com/issues/138"
tput sgr0
rm -rf /root/$sk_tmp
exit 4
fi
fi
#########################
#First file I do it in 3 files
# Mysql database importer from cPanel to vestacp
# Version beta 0.2 working version
# skamasle.com | @skamasle
# 31 mar 2016
function sk_start_restore_db () {
DATE=$(date +%F)
TIME=$(date +%T)
conect_to=localhost
user_db_tmp=usertmp1
db_tmp=dbtmp1
curl -s -O mirror.skamasle.com/vestacp/skcpanelimporter/sk-db-sed
tput setaf 2
echo "Get databases"
tput sgr0
grep $conect_to mysql.sql > sk-database
# not modified .sql file for create bds.
cp sk-database sk-database-full.sql
echo "Fix some files"
function fix_sk_dbs1() {
while IFS= read -r line; do
sed -i -e "s/$line//g" sk-database
done
}
fix_sk_dbs1 < "sk-db-sed"
#Not one liner, easy to edit and debuging
sed -i 's/@/ /g' sk-database
sed -i 's/\\//g' sk-database
sed -i "s/'//g" sk-database
sed -i "s/\`/ /g" sk-database
sed -i 's/;/ /g' sk-database
sed -i 's/GRANT ALL PRIVILEGES/GRANDES/g' sk-database
sed -i 's/GRANT USAGE/GRANUSO/g' sk-database
sed -i 's/GRANT/GRANDES/g' sk-database
sed -i "s/\*.\*//g" sk-database
sed -i 's/ \+/ /g' sk-database
sed -i "s/\.\*//g" sk-database
#sed -i 's/,//g' sk-database
touch sk_mysql_import.sql
mkdir $user_db_tmp
mkdir $db_tmp
######################
######################
# check local databases
sk_local_bds=$(echo "show databases;" | mysql)
mkdir sk_local_data_bases
for skldb in $sk_local_bds
do
touch sk_local_data_bases/$skldb
done
#######################
#######################
###
#Get BDS, users, passwords, and users and password again...
sk_databases=$(grep "GRANDES" sk-database |awk '{ print $2 }')
users_db=$(grep "GRANUSO" sk-database |awk '{ print $2 }')
# need some fix for detect real main user.. for now working
# 4 abril - fixed issue dont need it --pending remove --
#main_user=$(grep "GRAN" sk-database |awk '{ print $2 }' | cut -d "_" -f1 |head -1)
grep "GRANUSO" sk-database > sk_userpasusage
grep "GRANDES" sk-database > sk_db_user
echo "Working whit db..."
for user in $users_db
do
touch $user_db_tmp/$user
done
for sk_db in $sk_databases
do
touch $db_tmp/$sk_db
done
tput setaf 2
echo "Get mysql passwords.."
tput sgr0
function get_user_pass() {
while read a1 user a3 a4 a5 a6 pass
do
echo "$pass" > $user_db_tmp/$user
done
}
get_user_pass < sk_userpasusage
rm sk_userpasusage -f
# remove main user
rm -f $user_db_tmp/$sk_real_cp_user
# procesamos usuarios y bds..
echo "Start Importing Mysql Databases and Users..."
function get_db_user() {
b=0
while read a1 db userdb host
do
#if [ "$userdb" != "$main_user" ];then sk_real_cp_user
# Change this for compatibility whit account whit user longestthan 8 characters
# Added function to skip database if alredy on system only skip dont log or inform yet about it
if [[ "$userdb" != "$sk_real_cp_user" && ! -e sk_local_data_bases/$db ]]; then
if [ -e $user_db_tmp/$userdb ]; then
end_user_pass=$(cat $user_db_tmp/$userdb)
echo "DB='$db' DBUSER='$userdb' MD5='$end_user_pass' HOST='localhost' TYPE='mysql' CHARSET='UTF8' U_DISK='0' SUSPENDED='no' TIME='$TIME' DATE='$DATE'" >> /usr/local/vesta/data/users/$sk_cp_user/db.conf
mysql < mysql/$db.create
echo "GRANT ALL PRIVILEGES ON $db.* TO '$userdb'@'localhost';" >> sk_mysql_import.sql
echo "GRANT USAGE ON $db.* TO '$userdb'@'localhost' IDENTIFIED BY PASSWORD '$end_user_pass';" >> sk_mysql_import.sql
echo "Importing database $db ..."
#some people get gziped dbs
if [ -e mysql/$db.sql.gz ]; then
gunzip mysql/$db.sql.gz
fi
mysql $db < mysql/$db.sql
((b++))
#else
# echo "Skip......"
# need some functions here, maybe some one use main db user...
fi
else
if [ -e sk_local_data_bases/$db ]; then
tput setaf 1
tput bold
echo "Skip database $db I found it in your mysql."
tput sgr0
elif [ "$db" == "SKGRANTS" ];then
tput setaf 1
tput bold
echo "Detect user whit GRANT ALL *.* skip, this can be security risk on shared hosting"
tput sgr0
fi
fi
done
}
get_db_user < sk_db_user
rm -f sk_db_user
########## lalala
mysql < sk_mysql_import.sql
if [ "$b" -gt 0 ]; then
tput setaf 4
echo "$b databases imported!"
tput sgr0
else
# may something fail if see this, we check if there are databases whit sk_restore_dbs some lines before
echo "ups error 721 - bug ? no database imported, you should not see this message, report it "
fi
# this need more work, if user exists and have databases so, some wc -l and orther things, but
# for now working.
sed -i "s/U_DATABASES='0'/U_DATABASES='$b'/g" /usr/local/vesta/data/users/$sk_cp_user/user.conf
}
if [ "$sk_restore_dbs" -eq 0 ]; then
sk_start_restore_db
else
echo "No databases found for restore"
fi
tput setaf 2
echo "Start working whit web sites / files / domains / subdomains"
tput sgr0
##########
# second file
# file importer
skaddons=$(cat addons |cut -d "=" -f1)
sed -i 's/_/./g; s/=/ /g' addons
echo "Converting addons domains, subdomains and some orther fun"
cp sds sk_sds
cp sds2 sk_sds2
sed -i 's/_/./g' sk_sds
sed -i 's/public_html/public@html/g; s/_/./g; s/public@html/public_html/g; s/=/ /g; s/$sk_default_sub/@/g' sk_sds2
cat addons | while read sk_addon_domain sk_addon_sub
do
echo "Converting default subdomain: $sk_addon_sub in domain: $sk_addon_domain"
sed -i -e "s/$sk_addon_sub/$sk_addon_domain/g" sk_sds
sed -i -e "s/$sk_addon_sub/$sk_addon_domain/g" sk_sds2
mv userdata/$sk_addon_sub userdata/$sk_addon_domain
done
tput setaf 2
echo "Start restoring domains"
tput sgr0
function get_domain_path() {
while read sk_domain path
do
if [ -e userdata/$sk_domain ];then
v-add-domain $sk_cp_user $sk_domain
echo "Restoring $sk_domain..."
rm -f /home/$sk_cp_user/web/$sk_domain/public_html/index.html
if [ "$sk_debug" != 0 ]; then
rsync -av homedir/$path/ /home/$sk_cp_user/web/$sk_domain/public_html 2>&1 |
while read sk_file_dm; do
sk_sync=$((sk_sync+1))
echo -en "Working: $sk_sync restored files\r"
done
else
rsync homedir/$path/ /home/$sk_cp_user/web/$sk_domain/public_html
fi
chown $sk_cp_user:$sk_cp_user -R /home/$sk_cp_user/web/$sk_domain/public_html
chmod 751 /home/$sk_cp_user/web/$sk_domain/public_html
echo "$sk_domain" >> exclude_path
fi
done
}
get_domain_path < sk_sds2
/usr/local/vesta/bin/v-add-domain $sk_cp_user $main_domain1
# need it for restore main domain
if [ ! -e exclude_path ];then
touch exclude_path
fi
echo "Restore main domain: $main_domain1"
rm -f /home/$sk_cp_user/web/$main_domain1/public_html/index.html
if [ "$sk_debug" != 0 ]; then
rsync -av --exclude-from='exclude_path' homedir/public_html/ /home/$sk_cp_user/web/$main_domain1/public_html 2>&1 |
while read sk_file_dm; do
sk_sync=$((sk_sync+1))
echo -en "Working: $sk_sync restored files\r"
done
else
rsync --exclude-from='exclude_path' homedir/public_html/ /home/$sk_cp_user/web/$main_domain1/public_html 2>&1
fi
chown $sk_cp_user:$sk_cp_user -R /home/$sk_cp_user/web/$main_domain1/public_html
chmod 751 /home/$sk_cp_user/web/$main_domain1/public_html
rm -f sk_sds2 sk_sds
##################
# mail
tput setaf 2
echo "Start Restoring Mails"
tput sgr0
sk_mdir=$sk_importer_in/homedir/mail
cd $sk_mdir
for sk_maild in $(ls -1)
do
if [[ "$sk_maild" != "cur" && "$sk_maild" != "new" && "$sk_maild" != "tmp" ]]; then
if [ -d "$sk_maild" ]; then
for sk_mail_account in $(ls $sk_maild/)
do
echo "Create and restore mail account: $sk_mail_account@$sk_maild"
sk_mail_pass1=$(gen_password)
v-add-mail-account $sk_cp_user $sk_maild $sk_mail_account $sk_mail_pass1
mv $sk_maild/$sk_mail_account /home/$sk_cp_user/mail/$sk_maild
chown $sk_cp_user:mail -R /home/$sk_cp_user/mail/$sk_maild
echo "$sk_mail_account@$sk_maild | $sk_mail_pass1" >> /root/sk_mail_password_$sk_cp_user-$DATE
done
fi
#else
# this only detect default dirs account new, cur, tmp etc
# maybe can do something whit this, but on most cpanel default account have only spam.
fi
done
echo "All mail accounts restored"
#############
############
# Restore Cron
#
# need some extrafunctions.
sk_run_cron_restore() {
cr=0
grep -v "SHELL=" $sk_importer_in/cron/$sk_real_cp_user |grep -v "mailto" > $sk_importer_in/cron/sk_cron_p
mkdir sk-emptydir
cd sk-emptydir
function sk_restore_cron() {
while IFS= read -r line; do
v-add-cron-job $sk_cp_user $line
((cr++))
done
}
sk_restore_cron < "$sk_importer_in/cron/sk_cron_p"
echo "$cr cron restored"
}
if [ -e $sk_importer_in/cron/$sk_real_cp_user ]; then
tput setaf 2
echo "Cant restore crons yet"
#echo "Restore Crons for $sk_cp_user"
tput sgr0
# restore working, need some function to fix cron when have php comandas like php -q /absolutepath
# so disabled for now, vesta cant add cron from cli if cron has * - bug ?
# sk_run_cron_restore
else
echo " No cron jobs found for user $sk_cp_user"
fi
cd ..
echo "Remove tmp files"
rm -rf /root/$sk_tmp
echo "##############################"
echo "cPanel Backup restored"
echo "Review your content and report any fail"
echo "I reset mail password not posible restore it yet."
echo "Check your new passwords runing: cat /root/sk_mail_password_$sk_cp_user-$DATE "
echo "##############################"