-
Notifications
You must be signed in to change notification settings - Fork 1k
/
v-restore-user
executable file
·812 lines (692 loc) · 25.6 KB
/
v-restore-user
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#!/bin/bash
# info: restore user
# options: USER BACKUP [WEB] [DNS] [MAIL] [DB] [CRON] [UDIR] [NOTIFY]
#
# The function for resotring user from backup.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Import VESTA variable for cron launch
source /etc/profile
# Argument defenition
user=$1
backup=$2
web=$3
dns=$4
mail=$5
db=$6
cron=$7
udir=$8
notify=${9-no}
# Define backup dir
if [ -z "$BACKUP" ]; then
BACKUP=/home/backup/
fi
# Includes
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/ip.sh
source $VESTA/func/db.sh
source $VESTA/func/rebuild.sh
source $VESTA/conf/vesta.conf
# Check backup function
is_backup_valid() {
if [ ! -e "$BACKUP/$backup" ]; then
echo "Error: backup doesn't exist"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
args_usage='USER BACKUP [WEB] [DNS] [MAIL] [DB] [CRON] [UDIR] [NOTIFY]'
check_args '2' "$#" "$args_usage"
validate_format 'user' 'backup'
is_backup_valid
is_backup_available
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Define email wrapper
send_mail="$VESTA/web/inc/mail-wrapper.php"
# Check user
check_user=$(is_object_valid 'user' 'USER' "$user")
if [ -z "$check_user" ]; then
is_object_unsuspended 'user' 'USER' "$user"
if [ "$notify" != 'no' ]; then
subj="$user → restore failed"
email=$(get_user_value '$CONTACT')
else
subj="$user → restore failed"
email=$(grep CONTACT $VESTA/data/users/admin/user.conf |cut -f2 -d \')
fi
else
# Set flag for user creation
create_user="yes"
# Set notification email and subject
subj="$user → restore failed"
email=$(grep CONTACT $VESTA/data/users/admin/user.conf | cut -f2 -d \')
fi
# Check disk usage
disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
rm -rf $tmpdir
echo "Not enough disk space to run backup" | $send_mail -s "$subj" $email
echo "Error: Not enough disk space"
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
log_event "$E_DISK" "$EVENT"
exit $E_DISK
fi
# Check load average
la=$(cat /proc/loadavg | cut -f 1 -d ' ' | cut -f 1 -d '.')
i=0
while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
echo "$(date "+%F %T") Load Average $la"
echo
sleep 60
if [ "$i" -ge "15" ]; then
echo "LoadAverage $i is above threshold" |$send_mail -s "$subj" $email
echo "Error: LA is too high"
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
log_event "$E_LA" "$EVENT"
exit $E_LA
fi
(( ++i))
done
# Creating temporary directory
tmpdir=$(mktemp -p $BACKUP -d)
if [ "$?" -ne 0 ]; then
echo "Can't create tmp dir $tmpdir" | $send_mail -s "$subj" $email
echo "Error: can't create tmp dir"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
# User
if [ "$create_user" = 'yes' ]; then
echo "-- USER --"
msg="$msg\n-- USER --"
echo -e "$(date "+%F %T") $user"
msg="$msg\n$(date "+%F %T") $user"
# unpack user container
tar xf $BACKUP/$backup -C $tmpdir ./vesta
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack user contaner"
echo "Can't unpack user contaner" | $send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Restore cron records
mkdir $USER_DATA
cp $tmpdir/vesta/user.conf $USER_DATA/
# Rebuild cron
rebuild_user_conf
echo
msg="$msg\n"
fi
# WEB
if [ "$web" != 'no' ]; then
echo "-- WEB --"
msg="$msg\n-- WEB --"
# Unpack pam container
tar xf $BACKUP/$backup -C $tmpdir ./pam
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack PAM contaner"
echo "Can't unpack PAM contaner" | $send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Get user id
old_uid=$(cut -f 3 -d : $tmpdir/pam/passwd)
new_uid=$(grep "^$user:" /etc/passwd | cut -f 3 -d :)
# Create domain list
domain_list=$(tar -tf $BACKUP/$backup | grep "^./web" |\
grep domain_data.tar.gz | cut -f 3 -d '/')
if [ ! -z "$web" ]; then
dom_include_list=$(mktemp)
for domain_include in ${web//,/ }; do
echo "^$domain_include$" >> $dom_include_list
done
domain_list=$(echo "$domain_list" | egrep -f $dom_include_list )
rm -f $dom_include_list
fi
for domain in $domain_list; do
echo -e "$(date "+%F %T") $domain"
msg="$msg\n$(date "+%F %T") $domain"
# unpack domain container
tar xf $BACKUP/$backup -C $tmpdir ./web/$domain
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $domain web contaner"
echo "Can't unpack $domain web contaner" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Restore domain config
check_config=$(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
if [ -z "$check_config" ]; then
# Parse domain config
eval $(cat $tmpdir/web/$domain/vesta/web.conf)
# Check if domain new
check_new=$(is_domain_new 'web' $domain)
if [ ! -z "$check_new" ]; then
echo "Error: web domain $domain belongs to another user"
echo "Web domain $domain belongs to another user" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
# Check if domain alias is new
for dom_alias in ${ALIAS//,/ }; do
check_new=$(is_domain_new 'web' $dom_alias)
if [ ! -z "$check_new" ]; then
# Delete conflicting alias
ALIAS=$(echo "$ALIAS" |\
sed "s/,/\n/g"|\
sed "s/^$dom_alias$//g"|\
sed "/^$/d"|\
sed ':a;N;$!ba;s/\n/,/g')
fi
done
# Check ip address
check_ip=$(is_ip_valid $IP)
if [ -z "$check_ip" ]; then
check_ip=$(is_ip_avalable $IP)
fi
if [ ! -z "$check_ip" ]; then
IP=$(get_user_ip $user)
if [ -z "$IP" ]; then
echo "Error: no avaiable IP address"
echo "No available IP address" |\
$send_mail -s "$subj" $email
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
echo "$IP" > $tmpdir/ip_mapping.$domain
fi
# Check web template
check_tpl=$(is_web_template_valid)
if [ ! -e "$WEBTPL/$WEB_SYSTEM/$TPL.tpl" ]; then
TPL="default"
if [ ! -e "$WEBTPL/$WEB_SYSTEM/$TPL.tpl" ]; then
echo "Error: no avaiable web template"
echo "No available web template" |\
$send_mail -s "$subj" $email
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
fi
# Check proxy template
if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
if [ ! -e "$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl" ]; then
PROXY="default"
if [ ! -e "$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl" ]; then
echo "Error: no avaiable proxy template"
echo "No available proxy tpl" |\
$send_mail -s "$subj" $email
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
fi
fi
# Convert ftp user
if [ ! -z "$FTP_USER" ]; then
FTP_USER=$(echo "$FTP_USER" | cut -f 2,3,4,5,6,7 -d '_')
FTP_USER="${user}_${FTP_USER}"
fi
# Convert stats user
if [ ! -z "$STATS_USER" ]; then
STATS_USER=$(echo "$STATS_USER" | cut -f 2,3,4,5,6,7 -d '_')
STATS_USER="${user}_${STATS_USER}"
fi
str="DOMAIN='$domain' IP='$IP' IP6='$IP6' ALIAS='$ALIAS'"
str="$str TPL='$TPL' SSL='$SSL' SSL_HOME='$SSL_HOME'"
str="$str FTP_USER='$FTP_USER' FTP_MD5='$FTP_MD5' PROXY='$PROXY'"
str="$str PROXY_EXT='$PROXY_EXT' STATS='$STATS'"
str="$str STATS_USER='$STATS_USER' STATS_CRYPT='$STATS_CRYPT'"
str="$str U_DISK='$U_DISK' U_BANDWIDTH='0' SUSPENDED='no'"
str="$str TIME='$(date +%T)' DATE='$(date +%F)'"
echo $str >> $USER_DATA/web.conf
# Copy ssl certificate
if [ "$SSL" = 'yes' ]; then
for crt in $(ls $tmpdir/web/$domain/conf |grep ssl); do
crt=$(echo "$crt" |sed "s/ssl.//")
cp -f $tmpdir/web/$domain/conf/ssl.$crt $USER_DATA/ssl/$crt
done
fi
# Rebuild web config
rebuild_web_domain_conf
# Adding vhost
web_conf="/etc/$WEB_SYSTEM/conf.d/vesta.conf"
tmp_conf="$HOMEDIR/$user/conf/web/tmp_$WEB_SYSTEM.conf"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
cat $tmp_conf >> $conf
rm -f $tmp_conf
web_include=$(grep "$conf" $web_conf)
if [ -z "$web_include" ]; then
echo "Include $conf" >> $web_conf
fi
# Adding SSL vhost
if [ "$SSL" = 'yes' ]; then
tmp_conf="$HOMEDIR/$user/conf/web/tmp_s$WEB_SYSTEM.conf"
conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
cat $tmp_conf >> $conf
rm -f $tmp_conf
fi
ssl_include=$(grep "$conf" $web_conf)
if [ -z "$ssl_include" ]; then
echo "Include $conf" >> $web_conf
fi
# Proxy
if [ ! -z "$PROXY_SYSTEM" ]; then
# Adding proxy vhost
proxy_conf="/etc/$PROXY_SYSTEM/conf.d/vesta.conf"
if [ ! -z "$PROXY" ]; then
tmp_conf="$HOMEDIR/$user/conf/web/tmp_$PROXY_SYSTEM.conf"
conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
cat $tmp_conf >> $conf
rm -f $tmp_conf
fi
proxy_include=$(grep "$conf" $proxy_conf)
if [ -z "$proxy_include" ]; then
echo "include $conf;" >> $proxy_conf
fi
# Adding SSL proxy vhost
if [ ! -z "$PROXY" ] && [ "$SSL" = 'yes' ]; then
tmp_conf="$HOMEDIR/$user/conf/web/tmp_s$PROXY_SYSTEM.conf"
conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
cat $tmp_conf >> $conf
rm -f $tmp_conf
proxy_include=$(grep "$conf" $proxy_conf)
if [ -z "$proxy_include" ]; then
echo "include $conf;" >> $proxy_conf
fi
fi
fi
fi
# Restore data
tar -xzpf $tmpdir/web/$domain/domain_data.tar.gz \
-C $HOMEDIR/$user/web/$domain/
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $domain data tarball"
echo "Can't can't unpack $domain data tarball" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# ReChown files if uid differs
if [ "$old_uid" -ne "$new_uid" ]; then
find $HOMEDIR/$user/web/$domain/ -user $old_uid \
-exec chown -h $user:$user {} \;
fi
done
# Restart WEB
$BIN/v-restart-web
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
$BIN/v-restart-proxy
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
echo
msg="$msg\n"
fi
# DNS
if [ "$dns" != 'no' ]; then
echo "-- DNS --"
msg="$msg\n-- DNS --"
# Create domain list
domain_list=$(tar -tf $BACKUP/$backup | grep "^./dns" |\
grep dns.conf | cut -f 3 -d '/')
if [ ! -z "$dns" ]; then
dom_include_list=$(mktemp)
for domain_include in ${dns//,/ }; do
echo "^$domain_include$" >> $dom_include_list
done
domain_list=$(echo "$domain_list" | egrep -f $dom_include_list )
rm -f $dom_include_list
fi
for domain in $domain_list; do
echo -e "$(date "+%F %T") $domain"
msg="$msg\n$(date "+%F %T") $domain"
# unpack domain container
tar xf $BACKUP/$backup -C $tmpdir ./dns/$domain
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $domain dns contaner"
echo "Can't unpack $domain dns contaner" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Restore domain config
check_config=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
if [ -z "$check_config" ]; then
# Parse domain config
eval $(cat $tmpdir/dns/$domain/vesta/dns.conf)
# Check if domain new
check_new=$(is_domain_new 'dns' $domain)
if [ ! -z "$check_new" ]; then
echo "Error: dns domain $domain belongs to another user"
echo "DNS domain $domain belongs to another user" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
# Check ip address
if [ -e "$tmpdir/ip_mapping.$domain" ]; then
OLD=$IP
IP=$(cat $tmpdir/ip_mapping.$domain)
sed -i "s/$OLD/$IP/g" $tmpdir/dns/$domain/vesta/$domain.conf
else
check_ip=$(is_ip_valid $IP)
if [ ! -z "$check_ip" ]; then
if [ -z "$IP" ]; then
IP=$(get_user_ip $user)
fi
fi
fi
if [ -z "$IP" ]; then
echo "Error: no avaiable IP address"
echo "No available IP address" | $send_mail -s "$subj" $email
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
# Check dns template
check_tpl=$(is_dns_template_valid)
if [ ! -z "$check_tpl" ]; then
templates=$(ls -t $VESTA/data/templates/dns/ |\
grep '\.tpl' |\
cut -f 1 -d '.')
if [ ! -z "$(echo $templates |grep default)" ]; then
TPL=$(echo "$templates" |grep default |head -n1)
else
TPL=$("$templates" |head -n1)
fi
if [ -z "$TPL" ]; then
echo "Error: no avaiable dns template"
echo "No available dns template" |\
$send_mail -s "$subj" $email
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
fi
str="DOMAIN='$domain' IP='$IP' TPL='$TPL' TTL='$TTL' EXP='$EXP'"
str="$str SOA='$SOA' RECORDS='$RECORDS' SUSPENDED='no'"
str="$str TIME='$(date +%T)' DATE='$(date +%F)'"
echo $str >> $USER_DATA/dns.conf
fi
# Restore dns records
cp -f $tmpdir/dns/$domain/vesta/$domain.conf $USER_DATA/dns/
# Rebuild dns config
rebuild_dns_domain_conf
done
# Restart DNS
$BIN/v-restart-dns
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
echo
msg="$msg\n"
fi
# MAIL
if [ "$mail" != 'no' ]; then
echo "-- MAIL --"
msg="$msg\n-- MAIL --"
# Unpack pam container
tar xf $BACKUP/$backup -C $tmpdir ./pam
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack PAM contaner"
echo "Can't unpack PAM contaner" | $send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Get user id
old_uid=$(cut -f 3 -d : $tmpdir/pam/passwd)
new_uid=$(grep "^$user:" /etc/passwd | cut -f 3 -d :)
# Create domain list
domain_list=$(tar -tf $BACKUP/$backup | grep "^./mail" |\
grep mail.conf | cut -f 3 -d '/')
if [ ! -z "$mail" ]; then
dom_include_list=$(mktemp)
for domain_include in ${mail//,/ }; do
echo "^$domain_include$" >> $dom_include_list
done
domain_list=$(echo "$domain_list" | egrep -f $dom_include_list )
rm -f $dom_include_list
fi
for domain in $domain_list; do
echo -e "$(date "+%F %T") $domain"
msg="$msg\n$(date "+%F %T") $domain"
# unpack domain container
tar xf $BACKUP/$backup -C $tmpdir ./mail/$domain
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $domain mail contaner"
echo "Can't can't unpack $domain mail contaner" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Restore domain config
domain_idn=$(idn -t --quiet -a "$domain")
check_config=$(grep "DOMAIN='$domain'" $USER_DATA/mail.conf)
if [ -z "$check_config" ]; then
# Parse domain config
eval $(cat $tmpdir/mail/$domain/vesta/mail.conf)
# Check if domain new
check_new=$(is_domain_new 'mail' $domain)
if [ ! -z "$check_new" ]; then
echo "Error: mail domain $domain belongs to another user"
echo "Mail domain $domain belongs to another user" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi
str="DOMAIN='$domain' ANTIVIRUS='$ANTIVIRUS' ANTISPAM='$ANTISPAM'"
str="$str DKIM='$DKIM' ACCOUNTS='$ACCOUNTS' U_DISK='$U_DISK'"
str="$str CATCHALL='$CATCHALL' SUSPENDED='no'"
str="$str TIME='$(date +%T)' DATE='$(date +%F)'"
echo $str >> $USER_DATA/mail.conf
fi
# Restore DKIM
if [ -e "$tmpdir/mail/$domain/vesta/$domain.pem" ]; then
cp -f $tmpdir/mail/$domain/vesta/$domain.pem $USER_DATA/mail/
cp -f $tmpdir/mail/$domain/vesta/$domain.pub $USER_DATA/mail/
fi
# Restore email accounts
cp -f $tmpdir/mail/$domain/vesta/$domain.conf $USER_DATA/mail/
# Rebuild mail config
rebuild_mail_domain_conf
# Restore emails
if [ -e "$tmpdir/mail/$domain/accounts.tar.gz" ]; then
tar -xzpf $tmpdir/mail/$domain/accounts.tar.gz \
-C $HOMEDIR/$user/mail/$domain_idn/
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $domain mail account tarball"
echo "Can't unpack $domain mail account tarball" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# ReChown files if uid differs
if [ "$old_uid" -ne "$new_uid" ]; then
find $HOMEDIR/$user/mail/$domain_idn -user $old_uid \
-exec chown -h $user:mail {} \;
fi
fi
done
echo
msg="$msg\n"
fi
# DB
if [ "$db" != 'no' ]; then
echo "-- DB --"
msg="$msg\n-- DB --"
# Create domain list
db_list=$(tar -tf $BACKUP/$backup | grep "^./db" |\
grep db.conf | cut -f 3 -d '/')
if [ ! -z "$db" ]; then
db_include_list=$(mktemp)
for db_include in ${db//,/ }; do
echo "^$db_include$" >> $db_include_list
done
db_list=$(echo "$db_list" | egrep -f $db_include_list )
rm -f $db_include_list
fi
for db in $db_list; do
# unpack db container
tar xf $BACKUP/$backup -C $tmpdir ./db/$db
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $db database contaner"
echo "Can't unpack $db database contaner" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Restore domain config
check_config=$(grep "DB='$db'" $USER_DATA/db.conf)
if [ -z "$check_config" ]; then
# Parse database config
eval $(cat $tmpdir/db/$db/vesta/db.conf)
# Convert database & database user
DB=$(echo "$DB" | cut -f 2,3,4,5,6,7 -d '_')
DB="${user}_${DB}"
DBUSER=$(echo "$DBUSER" | cut -f 2,3,4,5,6,7 -d '_')
DBUSER="${user}_${DBUSER}"
str="DB='$DB' DBUSER='$DBUSER' MD5='$MD5' HOST='$HOST'"
str="$str TYPE='$TYPE' CHARSET='$CHARSET' U_DISK='$U_DISK'"
str="$str SUSPENDED='no' TIME='$(date +%T)' DATE='$(date +%F)'"
echo $str >> $USER_DATA/db.conf
else
eval $(grep "DB='$db'" $USER_DATA/db.conf)
fi
echo -e "$(date "+%F %T") $DB"
msg="$msg\n$(date "+%F %T") $DB"
# Unzip database dump
gzip -d $tmpdir/db/$db/$db.*.sql.gz
# Get database values
get_database_values
# Rebuild db
case $TYPE in
mysql) rebuild_mysql_database;
import_mysql_database $tmpdir/db/$db/$db.$TYPE.sql ;;
pgsql) rebuild_pgsql_database;
import_pgsql_database $tmpdir/db/$db/$db.$TYPE.sql ;;
esac
done
echo
msg="$msg\n"
fi
# Cron
if [ "$cron" != 'no' ]; then
echo "-- CRON --"
msg="$msg\n-- CRON --"
# unpack cron container
tar xf $BACKUP/$backup -C $tmpdir ./cron
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack cron contaner"
echo "Can't unpack cron contaner" | $send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
cron_record=$(wc -l $tmpdir/cron/cron.conf |cut -f 1 -d' ')
if [ "$cron_record" -eq 1 ]; then
echo -e "$(date "+%F %T") $cron_record record"
msg="$msg\n$(date "+%F %T") $cron_record record"
else
echo -e "$(date "+%F %T") $cron_record records"
msg="$msg\n$(date "+%F %T") $cron_record records"
fi
# Restore cron records
cp $tmpdir/cron/cron.conf $USER_DATA/cron.conf
# Rebuild cron
sync_cron_jobs
# Restart cron
$BIN/v-restart-cron
if [ $? -ne 0 ]; then
exit $E_RESTART
fi
echo
msg="$msg\n"
fi
# Restore user directories
if [ "$udir" != 'no' ]; then
echo "-- USER DIR --"
msg="$msg\n-- USER DIR --"
# unpack user dir container
if [ ! -z "$(tar -tf $BACKUP/$backup |grep './user_dir')" ]; then
# Unpack pam container
tar xf $BACKUP/$backup -C $tmpdir ./pam
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack PAM contaner"
echo "Can't unpack PAM contaner" | $send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# Get user id
old_uid=$(cut -f 3 -d : $tmpdir/pam/passwd)
new_uid=$(grep "^$user:" /etc/passwd | cut -f 3 -d :)
# Create user dir list
udir_list=$(tar -tf $BACKUP/$backup | grep "^./user_dir" |\
grep tar.gz | cut -f 3 -d '/' | sed "s/.tar.gz//")
if [ ! -z "$udir" ]; then
udir_include_list=$(mktemp)
for udir_include in ${udir//,/ }; do
echo "^$udir_include$" >> $udir_include_list
done
udir_list=$(echo "$udir_list" | egrep -f $udir_include_list )
rm -f $udir_include_list
fi
for user_dir in $udir_list; do
echo -e "$(date "+%F %T") $user_dir"
msg="$msg\n$(date "+%F %T") $user_dir"
# unpack user_dir container
tar xf $BACKUP/$backup -C $tmpdir ./user_dir/$user_dir.tar.gz
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $user_dir user dir contaner"
echo "Can't unpack $user_dir user dir contaner" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
tar xzf $tmpdir/user_dir/$user_dir.tar.gz -C $HOMEDIR/$user
if [ "$?" -ne 0 ]; then
echo "Error: can't unpack $user_dir user dir contaner"
echo "Can't unpack $user_dir user dir contaner" |\
$send_mail -s "$subj" $email
rm -rf $tmpdir
exit $E_PARSING
fi
# ReChown files if uid differs
if [ "$old_uid" -ne "$new_uid" ]; then
find $HOMEDIR/$user/$user_dir -user $old_uid \
-exec chown -h $user:$user {} \;
fi
done
fi
echo
msg="$msg\n"
fi
# Remove temporary data
rm -rf $tmpdir
# Clean restore queue
sed -i "/v-restore-user $user /d" $VESTA/data/queue/backup.pipe
# Send notification
if [ "$notify" != 'no' ]; then
subj="$user → restore has been completed"
email=$(get_user_value '$CONTACT')
echo -e "$msg" | $send_mail -s "$subj" $email
fi
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Update user counters
$BIN/v-update-user-counters $user
$BIN/v-update-user-counters admin
$BIN/v-update-sys-ip-counters
# Logging
log_event "$OK" "$EVENT"
exit