-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpanelbackup
executable file
·1339 lines (1149 loc) · 52.2 KB
/
cpanelbackup
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
# john@interserver.net
# open 8080 in firewall
# install bc
# only supporting weekly in the new backup for now
# touch /root/.swift/_lesspriv
# touch /root/.swift/_enablecpanelswift
# touch /root/.swift/username_skipall - disable backups and checks
# touch /root/.swift/username_nodisable - never disable backups
# options
# backupcheck - disabled accounts only
# ./cpanelbackup backupcheck (exit after disk check)
# diskcheck all - all
# uploadtest - only run upload test
#VARIABLES
#export NOBACKUPCHECK=1;
#export DELETEDATE=2016-11-20;
#export NOSLASHLIMIT=1;
#todo
# add replace getdiskspace with cached lookup
# run that cachedspace lookup once in a while. Automatically editing backup enabled / disabled in /var/cpanel/users/username
# * check age of cpanel backup process
#
MYVERSION='01.17.2018';
/admin/upscripts
if [ "$1" = "diskcheck" ]; then
# this variable checks for cpbackup running
NOBACKUPCHECK=1;
fi
#config
#must have mailx installed
MAILTO=`grep ^CONTACTEMAIL /etc/wwwacct.conf | cut -d" " -f2`;
# with out backups
# disk space in mb
MBLIMIT=12000;
#inode in mb
INLIMIT=300000;
#-rw------- 1 root root 11403877126 Mar 25 21:57 /backup/2015-03-25/accounts/trendvid.tar.gz
# with backups
CPLIMIT=11403877126;
# days to delete after
DELETEAFTER=40;
if [ -f /root/.swift/hostname ]; then
HOSTNAME=`cat /root/.swift/hostname`;
echo "Overriding hostname at $HOSTNAME";
else
HOSTNAME=`/bin/hostname`;
fi
PKGTMP='/home/cpbackuptmp';
export HOME=/root
date='/bin/date';
FROM='root@${hostname}';
if [ "$1" = "backupcheck" -o "$1" = "diskcheck" ]; then
export RUN_BY_CRON=0;
else
export RUN_BY_CRON=1;
fi
extension=`${date} +%Y-%m-%d`;
# yes both needed
logextension=`${date} +%Y-%m-%d`;
#colors
# only if we are interative
if [[ $- == *i* ]]; then
blue=$(tput setaf 4)
green=$(tput setaf 2)
cyan=$(tput setaf 6)
normal=$(tput sgr0)
fi
echo "Contact mail is $MAILTO";
if [ -d /usr/share/doc/centos-release-5 -o `uname -r | cut -d- -f1` = "2.6.18" -o -f /root/.swift/_forcecentos5mail ]; then
echo "Detected centos5"
MAILARGS2="-- -f $FROM"
MAILARGS="";
else
MAILARGS="-r $FROM"
MAILARGS2="";
fi
if [ "$1" = "mailtest" ]; then
echo "Running mailtest and existing";
echo "test message $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME test message" $MAILTO $MAILARGS2
exit;
fi
echo
#sanity check
if [ ! -d $PKGTMP ]; then
mkdir -p -v $PKGTMP
fi
if [ ! -e /usr/local/cpanel ]; then
echo 'cPanel Only';
exit;
fi
if [ ! -x /admin/swift/c ]; then
echo '/admin/swift/c missing or not executable';
exit;
fi
if [ ! -e /root/.swift/_lesspriv ]; then
/admin/swift/ismkdir $HOSTNAME
fi
# check for disk space over 95%
SLASHLIMIT=95;
if [ "$NOSLASHLIMIT" = "1" ]; then
if [ `df -m | grep /$ | awk '{print $5}' | cut -d% -f1` -gt "$SLASHLIMIT" ]; then
echo "Warning / is over ${SLASHLIMIT}%, existing";
echo "Reduce the disk space Line $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME / is over ${SLASHLIMIT}%" $MAILTO $MAILARGS2
exit;
fi
else
echo 'Skipping slashcheck due to NOSLASHLIMIT=1';
fi
if [ -f /root/.swift/_skipdisk ]; then
echo 'Skipping disk space checks'
fi
LOGFILE=/root/tmp/swift/log-$logextension;
if [ ! -d /root/tmp/swift ]; then
mkdir -p /root/tmp/swift
fi
touch $LOGFILE
echo "Log file will be $LOGFILE";
echo "Starting backup for $HOSTNAME on $extension to log $LOGFILE cpanelbackup version $MYVERSION" 2>&1 | tee -a $LOGFILE
COUNTSUCCESS=0;
COUNTFAILURE=0;
COUNTSKIP=0;
# test upload
function uploadrandom()
{
ok=0;
randomfile='';
randomcheck='';
# clear stale key
if [ -f /root/.swift/.auth_key ]; then
/bin/rm -v /root/.swift/.auth_key
fi
randomfile=`/admin/randompass swift`;
touch $PKGTMP/$randomfile
echo "testing upload with $PKGTMP/$randomfile" 2>&1 | tee -a $LOGFILE
/admin/swift/isput $HOSTNAME $PKGTMP/$randomfile 2>&1 | tee -a $LOGFILE
/bin/rm -v $PKGTMP/$randomfile
sleep 10s
randomcheck=`/admin/swift/isls $HOSTNAME | grep $randomfile`;
if [ "$randomcheck" = "" ]; then
echo 'Upload test failed in uploadrandom function' 2>&1 | tee -a $LOGFILE
else
ok=1;
echo 'Uploading works in uploadrandom function' | tee -a $LOGFILE
fi
sleep 2s;
# 99% of the time we failed, but that one time where the listing didn't work lets send a delete
/admin/swift/isrm $HOSTNAME $randomfile
}
uploadrandom
if [ "$ok" = "0" ]; then
echo ' Upload test failed time 1' 2>&1 | tee -a $LOGFILE
sleep 10s;
for uploadnumbertimes in 2 3 4 5 6 7 8 9 10; do
uploadrandom
if [ "$ok" = "0" ]; then
echo " Upload test failed time ${uploadnumbertimes}" 2>&1 | tee -a $LOGFILE
if [ "${uploadnumbertimes}" = "10" ]; then
echo ' Upload test failed time 10 times emailing' 2>&1 | tee -a $LOGFILE
echo ' Contents of /root/.swift/.auth_key ' | tee -a $LOGFILE
cat /root/.swift/.auth_key | tee -a $LOGFILE
echo "tried /admin/swift/isput $HOSTNAME $PKGTMP/$randomfile Line $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME upload test number 2 failed" $MAILTO $MAILARGS2
else
echo "sleeping ${uploadnumbertimes} m"
sleep ${uploadnumbertimes}m;
fi
else
echo "Upload test works on number ${uploadnumbertimes}" 2>&1 | tee -a $LOGFILE
break
fi
done
fi
echo 'Upload was ok';
sleep 1s;
if [ "$1" = "uploadtest" ]; then
echo 'exit: Just doing upload test';
exit;
fi
echo 'Cleaning up old logs' | tee -a $LOGFILE
if [ -e /usr/sbin/tmpwatch ]; then
/usr/sbin/tmpwatch -a 128 /root/tmp/swift
elif [ -e /usr/sbin/tmpreaper ]; then
/usr/sbin/tmpreaper -a 128 /root/tmp/swift
else
echo 'No program to clean up old logs, ignoring';
fi
# functions
function enable_backups()
{
if [ -f /var/cpanel/users/$1 ]; then
isenabledcheck=`grep ^LEGACY_BACKUP=1 /var/cpanel/users/$1`;
if [ "$isenabledcheck" = "" ]; then
/admin/replace-linux LEGACY_BACKUP=0 LEGACY_BACKUP=1 -- /var/cpanel/users/$1
fi
isenabledcheck2=`grep ^BACKUP=1 /var/cpanel/users/$1`;
if [ "$isenabledcheck2" = "" ]; then
/admin/replace-linux BACKUP=0 BACKUP=1 -- /var/cpanel/users/$1
fi
else
echo "Error: did not find /var/cpanel/users/$1 in function enable_backups" 2>&1 | tee -a $LOGFILE
fi
}
function disable_backups()
{
if [ -f /var/cpanel/users/$1 ]; then
echo "Disabling backups for user $1 at $LINENO";
if [ -f /root/.swift/$1_nodisable ]; then
echo "Backups not disabled due to the existance of /root/.swift/$1_nodisable";
else
isdisabledcheck=`grep ^LEGACY_BACKUP=0 /var/cpanel/users/$1`;
if [ "$isdisabledcheck" = "" ]; then
echo ' Legacy backups disabled';
/admin/replace-linux LEGACY_BACKUP=1 LEGACY_BACKUP=0 -- /var/cpanel/users/$1
else
echo ' Legacy already disabled';
fi
isdisabledcheck2=`grep ^BACKUP=0 /var/cpanel/users/$1`;
if [ "$isdisabledcheck2" = "" ]; then
echo ' Non Legacy disabled';
/admin/replace-linux BACKUP=1 BACKUP=0 -- /var/cpanel/users/$1
else
echo ' Non Legacy already disabled';
fi
fi
else
echo "Error: did not find /var/cpanel/users/$1 in function disable_backups" 2>&1 | tee -a $LOGFILE
fi
}
function issuspended()
{
shell=`cat /etc/passwd | grep ^$1: | cut -d: -f7`;
if [ "$shell" = "/bin/false" ]; then
echo "$1 is suspended (shell) at line $LINENO" 2>&1 | tee -a $LOGFILE
suspended=1;
elif [ -f /var/cpanel/suspended ]; then
echo "$1 is suspended (var/cpanel/suspended) at line $LINENO" 2>&1 | tee -a $LOGFILE
suspended=1;
else
echo "$1 is an active account" 2>&1 | tee -a $LOGFILE
suspended=0;
fi
}
function checkuploaddebug()
{
echo "Since upload has failed lets log what was returned" 2>&1 | tee -a $LOGFILE
echo "Running /admin/swift/rsync $HOSTNAME $1 --check $2" 2>&1 | tee -a $LOGFILE
/admin/swift/rsync $HOSTNAME $1 --check $2 2>&1 | tee -a $LOGFILE
}
function checkupload()
{
#codeblock 1
#checkupload $file cpmove-$user-$cpaneldate.tar.gz
echo "checking $1 md5 with $2 $3 in function checkupload at line $LINENO" 2>&1 | tee -a $LOGFILE
#/admin/swift/rsync webhosting1700.interserver.net /backup/2015-03-25/accounts/mp311com.tar.gz --check cpmove-mp311com-2015-03-26.tar.gz
#/admin/swift/rsync $HOSTNAME $1 --check $2
# give swift some time after upload
sleep 2s;
if [ "$3" = "24" ]; then
check_upload=`/admin/swift/rsync $HOSTNAME $1 --check $2 24 2>/dev/null| egrep "No match found|do not match"`;
else
check_upload=`/admin/swift/rsync $HOSTNAME $1 --check $2 2>/dev/null| egrep "No match found|do not match"`;
fi
echo "Running /admin/swift/rsync $HOSTNAME $1 --check $2 $3 at line $LINENO" 2>&1 | tee -a $LOGFILE
if [ "$check_upload" = "" ]; then
uploadcheck=1;
echo "Backup ok";
else
uploadcheck=0;
echo "no backup found"
fi
if [ "$uploadcheck" = "0" ] || [ "$uploadcheck" = "" ] ; then
echo "Running backup";
# tty to debug but only with out cron
#/admin/swift/rsync $HOSTNAME $1 --put $2 > /dev/tty 2>&1 | tee -a $LOGFILE
echo "Running /admin/swift/rsync $HOSTNAME $1 --put $2" | tee -a $LOGFILE
/admin/swift/rsync $HOSTNAME $1 --put $2 | tee -a $LOGFILE
fi
}
#legacy
inc_backup()
{
cpaneldate=`${date} +%Y-%m-%d`;
curdir=`pwd`;
dir=$1;
echo "Running $dir incremental legacy backup in $backupdir/cpbackup/$dir" 2>&1 | tee -a $LOGFILE
cd $backupdir/cpbackup/$dir
for user in *; do
cd $backupdir/cpbackup/$dir
echo 2>&1 | tee -a $LOGFILE
if [ ! -d $backupdir/cpbackup/$dir/$user ]; then
echo "${cyan}$backupdir/cpbackup/$dir/$user${normal} not a dir" 2>&1 | tee -a $LOGFILE
echo
continue;
fi
echo "Working on user $user" | tee -a $LOGFILE
if [ ! -f /var/cpanel/users/$user ]; then
echo "Did not find ${cyan}/var/cpanel/users/$user${normal} skipping" 2>&1 | tee -a $LOGFILE
echo
continue;
fi
#begin backup checks
issuspended $user
if [ "$suspended" = "1" ]; then
echo "Skipping suspended ${cyan}$user ${normal}" 2>&1 | tee -a $LOGFILE
echo
continue;
fi
# need for disk space
gethomedir $user
if [ "$homecontinue" = "0" ]; then
echo "Skipping unknown homedir ${cyan}$user${normal}" 2>&1 | tee -a $LOGFILE
echo
continue;
fi
if [ ! -f /root/.swift/_skipdisk ]; then
getdiskspace $user
if [ "$1" = "diskcheck" ]; then
echo "DISK_CHECK called $user returned $diskusage and out limit is $MBLIMIT" 2>&1 | tee -a $LOGFILE
fi
if [ "$diskusage" -gt "$MBLIMIT" ]; then
echo "Skipping ${cyan} $user ${normal} as disk usage $disk usage is over limit of $MBLIMIT and backups will be disabled at $LINENO" 2>&1 | tee -a $LOGFILE
if [ ! "$1" = "diskcheck" ]; then
echo "Skipping $user as disk usage $disk usage is over limit of $MBLIMIT Line $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME skipped user $user in swift backup run" $MAILTO $MAILARGS2
fi
echo
disable_backups $user
continue;
else
enable_backups $user
fi
#get inodes
getinodes $user
# no re-enable in inode code
# if the inodes reduce in the future, the diskusage check will then enable the backups
if [ ! "$inodeusage" = "" ]; then
if [ "$inodeusage" -gt "$INLIMIT" ]; then
echo "DISK_CHECK: ${cyan}$user ${normal} is over the inode limit at $inodeusage and backups will be disabled at $LINENO" 2>&1 | tee -a $LOGFILE
disable_backups $user
sleep 2s;
if [ ! "$1" = "diskcheck" ]; then
echo "Skipping $user as disk usage $diskusage is over limit of $MBLIMIT Line $LINENO and backups disabled" | /bin/mail $MAILARGS -s "$HOSTNAME skipped user $user in swift backup run" $MAILTO $MAILARGS2
fi
else
echo "BACKUP CHECK: ${green}$user ${normal} inodes at $inodeusage and ok" 2>&1 | tee -a $LOGFILE
fi
fi
echo "" 2>&1 | tee -a $LOGFILE
# end inodes
if [ "$1" = "diskcheck" ]; then
echo "Running in DISK_CHECK mode, backups will not be run" 2>&1 | tee -a $LOGFILE
echo
continue;
fi
# check backup size
getincbackupsize $backupdir/cpbackup/$dir/$user
if [ "$incdiskusage" -gt "$MBLIMIT" ]; then
echo "Skipping ${cyan}$user{normal} as diskusage $incdiskusage usage is over limit of $MBLIMIT for backup dir $backupdir/cpbackup/$dir/$user" 2>&1 | tee -a $LOGFILE
echo "Skipping $user $incdiskusage is over limit of $MBLIMIT for dir $backupdir/cpbackup/$dir/$user Line $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME skipped user $user in swift backup run" $MAILTO $MAILARGS2
echo
continue
fi
fi
echo "Backing up ${green}$user${normal} folder $backupdir/cpbackup/$dir/$user as cpmove-$user-$extension.tar.gz" 2>&1 | tee -a $LOGFILE
# move to dir to make it a restorable archive for cpanel instead of including the full path in the tar
cd $backupdir/cpbackup/$dir
/admin/swift/fly $HOSTNAME $user frombackup cpmove-$user-$extension.tar.gz
cd $curdir
echo "Sending deleteafter $DELETEAFTER days for $user upload $HOSTNAME cpmove-$user-$extension.tar.gz" 2>&1 | tee -a $LOGFILE
sleep 2s;
/admin/swift/deleteafter $HOSTNAME cpmove-$user-$extension.tar.gz $DELETEAFTER
sleep 2s;
echo
done
}
run_backup()
{
cpaneldate=`${date} +%Y-%m-%d`;
dir=$1;
echo "Running ${green}$dir${normal} legacy backup" 2>&1 | tee -a $LOGFILE
#/admin/swift/mkdir_p $HOSTNAME --force
#cd $backupdir/cpbackup/$dir
# compressed
for file in `find $backupdir/cpbackup/$dir -maxdepth 1 -type f | grep gz$`; do
user=`echo $file | cut -d/ -f5 | cut -d. -f1`;
echo 2>&1 | tee -a $LOGFILE
echo "Working on user ${green}$user${normal} for $dir and file $file" 2>&1 | tee -a $LOGFILE
if [ ! -f /var/cpanel/users/$user ]; then
echo "Did not find ${cyan}/var/cpanel/users/$user${normal} skipping" 2>&1 | tee -a $LOGFILE
continue;
fi
#begin backup checks
issuspended $user
if [ "$suspended" = "1" ]; then
echo "Skipping suspended ${cyan}$user ${normal}" 2>&1 | tee -a $LOGFILE
echo
continue;
fi
if [ ! -e /root/.swift/_skipdisk ]; then
diskusage=`ls -l $file | awk '{print $5}'`;
if [ "$diskusage" -gt "$CPLIMIT" ]; then
echo "Skipping ${cyan} $user ${normal} as disk usage $disk usage is over limit of $CPLIMIT" 2>&1 | tee -a $LOGFILE
echo "Skipping $user as disk usage $disk usage is over limit of $CPLIMIT Line $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME skipped user $user in swift backup run" $MAILTO $MAILARGS2
/admin/replace-linux LEGACY_BACKUP=1 LEGACY_BACKUP=0 -- /var/cpanel/users/$user
/scripts/updateuserdomains
echo "Disabled legacy auto backups for ${cyan}$user${normal}" 2>&1 | tee -a $LOGFILE
echo "$user was too large at $diskusage Line $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME user $user disabled backups" $MAILTO $MAILARGS2
echo
sleep 2s;
continue;
fi
fi
enable_backups $user
echo "" 2>&1 | tee -a $LOGFILE
echo "Starting backup of $user" 2>&1 | tee -a $LOGFILE
echo "Date extension is $cpaneldate" 2>&1 | tee -a $LOGFILE
checkupload $file cpmove-$user-$cpaneldate.tar.gz
#echo " DEBUG: checkupload returned: $check_upload at line $LINENO" 2>&1 | tee -a $LOGFILE
echo
if [ "$uploadcheck" = "0" ] || [ "$uploadcheck" = "" ] ; then
#echo "user ${cyan}$user${normal} md5sum does not match, we have an error at line $LINENO time 1" 2>&1 | tee -a $LOGFILE
for uploadnumbertimes in 1 2 3 4 5 6 7 8 9 10; do
checkupload $file cpmove-$user-$cpaneldate.tar.gz
if [ "$uploadcheck" = "0" ] || [ "$uploadcheck" = "" ] ; then
# commenting out this, it appears to have no reason to run again, it will loop back and check. Running here will do the upload and not check
#checkupload $file cpmove-$user-$cpaneldate.tar.gz
/admin/swift/isrmsplit $HOSTNAME $file cpmove-$user-$cpaneldate.tar.gz
echo "RUN NUMBER $uploadnumbertimes user ${cyan}$user${normal} md5sum does not match, we have an error at line $LINENO" 2>&1 | tee -a $LOGFILE
sleep ${uploadnumbertimes}m
else
echo "SUCCESS RUN NUMBER $uploadnumbertimes user ${cyan}$user${normal} md5sum matches $LINENO" 2>&1 | tee -a $LOGFILE
break;
fi
done
echo "Sending deleteafter $DELETEAFTER days for ${green}$user${normal} upload $HOSTNAME $user-$cpaneldate.tar.gz" 2>&1 | tee -a $LOGFILE
/admin/swift/deleteafter $HOSTNAME cpmove-$user-$cpaneldate.tar.gz $DELETEAFTER
sleep 5s;
else
echo "$user file cpmove-$user-$cpaneldate.tar.gz checksum ok, no upload needed at line $LINENO" 2>&1 | tee -a $LOGFILE
COUNTSKIP=`expr $COUNTSKIP + 1`
echo "Total accounts FAILED backup $COUNTFAILURE" 2>&1 | tee -a $LOGFILE
echo "Total accounts backed up $COUNTSUCCESS" 2>&1 | tee -a $LOGFILE
echo "Total accounts backed up $COUNTSUCCESS" 2>&1 | tee -a $LOGFILE
echo "Total accounts skipped due to md5 match $COUNTSKIP" 2>&1 | tee -a $LOGFILE
continue;
echo "done with ${green}$user${normal}" 2>&1 | tee -a $LOGFILE
sleep 5s;
echo
fi
checkupload $file cpmove-$user-$cpaneldate.tar.gz 24
if [ "$uploadcheck" = "0" ] || [ "$uploadcheck" = "" ]; then
echo "md5sum does not match for ${cyan}$user${normal}, we have an error at line $LINENO" 2>&1 | tee -a $LOGFILE
echo " DEBUG: checkupload returned: $check_upload at line $LINENO" 2>&1 | tee -a $LOGFILE
echo 'Hold on here, lets double check this';
sleep 2s;
if [ -f /root/.swift/.auth_key ]; then
/bin/rm -v /root/.swift/.auth_key
fi
if [ `checkupload $file cpmove-$user-$cpaneldate.tar.gz 24` = "0" ]; then
checkuploaddebug "${file}" "cpmove-${user}-${cpaneldate}.tar.gz"
echo "$user backup failed md5check. $uploadcheck at $LINENO (check2) Line $LINENO version $MYVERSION" | /bin/mail $MAILARGS -s "$HOSTNAME user $user in swift backup run failed md5" $MAILTO $MAILARGS2
COUNTFAILURE=`expr $COUNTFAILURE + 1`
echo "Total accounts FAILED backup $COUNTFAILURE" 2>&1 | tee -a $LOGFILE
echo "Total accounts backed up $COUNTSUCCESS" 2>&1 | tee -a $LOGFILE
echo "Total accounts backed up $COUNTSUCCESS" 2>&1 | tee -a $LOGFILE
echo "Total accounts retried backed up successful $RETRYSUCCESS" 2>&1 | tee -a $LOGFILE
echo "Total accounts retried FAILED $RETRYFAILURE" 2>&1 | tee -a $LOGFILE
fi
echo 2>&1 | tee -a $LOGFILE
sleep 2s;
else
echo "${green}$user${normal} successfully backed up" 2>&1 | tee -a $LOGFILE
COUNTSUCCESS=`expr $COUNTSUCCESS + 1`;
echo "Total accounts backed up $COUNTSUCCESS" 2>&1 | tee -a $LOGFILE
echo "Total accounts FAILED backup $COUNTFAILURE" 2>&1 | tee -a $LOGFILE
echo "Total accounts backed up $COUNTSUCCESS" 2>&1 | tee -a $LOGFILE
sleep 2s;
fi
echo 2>&1 | tee -a $LOGFILE
done
}
function gethomedir()
{
homedir=`cat /etc/passwd | grep ^$1: | cut -d: -f6`;
if [ "$homedir" = "" ]; then
echo "${cyan}$1${normal} got blank home dir at line $LINENO" 2>&1 | tee -a $LOGFILE
homecontinue=0;
elif [ ! -e $homedir ]; then
echo "${cyan}$1${normal} homedir $homedir does not exist at line $LINENO" 2>&1 | tee -a $LOGFILE
homecontinue=0;
else
echo "${green}$1${normal} found homedir $homedir" 2>&1 | tee -a $LOGFILE
homecontinue=1;
fi
}
function getincbackupsize()
{
if [ ! -d $1 ]; then
echo "$1 is not a directory at $LINENO" 2>&1 | tee -a $LOGFILE
else
incdiskusage=`nice -n 19 du -sm $1/ | awk '{print $1}'`;
sleep 1s;
fi
}
#check inodes
function getinodes()
{
# blank values
inodes='';
inodesfloat='';
inodeusage='';
# only support cloudlinux
if [ -f /etc/container/ve.cfg ]; then
inodes=`quota -p -u $1 | grep /dev | awk '{print $6}' | tr -d "*" | tr '\n' '+'`;
inodesfloat=`echo "(${inodes%?})" | bc -l`;
inodeusage=`echo $inodesfloat | cut -d. -f1`;
echo "Inode check: user $1 returned inodes $inodes float $inodesfloat and usage $inodeusage" 2>&1 >> $LOGFILE
fi
}
# check size
function getdiskspace()
{
space='';
diskusage='';
diskusagefloat='';
space=`quota -w -u $1 | grep /dev | awk '{print $2}' | tr -d "*" | tr '\n' '+'`;
# openvz with out quota will return blank
if [ ! "$space" = "" ]; then
diskusagefloat=`echo "(${space%?})/1024" | bc -l`;
diskusage=`echo $diskusagefloat | cut -d. -f1`;
fi
if [ "$diskusage" = "" ]; then
echo "Quota for ${green}$1${normal} is blank. Falling back to slower disk usage and checking $homedir/" 2>&1 | tee -a $LOGFILE
#trailing / needed for symlinks
diskusage=`nice -n 19 du -sm $homedir/ | awk '{print $1}'`;
echo "${green}$user${normal} found quota $diskusage" 2>&1 | tee -a $LOGFILE
else
echo "Got quota of $1 as $diskusage" 2>&1 | tee -a $LOGFILE
fi
}
# don't run two backups at once
echo 'Checking for cpanelswift backup processes' 2>&1 | tee -a $LOGFILE
cpbackupcheck=`ps auxw | grep splitwrapper | grep /admin/swift | awk '{print $9}'`;
echo $cpbackupcheck;
echo "check returned $cpbackupcheck";
if [ ! "$cpbackupcheck" = "" ]; then
echo "Old cpanelbackup / splitwrapper process found from $cpbackupcheck" 2>&1 | tee -a $LOGFILE
echo "check for hung cpanel swift backup processes on $HOSTNAME Line $LINENO - ps shows date $cpbackupcheck" | /bin/mail $MAILARGS -s "$HOSTNAME timed out waiting for swift cpanel backup" $MAILTO $MAILARGS2
exit;
else
if [ -e /usr/sbin/tmpwatch ]; then
/usr/sbin/tmpwatch -a 1 /dev/shm
elif [ -e /usr/sbin/tmpreaper ]; then
/usr/sbin/tmpreaper -a 1 /dev/shm
else
echo 'No program to clean up old logs, ignoring';
fi
fi
if [ ! "$NOBACKUPCHECK" = "1" ]; then
echo 'Checking for cpanel backup processes running' 2>&1 | tee -a $LOGFILE
mystart=0;
while ps axg | grep -v grep | grep -E "/usr/local/cpanel/bin/backup|/usr/local/cpanel/scripts/cpbackup|/usr/local/cpanel//bin/backup|/usr/local/cpanel//scripts/cpbackup" > /dev/null; do
waitdate=`date`;
if [ "$mystart" = "600" ]; then
echo '600 checks starting backup in 10 minutes' 2>&1 | tee -a $LOGFILE
backup_date_check=`ps auxw | grep -v grep | grep -E "/usr/local/cpanel/bin/backup|/usr/local/cpanel/scripts/cpbackup|/usr/local/cpanel//bin/backup|/usr/local/cpanel//scripts/cpbackup" | awk '{print $9 " " $10}'`;
echo "check for hung backup processes on $HOSTNAME Line $LINENO - ps shows a date of $backup_date_check" | /bin/mail $MAILARGS -s "$HOSTNAME timed out waiting for cpanel backup" $MAILTO $MAILARGS2
sleep 10m;
break;
fi
mystart=`expr $mystart + 1`
echo "sleeping 5 minutes while backup process finishes at $waitdate times $mystart" 2>&1 | tee -a $LOGFILE
sleep 5m
done
else
echo 'Skipping checking for cpanel backups due to NOBACKUPCHECK' 2>&1 | tee -a $LOGFILE
fi
echo 'Continuing with backup run' 2>&1 | tee -a $LOGFILE
expected_total=`ls /var/cpanel/users | wc -l`;
echo "Total files counted is ${green}$expected_total${normal} in /var/cpanel/users. This is our expected backup total" 2>&1 | tee -a $LOGFILE
echo "" 2>&1 | tee -a $LOGFILE
# check for disabled backups that should be enabled
echo "" 2>&1 | tee -a $LOGFILE
echo "BACKUP CHECK: Checking space of disabled backup accounts" 2>&1 | tee -a $LOGFILE
if [ ! -f /root/.swift/_skipdisk ]; then
cd /var/cpanel/users
if [ "$2" = "all" ]; then
echo 'Searching for both enabled and disabled backups' 2>&1 | tee -a $LOGFILE
diskchecksearch='';
else
echo 'Searching for only disabled backups' 2>&1 | tee -a $LOGFILE
diskchecksearch='0$';
fi
for cuser in `grep -lri BACKUP=${diskchecksearch} . | cut -d/ -f2`; do
echo "BACKUP CHECK: Found cpanel file ${green}$cuser${normal}" 2>&1 | tee -a $LOGFILE
if [ -f /root/.swift/${cuser}_skipall ]; then
echo "BACKUP CHECK: Found cpanel file ${green}$cuser${normal} set to never backup based on /root/.swift/${cuser}_skipall" 2>&1 | tee -a $LOGFILE
continue;
fi
gethomedir $cuser
if [ "$homecontinue" = "0" ]; then
echo "BACKUP CHECK: Skipping unknown homedir ${cyan}$cuser${normal}" 2>&1 | tee -a $LOGFILE
echo
sleep 2s;
continue;
fi
issuspended $cuser
if [ "$suspended" = "1" ]; then
echo "BACKUP CHECK: Skipping suspended ${cyan}$cuser ${normal}" 2>&1 | tee -a $LOGFILE
echo
disable_backups $cuser
continue;
fi
getdiskspace $cuser
if [ "$diskusage" -gt "$MBLIMIT" ]; then
echo "BACKUP CHECK: ${cyan}$cuser ${normal} is over the space limit at $diskusage" 2>&1 | tee -a $LOGFILE
disable_backups $cuser
else
echo "BACKUP CHECK: ${green}$cuser${normal} is at $diskusage. Enabling future backups" 2>&1 | tee -a $LOGFILE
enable_backups $cuser
fi
echo "" 2>&1 | tee -a $LOGFILE
#get inodes
getinodes $cuser
# no re-enable in inode code
# if the inodes reduce in the future, the diskusage check will then enable the backups
if [ ! "$inodeusage" = "" ]; then
if [ "$inodeusage" -gt "$INLIMIT" ]; then
echo "BACKUP CHECK: ${cyan}$cuser ${normal} is over the inode limit at $inodeusage" 2>&1 | tee -a $LOGFILE
disable_backups $cuser
sleep 2s;
else
echo "BACKUP CHECK: ${green}$cuser ${normal} inodes at $inodeusage and ok" 2>&1 | tee -a $LOGFILE
fi
fi
echo "" 2>&1 | tee -a $LOGFILE
# end inodes
done
/scripts/updateuserdomains
else
echo "/root/.swift/_skipdisk exists, skipping all disk space calls" 2>&1 | tee -a $LOGFILE
fi
echo "BACKUP CHECK: ENDING" 2>&1 | tee -a $LOGFILE
echo "" 2>&1 | tee -a $LOGFILE
if [ "$1" = "backupcheck" -o "$1" = "diskcheck" ]; then
echo "Exiting on only backup check";
exit;
fi
# NEW backup here
BACKUP_CHECK=`cat /var/cpanel/backups/config | grep BACKUPENABLE: | cut -d\' -f2`;
if [ "$BACKUP_CHECK" = "yes" -o "$BACKUP_CHECK" = "BACKUPENABLE: yes" ]; then
echo 'cPanel backups are enabled';
BACKUPDIR=`cat /var/cpanel/backups/config | grep ^BACKUPDIR: | awk '{print $2}'`;
if [ "$BACKUPDIR" = "" ]; then
echo '/var/cpanel/backups/config backup dir is blank' 2>&1 | tee -a $LOGFILE
exit;
elif [ ! -d "$BACKUPDIR" ]; then
echo "${cyan}$BACKUPDIR${normal} is not a directory" 2>&1 | tee -a $LOGFILE
exit;
else
echo "Found backup dir $BACKUPDIR" 2>&1 | tee -a $LOGFILE
fi
# support for inremental
if [ `cat /var/cpanel/backups/config | grep ^BACKUPTYPE: | awk '{print $2}'` = "incremental" ]; then
curdir=`pwd`;
echo 'Found Backup (non legacy) incremental' 2>&1 | tee -a $LOGFILE
# $BACKUPDIR/incremental/accounts
# $BACKUPDIR/weekly/incremental/accounts
# $BACKUPDIR/monthly/incremental/accounts
# prefer weekly
if [ -d $BACKUPDIR/weekly/incremental/accounts ]; then
BTIME=weekly;
elif [ -d $BACKUPDIR/incremental/accounts ]; then
BTIME='';
elif [ -d $BACKUPDIR/monthly/incremental/accounts ]; then
BTIME=monthly;
# 11.64 added in date functions
elif [ -d $BACKUPDIR/weekly ]; then
echo "Searching $BACKUPDIR/weekly" 2>&1 | tee -a $LOGFILE
cd $BACKUPDIR/weekly
latest_dir_check=$(ls -td -- */ | head -n 1);
if [ -d $BACKUPDIR/weekly/$latest_dir_check/accounts ]; then
SEARCHBDIR=0;
USERBACKUPDIR="$BACKUPDIR/weekly/$latest_dir_check/accounts";
echo "Using $BACKUPDIR location $USERBACKUPDIR" 2>&1 | tee -a $LOGFILE
SEARCHBDIR=1;
else
for findbackupdir in *; do
SEARCHBDIR=0;
if [ -d $findbackupdir/accounts ]; then
USERBACKUPDIR="$BACKUPDIR/weekly/$findbackupdir/accounts";
echo "Using $BACKUPDIR location $USERBACKUPDIR" 2>&1 | tee -a $LOGFILE
SEARCHBDIR=1;
continue
fi
done
fi
if [ "$SEARCHBDIR" = "0" ]; then
# fallback
#/backup/2017-05-31/accounts
USERBACKUPDIR=$(find $BACKUPDIR -maxdepth 2 | grep /accounts$ | head -n 1);
if [ "$USERBACKUPDIR" = "" ]; then
echo 'Did not find incremental daily weekly or monthly in new 11.64 search' 2>&1 | tee -a $LOGFILE
echo "Did not find incremental daily weekly or monthly at $LINENO in new 11.64 search but non legacy backup type incrementalwas selected with version $MYVERSION" | /bin/mail $MAILARGS -s "$HOSTNAME incremental backup not found" $MAILTO $MAILARGS2
exit;
else
if [ ! -d "$USERBACKUPDIR" ]; then
echo "$USERBACKUPDIR in fallback at line $LINENO is not a directory" 2>&1 | tee -a $LOGFILE
exit;
fi
fi
fi
else
# fallback
#/backup/2017-05-31/accounts
USERBACKUPDIR=$(find $BACKUPDIR -maxdepth 2 | grep /accounts$ | head -n 1);
if [ "$USERBACKUPDIR" = "" ]; then
echo 'Did not find incremental daily weekly or monthly' 2>&1 | tee -a $LOGFILE
echo "Did not find incremental daily weekly or monthly at $LINENO but non legacy backup type incremental was selected with version $MYVERSION" | /bin/mail $MAILARGS -s "$HOSTNAME incremental backup not found" $MAILTO $MAILARGS2
exit;
else
if [ ! -d "$USERBACKUPDIR" ]; then
echo "$USERBACKUPDIR in fallback at line $LINENO is not a directory" 2>&1 | tee -a $LOGFILE
exit;
fi
SEARCHBDIR=1;
fi
fi
echo "TIME period: $BTIME (if blank daily)" 2>&1 | tee -a $LOGFILE
if [ "$SEARCHBDIR" = "1" ]; then
cd $USERBACKUPDIR
else
USERBACKUPDIR=$BACKUPDIR/$BTIME/incremental/accounts;
cd $USERBACKUPDIR
fi
for dir in *; do
cd $USERBACKUPDIR
if [ -d "$dir" ]; then
echo 2>&1 | tee -a $LOGFILE
echo "Found $dir" 2>&1 | tee -a $LOGFILE
user=$dir;
if [ -f "/var/cpanel/users/$dir" ]; then
issuspended $user
if [ "$suspended" = "1" ]; then
echo "Skipping suspended ${cyan}$user ${normal}" 2>&1 | tee -a $LOGFILE
echo
sleep 2s;
continue;
fi
# need for disk space
gethomedir $user
if [ "$homecontinue" = "0" ]; then
echo "Skipping unknown homedir ${cyan}$user${normal}" 2>&1 | tee -a $LOGFILE
echo
sleep 2s;
continue;
fi
if [ ! -f /root/.swift/_skipdisk ]; then
getdiskspace $user
if [ "$1" = "diskcheck" ]; then
echo "DISK_CHECK $user returned $diskusage with limit $MBLIMIT" 2>&1 | tee -a $LOGFILE
fi
if [ "$diskusage" -gt "$MBLIMIT" ]; then
echo "Skipping ${cyan} $user ${normal} as disk usage $diskusage is over limit of $MBLIMIT and backups will be disabled at $LINENO" 2>&1 | tee -a $LOGFILE
if [ ! "$1" = "diskcheck" ]; then
echo "Skipping $user as disk usage $diskusage is over limit of $MBLIMIT Line $LINENO and backups disabled" | /bin/mail $MAILARGS -s "$HOSTNAME skipped user $user in swift backup run" $MAILTO $MAILARGS2
fi
echo
disable_backups $user
continue;
else
enable_backups $user
fi
#get inodes
getinodes $user
# no re-enable in inode code
# if the inodes reduce in the future, the diskusage check will then enable the backups
if [ ! "$inodeusage" = "" ]; then
if [ "$inodeusage" -gt "$INLIMIT" ]; then
echo "DISK_CHECK: ${cyan}$user ${normal} is over the inode limit at $inodeusage and backups will be disabled at $LINENO" 2>&1 | tee -a $LOGFILE
disable_backups $user
sleep 2s;
if [ ! "$1" = "diskcheck" ]; then
echo "Skipping $user as inode usage $inodeusage is over limit of $INLIMIT Line $LINENO and backups disabled" | /bin/mail $MAILARGS -s "$HOSTNAME skipped user $user in swift backup run" $MAILTO $MAILARGS2
fi
else
echo "BACKUP CHECK: ${green}$user ${normal} inodes at $inodeusage and ok" 2>&1 | tee -a $LOGFILE
fi
fi
echo "" 2>&1 | tee -a $LOGFILE
# end inodes
if [ "$1" = "diskcheck" ]; then
echo "Running in disk check mode - no backup will run" 2>&1 | tee -a $LOGFILE
echo 2>&1 | tee -a $LOGFILE
continue;
fi
# check backup size
getincbackupsize $USERBACKUPDIR/$dir
if [ "$incdiskusage" -gt "$MBLIMIT" ]; then
echo "Skipping ${cyan}$user{normal} as diskusage $incdisk usage is over limit of $MBLIMIT for backup dir $USERBACKUPDIR/$dir" 2>&1 | tee -a $LOGFILE
echo "Skipping $user $incdiskusage is over limit of $MBLIMIT for dir $USERBACKUPDIR/$dir Line $LINENO" | /bin/mail $MAILARGS -s "$HOSTNAME skipped user $user in swift backup run" $MAILTO $MAILARGS2
echo
continue
fi
fi
# start backup
echo "Backing up user ${green}$dir${normal} folder $USERBACKUPDIR/$dir as cpmove-$dir-$extension.tar.gz" 2>&1 | tee -a $LOGFILE
cd $USERBACKUPDIR
/admin/swift/fly $HOSTNAME $dir frombackup cpmove-$dir-$extension.tar.gz
cd $curdir
echo "Sending deleteafter $DELETEAFTER days for $user upload $HOSTNAME $user-$extension.tar.gz" 2>&1 | tee -a $LOGFILE
sleep 2s;
/admin/swift/deleteafter $HOSTNAME cpmove-$dir-$extension.tar.gz $DELETEAFTER
sleep 2s;
echo
else
echo "skipping, did not find /var/cpanel/users/${cyan}$dir ${normal}" 2>&1 | tee -a $LOGFILE
fi
fi
done
# no more to run
# mail backup log
echo "backup log attached" | /bin/mail $MAILARGS -s "$HOSTNAME backup completed" -a $LOGFILE $MAILTO $MAILARGS2
exit;
fi
# end incremental
# files to backup
#/backup/2015-03-25/accounts/aimaiwoa.tar.gz
# only supporting weekly now
#echo "find $BACKUPDIR/weekly -maxdepth 1 -type f | grep /accounts/ | grep tar.gz$";
for file in `find $BACKUPDIR/weekly -maxdepth 3 -type f -printf "%T+\t%p\n" | sort -r | grep /accounts/ | grep tar.gz$ | awk '{print $2}'`; do
user=`echo $file | cut -d/ -f6 | cut -d. -f1`;
cpaneldate=`echo $file | cut -d/ -f4`;
# compatibility
extension=$cpaneldate;
echo 2>&1 | tee -a $LOGFILE
echo "Found file $file for user ${green}$user${normal} for date $cpaneldate" 2>&1 | tee -a $LOGFILE
if [ ! -f $file ]; then
echo "Hit error. file ${cyan}$file${normal} is not a file on check 1 line $LINENO" 2>&1 | tee -a $LOGFILE
sleep 2s;
file=`find $BACKUPDIR/weekly -maxdepth 3 -type f | grep /accounts/ | grep ^${user}.tar.gz$ | head -n 1`;
if [ ! -f $file ]; then
echo "Hit error. file ${cyan}$file${normal} is not a file on check 2 line $LINENO" 2>&1 | tee -a $LOGFILE
continue;
fi
fi
#begin backup checks
issuspended $user
if [ "$suspended" = "1" ]; then
echo "Skipping suspended ${cyan}$user ${normal}" 2>&1 | tee -a $LOGFILE
sleep 2s;
echo
continue;
fi