forked from outroll/vesta
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathv-backup-user
executable file
·1028 lines (888 loc) · 32.5 KB
/
v-backup-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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# info: backup system user with all its objects
# options: USER NOTIFY
#
# The call is used for backing up user with all its domains and databases.
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Importing system variables
source /etc/profile
# Argument definition
user=$1
notify=${2-no}
# Includes
source $VESTA/func/main.sh
source $VESTA/func/domain.sh
source $VESTA/func/db.sh
source $VESTA/conf/vesta.conf
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '1' "$#" 'USER [NOTIFY]'
is_format_valid 'user'
is_system_enabled "$BACKUP_SYSTEM" 'BACKUP_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_backup_enabled
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
wait_for_backup_if_it_is_not_time_for_backup
# Override backup path
if [ ! -z "$OVERRIDE_BACKUP_PATH" ]; then
BACKUP=$OVERRIDE_BACKUP_PATH
fi
# Set backup directory if undefined
if [ -z "$BACKUP" ]; then
BACKUP=/backup
fi
mkdir -p $BACKUP
# Get current time
start_time=$(date '+%s')
# Set notification email and subject
subj="$user → backup failed"
email=$(grep CONTACT $VESTA/data/users/admin/user.conf |cut -f 2 -d \')
# Validate available disk space (take usage * 2, due to the backup handling)
let u_account=$(grep "U_DISK=" $VESTA/data/users/$user/user.conf |cut -f 2 -d \')
let u_disk=$(grep "U_DISK=" $VESTA/data/users/$user/user.conf |cut -f 2 -d \')*2
let v_disk=$(($(stat -f --format="%a*%S" $BACKUP)))/1024/1024
if [ "$u_disk" -gt "$v_disk" ]; then
echo "account size : $u_account megabytes" |tee $BACKUP/$user.log
echo "available space on disk: $v_disk megabytes" |tee $BACKUP/$user.log
echo "needed space on disk : $u_disk megabytes" |tee $BACKUP/$user.log
echo "not enough disk space available to perform the backup." |$SENDMAIL -s "$subj" $email $notify
check_result $E_LIMIT "not enough disk space available to perform the backup."
fi
if [ -z "$BACKUP_TEMP" ]; then
BACKUP_TEMP=$BACKUP
fi
# Creating temporary directory
tmpdir=$(mktemp -p $BACKUP_TEMP -d)
if [ "$?" -ne 0 ]; then
echo "Can't create tmp dir $tmpdir" |$SENDMAIL -s "$subj" $email $notify
check_result $E_NOTEXIST "can't create tmp dir"
fi
# Backup sys configs
echo "-- SYSTEM --" |tee $BACKUP/$user.log
mkdir $tmpdir/vesta
echo -e "$(date "+%F %T") $user.conf" |tee -a $BACKUP/$user.log
cp -r $USER_DATA/user.conf $tmpdir/vesta/
cp -r $USER_DATA/ssl $tmpdir/vesta/
if [ -e "$USER_DATA/stats.log" ]; then
echo -e "$(date "+%F %T") stats.log" |tee -a $BACKUP/$user.log
cp -r $USER_DATA/stats.log $tmpdir/vesta/
fi
if [ -e "$USER_DATA/history.log" ]; then
echo -e "$(date "+%F %T") history.log" |tee -a $BACKUP/$user.log
cp -r $USER_DATA/history.log $tmpdir/vesta/
fi
if [ -e "$USER_DATA/backup-excludes.conf" ]; then
echo -e "$(date "+%F %T") backup-excludes.conf" |tee -a $BACKUP/$user.log
cp -r $USER_DATA/backup-excludes.conf $tmpdir/vesta/
fi
# Backup PAM
mkdir $tmpdir/pam
echo -e "$(date "+%F %T") pam" |tee -a $BACKUP/$user.log
grep "^$user:" /etc/passwd > $tmpdir/pam/passwd
grep "^$user:" /etc/shadow > $tmpdir/pam/shadow
grep "^$user:" /etc/group > $tmpdir/pam/group
echo
# Parsing excludes
if [ -e "$USER_DATA/backup-excludes.conf" ]; then
source $USER_DATA/backup-excludes.conf
fi
# WEB domains
if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB" != '*' ]; then
echo -e "\n-- WEB --" |tee -a $BACKUP/$user.log
mkdir $tmpdir/web/
# Parsing domain exclusions
conf="$USER_DATA/web.conf"
for domain in $(search_objects 'web' 'SUSPENDED' "*" 'DOMAIN'); do
exclusion=$(echo -e "$WEB" |tr ',' '\n' |grep "^$domain$")
if [ -z "$exclusion" ]; then
web_list="$web_list $domain"
else
echo "$(date "+%F %T") excluding $domain"|tee -a $BACKUP/$user.log
fi
done
web_list=$(echo "$web_list" |sed -e "s/ */\ /g" -e "s/^ //")
i=0
for domain in $web_list; do
wait_for_backup_if_it_is_not_time_for_backup
((i ++))
echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
mkdir -p $tmpdir/web/$domain/conf
mkdir -p $tmpdir/web/$domain/vesta
# Get domain variables
domain_idn=$domain
format_domain_idn
get_domain_values 'web'
# backuping php-fpm conf file
if [[ $TPL == "PHP-FPM-"* ]]; then
fpm_tpl_ver=${TPL:8:2}
fpm_ver="${TPL:8:1}.${TPL:9:1}"
fpm_folder="$fpm_ver/fpm/pool.d"
fpm_path="$fpm_ver/fpm/pool.d/$domain.conf"
if [[ $TPL == *"-ioncube" ]]; then
fpm_folder="$fpm_ver/fpm/pool.d-ioncube"
fpm_path="$fpm_ver/fpm/pool.d-ioncube/$domain.conf"
fi
fpm_original_path="/etc/php/$fpm_path"
fpm_dest_path="$tmpdir/web/$domain/php/$fpm_path"
fpm_dest_folder="$tmpdir/web/$domain/php/$fpm_folder"
if [ -f "$fpm_original_path" ]; then
mkdir -p $fpm_dest_folder
cp $fpm_original_path $fpm_dest_path
fi
fi
# Backup web.conf
cd $tmpdir/web/$domain/
conf="$USER_DATA/web.conf"
grep "DOMAIN='$domain'" $conf > vesta/web.conf
# Backup vhost config
conf=$HOMEDIR/$user/conf/web/$domain.$WEB_SYSTEM.conf
if [ -e "$conf" ]; then
cp $conf conf/$WEB_SYSTEM.conf
else
# old style configs
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
get_web_config_lines $tpl_file $conf
sed -n "$top_line,$bottom_line p" $conf > conf/$WEB_SYSTEM.conf
fi
# Backup ssl vhost
if [ "$SSL" = 'yes' ]; then
conf=$HOMEDIR/$user/conf/web/$domain.$WEB_SYSTEM.ssl.conf
if [ -e "$conf" ]; then
cp $conf conf/$WEB_SYSTEM.ssl.conf
else
tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
get_web_config_lines $tpl_file $conf
sed -n "$top_line,$bottom_line p" $conf > \
conf/s$WEB_SYSTEM.conf
fi
fi
# Backup proxy config
if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
conf=$HOMEDIR/$user/conf/web/$domain.$PROXY_SYSTEM.conf
if [ -e "$conf" ]; then
cp $conf conf/$PROXY_SYSTEM.conf
else
tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl"
conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
get_web_config_lines $tpl_file $conf
sed -n "$top_line,$bottom_line p" $conf > \
conf/$PROXY_SYSTEM.conf
fi
fi
# Backup ssl proxy config
if [ ! -z "$PROXY_SYSTEM" ] && [ "$SSL" = 'yes' ]; then
conf=$HOMEDIR/$user/conf/web/$domain.$PROXY_SYSTEM.ssl.conf
if [ -e "$conf" ]; then
cp $conf conf/$PROXY_SYSTEM.ssl.conf
else
tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
get_web_config_lines $tpl_file $conf
sed -n "$top_line,$bottom_line p" $conf >\
conf/s$PROXY_SYSTEM.conf
fi
fi
# Backup custom config / backup LE config
for sconfig in $(ls $HOMEDIR/$user/conf/web/|grep ".$domain.conf"); do
cp $HOMEDIR/$user/conf/web/$sconfig conf/
done
# Backup ssl certificates
if [ "$SSL" = 'yes' ] ; then
cp $HOMEDIR/$user/conf/web/ssl.$domain.* conf/
cp $USER_DATA/ssl/$domain.* vesta/
fi
# Changin dir to documentroot
cd $HOMEDIR/$user/web/$domain
# Define exclude arguments
exlusion=$(echo -e "$WEB" |tr ',' '\n' |grep "^$domain:")
set -f
fargs=()
fargs+=(--exclude='./logs/*')
if [ ! -z "$exlusion" ]; then
xdirs="$(echo -e "$exlusion" |tr ':' '\n' |grep -v $domain)"
for xpath in $xdirs; do
# Add ./ at the beginning of the path if the path is in old pattern
if [[ $xpath != ./* ]]; then
xpath=(./$xpath)
fi
if [ -d "$xpath" ]; then
fargs+=(--exclude=$xpath/*)
echo "$(date "+%F %T") excluding directory $xpath"
msg="$msg\n$(date "+%F %T") excluding directory $xpath"
else
echo "$(date "+%F %T") excluding file $xpath"
msg="$msg\n$(date "+%F %T") excluding file $xpath"
fargs+=(--exclude=$xpath)
fi
done
fi
set +f
# Backup files
tar --anchored -cpf- ${fargs[@]} --exclude={'./','../'} . |gzip -$BACKUP_GZIP - > $tmpdir/web/$domain/domain_data.tar.gz
done
# Print total
if [ "$i" -eq 1 ]; then
echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
else
echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
fi
fi
# DNS domains
if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS" != '*' ]; then
echo -e "\n-- DNS --" |tee -a $BACKUP/$user.log
mkdir $tmpdir/dns/
# Parsing domain exclusions
for domain in $(search_objects 'dns' 'SUSPENDED' "*" 'DOMAIN'); do
exclusion=$(echo "$DNS" |tr ',' '\n' |grep "^$domain$")
if [ -z "$exclusion" ]; then
dns_list="$dns_list $domain"
else
echo "$(date "+%F %T") excluding $domain"
msg="$msg\n$(date "+%F %T") excluding $domain"
fi
done
dns_list=$(echo "$dns_list" |sed -e "s/ */\ /g" -e "s/^ //")
i=0
for domain in $dns_list; do
((i ++))
echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
# Building directory tree
mkdir -p $tmpdir/dns/$domain/conf
mkdir -p $tmpdir/dns/$domain/vesta
# Backup dns.conf
cd $tmpdir/dns/$domain/
conf="$USER_DATA/dns.conf"
grep "DOMAIN='$domain'" $conf > vesta/dns.conf
# Backup dns recods
cp $USER_DATA/dns/$domain.conf vesta/$domain.conf
if [ "$DNS_SYSTEM" != 'remote' ]; then
cp $HOMEDIR/$user/conf/dns/$domain.db conf/$domain.db
fi
done
# Print total
if [ "$i" -eq 1 ]; then
echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
else
echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
fi
fi
# Mail domains
if [ ! -z "$MAIL_SYSTEM" ] && [ "$MAIL" != '*' ]; then
echo -e "\n-- MAIL --" |tee -a $BACKUP/$user.log
mkdir $tmpdir/mail/
# Parsing domain exclusions
conf="$USER_DATA/mail.conf"
for domain in $(search_objects 'mail' 'SUSPENDED' "*" 'DOMAIN'); do
check_exl=$(echo "$MAIL" |tr ',' '\n' |grep "^$domain$")
if [ -z "$check_exl" ]; then
mail_list="$mail_list $domain"
else
echo "$(date "+%F %T") excluding $domain"|tee -a $BACKUP/$user.log
fi
done
mail_list=$(echo "$mail_list" |sed -e "s/ */\ /g" -e "s/^ //")
i=0
for domain in $mail_list; do
wait_for_backup_if_it_is_not_time_for_backup
((i ++))
echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
mkdir -p $tmpdir/mail/$domain/conf
mkdir -p $tmpdir/mail/$domain/vesta
domain_idn=$domain
format_domain_idn
# Backup exim config
if [[ "$MAIL_SYSTEM" =~ exim ]]; then
cd $tmpdir/mail/$domain/
cp $HOMEDIR/$user/conf/mail/$domain/* conf/
fi
# Backup mail.conf
conf="$USER_DATA/mail.conf"
grep "DOMAIN='$domain'" $conf > vesta/mail.conf
cp $USER_DATA/mail/$domain.* vesta/
if [ ! -z "$(ls $USER_DATA/mail/|grep *@$domain)" ]; then
cp $USER_DATA/mail/*@$domain.* vesta/
fi
# Backup emails
cd $HOMEDIR/$user/mail/$domain_idn
accounts=()
for account in $(ls); do
exclusion=$(echo "$MAIL" |tr ',' '\n' |grep "$domain:")
exclusion=$(echo "$exclusion" |tr ':' '\n' |grep "^$account$")
# Checking exlusions
if [ -z "$exclusion" ] && [[ "$MAIL_SYSTEM" =~ exim ]]; then
accounts+=($account)
else
echo "$(date "+%F %T") excluding mail account $account" |\
tee -a $BACKUP/$user.log
fi
done
# Compress archive
if [ ${#accounts[@]} -gt 0 ]; then
tar -cpf- ${accounts[@]} |gzip -$BACKUP_GZIP - > $tmpdir/mail/$domain/accounts.tar.gz
fi
done
# Print total
if [ "$i" -eq 1 ]; then
echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
else
echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
fi
fi
# Databases
if [ ! -z "$DB_SYSTEM" ] && [ "$DB" != '*' ]; then
echo -e "\n-- DB --" |tee -a $BACKUP/$user.log
mkdir $tmpdir/db/
# Parsing database exclusions
for database in $(search_objects 'db' 'SUSPENDED' "*" 'DB'); do
exclusion=$(echo "$DB" |tr ',' '\n' |grep "^$database$")
if [ -z "$exclusion" ]; then
db_list="$db_list $database"
else
echo "$(date "+%F %T") excluding $database" |\
tee -a $BACKUP/$user.log
fi
done
i=0
conf="$USER_DATA/db.conf"
db_list=$(echo "$db_list" |sed -e "s/ */\ /g" -e "s/^ //")
for database in $db_list; do
wait_for_backup_if_it_is_not_time_for_backup
((i ++))
get_database_values
echo -e "$(date "+%F %T") $database ($TYPE)" |tee -a $BACKUP/$user.log
mkdir -p $tmpdir/db/$database/conf
mkdir -p $tmpdir/db/$database/vesta
cd $tmpdir/db/$database/
grep "DB='$database'" $conf > vesta/db.conf
dump="$tmpdir/db/$database/$database.$TYPE.sql"
dumpgz="$tmpdir/db/$database/$database.$TYPE.sql.gz"
grants="$tmpdir/db/$database/conf/$database.$TYPE.$DBUSER"
if [ ! -f "$dumpgz" ]; then
WAIT_LOOP_ENTERED=0
while true
do
if pgrep -x "mysqldump" > /dev/null
then
WAIT_LOOP_ENTERED=1
echo "Wait other mysqldump to finish"
sleep 1
else
if [ "$WAIT_LOOP_ENTERED" -eq 1 ]; then
echo "We can use mysqldump now"
fi
break
fi
done
case $TYPE in
mysql) dump_mysql_database ;;
pgsql) dump_pgsql_database ;;
esac
# Compress dump
gzip -$BACKUP_GZIP $dump
fi
done
# Print total
if [ "$i" -eq 1 ]; then
echo -e "$(date "+%F %T") *** $i database ***" |\
tee -a $BACKUP/$user.log
else
echo -e "$(date "+%F %T") *** $i databases ***"|\
tee -a $BACKUP/$user.log
fi
fi
# Cron jobs
if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON" != '*' ]; then
echo -e "\n-- CRON --" |tee -a $BACKUP/$user.log
mkdir $tmpdir/cron/
# Backup cron.conf
cp $USER_DATA/cron.conf $tmpdir/cron/
cron_record=$(wc -l $USER_DATA/cron.conf|cut -f 1 -d ' ')
if [ -e "/var/spool/cron/$user" ]; then
cron_list="$cron_record"
cp /var/spool/cron/$user $tmpdir/cron/
fi
# Print total
if [ "$cron_record" -eq 1 ]; then
echo -e "$(date "+%F %T") *** $cron_record job ***" |\
tee -a $BACKUP/$user.log
else
echo -e "$(date "+%F %T") *** $cron_record jobs ***" |\
tee -a $BACKUP/$user.log
fi
fi
# User Directories
if [ "$USER" != '*' ]; then
echo -e "\n-- User Dir --" |tee -a $BACKUP/$user.log
mkdir $tmpdir/user_dir
cd $HOMEDIR/$user
# Parsing directory exlusions
USER=''
if [ -e "$USER_DATA/backup-excludes.conf" ]; then
source $USER_DATA/backup-excludes.conf
fi
fargs=()
for xpath in $(echo "$USER" |tr ',' '\n'); do
if [ -d "$xpath" ]; then
fargs+=(--exclude=$xpath/*)
echo "$(date "+%F %T") excluding directory $xpath" |\
tee -a $BACKUP/$user.log
else
echo "$(date "+%F %T") excluding file $xpath" |\
tee -a $BACKUP/$user.log
fargs+=(--exclude=$xpath)
fi
done
IFS=$'\n'
set -f
i=0
for udir in $(ls -a |egrep -v "^conf$|^web$|^dns$|^tmp$|^mail$|^\.\.$|^\.$"); do
exclusion=$(echo "$USER" |tr ',' '\n' |grep "^$udir$")
if [ -z "$exclusion" ]; then
((i ++))
udir_str=$(echo "$udir" |sed -e "s|'|\\\'|g")
udir_list="$udir_list $udir_str"
echo -e "$(date "+%F %T") adding $udir" |tee -a $BACKUP/$user.log
wait_for_backup_if_it_is_not_time_for_backup
# Backup files and dirs
tar --anchored -cpf- ${fargs[@]} $udir |gzip -$BACKUP_GZIP - > $tmpdir/user_dir/$udir.tar.gz
fi
done
set +f
udir_list=$(echo "$udir_list" |sed -e "s/ */\ /g" -e "s/^ //")
# Print total
if [ "$i" -eq 1 ]; then
echo -e "$(date "+%F %T") *** $i user directory ***" |\
tee -a $BACKUP/$user.log
else
echo -e "$(date "+%F %T") *** $i directories ***" |\
tee -a $BACKUP/$user.log
fi
fi
# Get backup size
size="$(du -shm $tmpdir |cut -f 1)"
# Get current time
end_time=$(date '+%s')
time_n_date=$(date +'%T %F')
time=$(echo "$time_n_date" |cut -f 1 -d \ )
date=$(echo "$time_n_date" |cut -f 2 -d \ )
backup_new_date=$(date +"%Y-%m-%d_%H-%M-%S")
# Defining local storage function
local_backup(){
rm -f $BACKUP/$user.$backup_new_date.tar
# Checking retention
backup_list=$(ls -lrt $BACKUP/ |awk '{print $9}' |grep "^$user\." | grep ".tar")
backups_count=$(echo "$backup_list" |wc -l)
if [ "$BACKUPS" -le "$backups_count" ]; then
backups_rm_number=$((backups_count - BACKUPS + 1))
# Removing old backup
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
echo -e "$(date "+%F %T") Rotated: $backup_date" |\
tee -a $BACKUP/$user.log
rm -f $BACKUP/$backup
done
fi
# Checking disk space
disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
rm -rf $tmpdir
rm -f $BACKUP/$user.log
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
echo "Not enough disk space" |$SENDMAIL -s "$subj" $email $notify
check_result "$E_DISK" "Not enough dsk space"
fi
# Creating final tarball
cd $tmpdir
tar -cf $BACKUP/$user.$backup_new_date.tar .
chmod 640 $BACKUP/$user.$backup_new_date.tar
chown admin:$user $BACKUP/$user.$backup_new_date.tar
localbackup='yes'
echo -e "$(date "+%F %T") Local: $BACKUP/$user.$backup_new_date.tar" |\
tee -a $BACKUP/$user.log
}
# Defining ftp command function
ftpc() {
/usr/bin/ftp -np $HOST $PORT <<EOF
quote USER $USERNAME
quote PASS $PASSWORD
binary
$1
$2
$3
quit
EOF
}
# Defining ftp storage function
ftp_backup() {
# Checking config
if [ ! -e "$VESTA/conf/ftp.backup.conf" ]; then
error="ftp.backup.conf doesn't exist"
rm -rf $tmpdir
rm -f $BACKUP/$user.log
echo "$error" |$SENDMAIL -s "$subj" $email $notify
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
check_result "$E_NOTEXIST" "$error"
fi
# Parse config
source $VESTA/conf/ftp.backup.conf
# Set default port
if [ -z "$(grep 'PORT=' $VESTA/conf/ftp.backup.conf)" ]; then
PORT='21'
fi
# Checking variables
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
error="Can't parse ftp backup configuration"
rm -rf $tmpdir
rm -f $BACKUP/$user.log
echo "$error" |$SENDMAIL -s "$subj" $email $notify
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
check_result "$E_PARSING" "$error"
fi
# Debug info
echo -e "$(date "+%F %T") Remote: ftp://$HOST/$BPATH/$user.$backup_new_date.tar"
# Checking ftp connection
fconn=$(ftpc)
ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
if [ ! -z "$ferror" ]; then
error="Error: can't login to ftp ftp://$USERNAME@$HOST"
rm -rf $tmpdir
rm -f $BACKUP/$user.log
echo "$error" |$SENDMAIL -s "$subj" $email $notify
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
check_result "$E_CONNECT" "$error"
fi
# Check ftp permissions
if [ -z $BPATH ]; then
ftmpdir="vst.bK76A9SUkt"
else
ftpc "mkdir $BPATH" > /dev/null 2>&1
ftmpdir="$BPATH/vst.bK76A9SUkt"
fi
ftpc "mkdir $ftmpdir" "rm $ftmpdir"
ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir" |grep -v Trying)
if [ ! -z "$ftp_result" ] ; then
error="Can't create ftp backup folder ftp://$HOST$BPATH"
rm -rf $tmpdir
rm -f $BACKUP/$user.log
echo "$error" |$SENDMAIL -s "$subj" $email $notify
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
check_result "$E_FTP" "$error"
fi
# Checking retention
if [ -z $BPATH ]; then
backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
else
backup_list=$(ftpc "cd $BPATH" "ls" |awk '{print $9}' |grep "^$user\.")
fi
if [ ! -z "$ONLY_ONE_FTP_BACKUP" ]; then
TEMP_BACKUPS=$BACKUPS
BACKUPS=1
echo "=== Set BACKUPS=1"
fi
if [ ! -z "$KEEP_N_FTP_BACKUPS" ]; then
TEMP_BACKUPS=$BACKUPS
BACKUPS=$KEEP_N_FTP_BACKUPS
echo "=== Set BACKUPS=$KEEP_N_FTP_BACKUPS"
fi
backups_count=$(echo "$backup_list" |wc -l)
if [ "$backups_count" -ge "$BACKUPS" ]; then
backups_rm_number=$((backups_count - BACKUPS + 1))
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
echo -e "$(date "+%F %T") Rotated ftp backup: $backup_date" |\
tee -a $BACKUP/$user.log
if [ -z $BPATH ]; then
ftpc "delete $backup"
else
ftpc "cd $BPATH" "delete $backup"
fi
done
fi
if [ ! -z "$ONLY_ONE_FTP_BACKUP" ]; then
BACKUPS=$TEMP_BACKUPS
echo "=== Bringing back old value BACKUPS=$BACKUPS"
fi
if [ ! -z "$KEEP_N_FTP_BACKUPS" ]; then
BACKUPS=$TEMP_BACKUPS
echo "=== Bringing back old value BACKUPS=$BACKUPS"
fi
# Uploading backup archive
if [ "$localbackup" = 'yes' ]; then
cd $BACKUP
if [ -z $BPATH ]; then
ftpc "put $user.$backup_new_date.tar"
else
ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
fi
else
cd $tmpdir
tar -cf $BACKUP/$user.$backup_new_date.tar .
cd $BACKUP/
if [ -z $BPATH ]; then
ftpc "put $user.$backup_new_date.tar"
else
ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
fi
rm -f $user.$backup_new_date.tar
fi
}
# sftp command function
sftpc() {
expect -f "-" <<EOF "$@"
set timeout 60
set count 0
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
-o Port=$PORT $USERNAME@$HOST
expect {
"password:" {
send "$PASSWORD\r"
exp_continue
}
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
set count \$argc
set output "Disconnected."
set rc $E_FTP
exp_continue
}
-re ".*denied.*(publickey|password)." {
set output "Permission denied, wrong publickey or password."
set rc $E_CONNECT
}
-re "\[0-9]*%" {
exp_continue
}
"sftp>" {
if {\$count < \$argc} {
set arg [lindex \$argv \$count]
send "\$arg\r"
incr count
} else {
send "exit\r"
set output "Disconnected."
if {[info exists rc] != 1} {
set rc $OK
}
}
exp_continue
}
timeout {
set output "Connection timeout."
set rc $E_CONNECT
}
}
if {[info exists output] == 1} {
puts "\$output"
}
exit \$rc
EOF
}
sftp_backup() {
# Checking config
if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
error="Can't open sftp.backup.conf"
rm -rf $tmpdir
rm -f $BACKUP/$user.log
echo "$error" |$SENDMAIL -s "$subj" $email $notify
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
check_result "$E_NOTEXIST" "$error"
fi
# Parse config
source $VESTA/conf/sftp.backup.conf
# Set default port
if [ -z "$(grep 'PORT=' $VESTA/conf/sftp.backup.conf)" ]; then
PORT='22'
fi
# Checking variables
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
error="Can't parse sftp backup configuration"
rm -rf $tmpdir
rm -f $BACKUP/$user.log
echo "$error" |$SENDMAIL -s "$subj" $email $notify
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
check_result "$E_PARSING" "$error"
fi
# Debug info
echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$backup_new_date.tar" |\
tee -a $BACKUP/$user.log
# Checking network connection and write permissions
if [ -z $BPATH ]; then
sftmpdir="vst.bK76A9SUkt"
else
sftmpdir="$BPATH/vst.bK76A9SUkt"
fi
sftpc "mkdir $BPATH" > /dev/null 2>&1
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
rc=$?
if [[ "$rc" != 0 ]]; then
case $rc in
$E_CONNECT) error="Can't login to sftp host $HOST" ;;
$E_FTP) error="Can't create temp folder on sftp $HOST" ;;
esac
rm -rf $tmpdir
rm -f $BACKUP/$user.log
echo "$error" |$SENDMAIL -s "$subj" $email $notify
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
check_result "$rc" "$error"
fi
# Checking retention
if [ -z $BPATH ]; then
backup_list=$(sftpc "ls -l" |awk '{print $9}'|grep "^$user\.")
else
backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}'|grep "^$user\.")
fi
backups_count=$(echo "$backup_list" |wc -l)
if [ ! -z "$ONLY_ONE_FTP_BACKUP" ]; then
TEMP_BACKUPS=$BACKUPS
BACKUPS=1
echo "=== Set BACKUPS=1"
fi
if [ ! -z "$KEEP_N_FTP_BACKUPS" ]; then
TEMP_BACKUPS=$BACKUPS
BACKUPS=$KEEP_N_FTP_BACKUPS
echo "=== Set BACKUPS=$KEEP_N_FTP_BACKUPS"
fi
if [ "$backups_count" -ge "$BACKUPS" ]; then
backups_rm_number=$((backups_count - BACKUPS + 1))
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar.*$//")
echo -e "$(date "+%F %T") Rotated sftp backup: $backup_date" |\
tee -a $BACKUP/$user.log
if [ -z $BPATH ]; then
sftpc "rm $backup" > /dev/null 2>&1
else
sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
fi
done
fi
if [ ! -z "$ONLY_ONE_FTP_BACKUP" ]; then
BACKUPS=$TEMP_BACKUPS
echo "=== Bringing back old value BACKUPS=$BACKUPS"
fi
if [ ! -z "$KEEP_N_FTP_BACKUPS" ]; then
BACKUPS=$TEMP_BACKUPS
echo "=== Bringing back old value BACKUPS=$BACKUPS"
fi
# Uploading backup archive
echo "$(date "+%F %T") Uploading $user.$backup_new_date.tar"|tee -a $BACKUP/$user.log
if [ "$localbackup" = 'yes' ]; then
cd $BACKUP
if [ -z $BPATH ]; then
sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
else
sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
fi
else
cd $tmpdir
tar -cf $BACKUP/$user.$backup_new_date.tar .
cd $BACKUP/
if [ -z $BPATH ]; then
sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
else
sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
fi
rm -f $user.$backup_new_date.tar
fi
}
google_backup() {
# Defining google settings
source $VESTA/conf/google.backup.conf
gsutil="$VESTA/3rdparty/gsutil/gsutil"
export BOTO_CONFIG="$VESTA/conf/.google.backup.boto"
# Debug info
echo -e "$(date "+%F %T") Remote: gs://$BUCKET/$BPATH/$user.$backup_new_date.tar"
# Checking retention
backup_list=$(${gsutil} ls gs://$BUCKET/$BPATH/$user.* 2>/dev/null)
backups_count=$(echo "$backup_list" |wc -l)
if [ "$backups_count" -ge "$BACKUPS" ]; then
backups_rm_number=$((backups_count - BACKUPS))
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
echo -e "$(date "+%F %T") Rotated gcp backup: $backup"
$gsutil rm $backup > /dev/null 2>&1
done
fi
# Uploading backup archive
echo -e "$(date "+%F %T") Uploading $user.$backup_new_date.tar ..."
if [ "$localbackup" = 'yes' ]; then
cd $BACKUP
${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
else
cd $tmpdir
tar -cf $BACKUP/$user.$backup_new_date.tar .
cd $BACKUP/
${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
rc=$?
rm -f $user.$backup_new_date.tar
if [ "$rc" -ne 0 ]; then
check_result "$E_CONNECT" "gsutil failed to upload $user.$backup_new_date.tar"
fi
fi
}
echo -e "\n-- SUMMARY --" |tee -a $BACKUP/$user.log
# Switching on backup system types
for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
case $backup_type in
local) local_backup ;;
ftp) ftp_backup ;;
sftp) sftp_backup ;;
google) google_backup ;;
esac
done
# Removing tmpdir
rm -rf $tmpdir
# Calculation run time
run_time=$((end_time - start_time))
run_time=$((run_time / 60))
current_time=$(date "+%T")
if [ "$run_time" -lt 1 ]; then
run_time=1
fi
min=minutes
if [ "$run_time" -eq 1 ]; then
min=minute
fi
echo "$(date "+%F %T") Size: $size MB" |tee -a $BACKUP/$user.log
echo "$(date "+%F %T") Runtime: $run_time $min" |tee -a $BACKUP/$user.log
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Removing duplicate
touch $USER_DATA/backup.conf
sed -i "/$user.$backup_new_date.tar/d" $USER_DATA/backup.conf
# Registering new backup
backup_str="BACKUP='$user.$backup_new_date.tar'"
backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
backup_str="$backup_str WEB='${web_list// /,}'"
backup_str="$backup_str DNS='${dns_list// /,}'"
backup_str="$backup_str MAIL='${mail_list// /,}'"
backup_str="$backup_str DB='${db_list// /,}'"
backup_str="$backup_str CRON='$cron_list'"