forked from vmstan/gravity-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravity-sync.sh
executable file
·1493 lines (1236 loc) · 32.6 KB
/
gravity-sync.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
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
SCRIPT_START=$SECONDS
# GRAVITY SYNC BY VMSTAN #####################
PROGRAM='Gravity Sync'
VERSION='1.8.3'
# Execute from the home folder of the user who owns it (ex: 'cd ~/gravity-sync')
# For documentation or downloading updates visit https://github.com/vmstan/gravity-sync
# REQUIRED SETTINGS ##########################
# Run './gravity-sync.sh config' to get started
# STANDARD VARIABLES #########################
# GS Folder/File Locations
LOCAL_FOLDR='gravity-sync' # must exist in running user home folder
CONFIG_FILE='gravity-sync.conf' # must exist with primary host/user configured
GS_FILENAME='gravity-sync.sh' # must exist because it's this script
BACKUP_FOLD='backup' # must exist as subdirectory in LOCAL_FOLDR
# Logging Folder/File Locations
LOG_PATH="$HOME/${LOCAL_FOLDR}" # replace in gravity-sync.conf to overwrite
SYNCING_LOG='gravity-sync.log' # replace in gravity-sync.conf to overwrite
CRONJOB_LOG='gravity-sync.cron' # replace in gravity-sync.conf to overwrite
# Interaction Customization
VERIFY_PASS='0' # replace in gravity-sync.conf to overwrite
SKIP_CUSTOM='0' # replace in gravity-sync.conf to overwrite
DATE_OUTPUT='0' # replace in gravity-sync.conf to overwrite
PING_AVOID='0' # replace in gravity-sync.conf to overwrite
# Pi-hole Folder/File Locations
PIHOLE_DIR='/etc/pihole' # default Pi-hole data directory
GRAVITY_FI='gravity.db' # default Pi-hole database file
CUSTOM_DNS='custom.list' # default Pi-hole local DNS lookups
PIHOLE_BIN='/usr/local/bin/pihole' # default Pi-hole binary directory
# OS Settings
BASH_PATH='/bin/bash' # default OS bash path
# SSH CONFIGURATION ##########################
# Suggested not to replace these values here
# Add replacement variables to gravity-sync.conf
SSH_PORT='22' # default SSH port
SSH_PKIF='.ssh/id_rsa' # default local SSH key
##############################################
### DO NOT CHANGE ANYTHING BELOW THIS LINE ###
##############################################
# Script Colors
RED='\033[0;91m'
GREEN='\033[0;92m'
CYAN='\033[0;96m'
YELLOW='\033[0;93m'
PURPLE='\033[0;95m'
BLUE='\033[0;94m'
BOLD='\033[1m'
NC='\033[0m'
# Message Codes
FAIL="[ ${RED}FAIL${NC} ]"
WARN="[ ${PURPLE}WARN${NC} ]"
GOOD="[ ${GREEN} OK ${NC} ]"
STAT="[ ${CYAN}EXEC${NC} ]"
INFO="[ ${YELLOW}INFO${NC} ]"
NEED="[ ${BLUE}NEED${NC} ]"
# FUNCTION DEFINITIONS #######################
# Import Settings
function import_gs {
MESSAGE="Importing ${CONFIG_FILE} Settings"
echo -en "${STAT} $MESSAGE"
if [ -f $HOME/${LOCAL_FOLDR}/${CONFIG_FILE} ]
then
source $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
MESSAGE="Targeting ${REMOTE_USER}@${REMOTE_HOST}"
echo_info
detect_ssh
else
echo_fail
MESSAGE="${CONFIG_FILE} Missing"
echo_info
TASKTYPE='CONFIG'
config_generate
fi
}
# GS Update Functions
## Master Branch
function update_gs {
TASKTYPE='UPDATE'
if [ -f "$HOME/${LOCAL_FOLDR}/dev" ]
then
BRANCH='development'
else
BRANCH='master'
fi
if [ "$BRANCH" = "development" ]
then
MESSAGE="Pulling from origin/${BRANCH}"
echo_info
fi
GIT_CHECK=$(git status | awk '{print $1}')
if [ "$GIT_CHECK" == "fatal:" ]
then
MESSAGE="Requires GitHub Installation"
echo_warn
exit_nochange
else
MESSAGE="Updating Cache"
echo_stat
git fetch --all >/dev/null 2>&1
error_validate
MESSAGE="Applying Update"
echo_stat
git reset --hard origin/${BRANCH} >/dev/null 2>&1
error_validate
fi
exit_withchange
}
# Gravity Core Functions
## Pull Function
function pull_gs {
md5_compare
MESSAGE="Backing Up ${GRAVITY_FI} on $HOSTNAME"
echo_stat
cp ${PIHOLE_DIR}/${GRAVITY_FI} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup >/dev/null 2>&1
error_validate
MESSAGE="Pulling ${GRAVITY_FI} from ${REMOTE_HOST}"
echo_stat
RSYNC_REPATH="rsync"
RSYNC_SOURCE="${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI}"
RSYNC_TARGET="$HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.pull"
create_rsynccmd
MESSAGE="Replacing ${GRAVITY_FI} on $HOSTNAME"
echo_stat
sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.pull ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Validating Settings of ${GRAVITY_FI}"
echo_stat
GRAVDB_OWN=$(ls -ld ${PIHOLE_DIR}/${GRAVITY_FI} | awk '{print $3 $4}')
if [ "$GRAVDB_OWN" != "piholepihole" ]
then
MESSAGE="Validating Ownership on ${GRAVITY_FI}"
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo_stat
sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Continuing Validation of ${GRAVITY_FI}"
echo_stat
fi
GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${GRAVITY_FI} | grep -v f: | grep ${GRAVITY_FI} | awk '{print $1}')
if [ "$GRAVDB_RWE" != "-rw-rw-r--" ]
then
MESSAGE="Validating Permissions on ${GRAVITY_FI}"
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Permissions on ${GRAVITY_FI}"
echo_stat
sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Continuing Validation of ${GRAVITY_FI}"
echo_stat
fi
echo_good
if [ "$SKIP_CUSTOM" != '1' ]
then
if [ "$REMOTE_CUSTOM_DNS" == "1" ]
then
if [ -f ${PIHOLE_DIR}/${CUSTOM_DNS} ]
then
MESSAGE="Backing Up ${CUSTOM_DNS} on $HOSTNAME"
echo_stat
cp ${PIHOLE_DIR}/${CUSTOM_DNS} $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup >/dev/null 2>&1
error_validate
fi
MESSAGE="Pulling ${CUSTOM_DNS} from ${REMOTE_HOST}"
echo_stat
RSYNC_REPATH="rsync"
RSYNC_SOURCE="${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS}"
RSYNC_TARGET="$HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull"
create_rsynccmd
MESSAGE="Replacing ${CUSTOM_DNS} on $HOSTNAME"
echo_stat
sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.pull ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
error_validate
MESSAGE="Validating Settings on ${CUSTOM_DNS}"
echo_stat
CUSTOMLS_OWN=$(ls -ld ${PIHOLE_DIR}/${CUSTOM_DNS} | awk '{print $3 $4}')
if [ "$CUSTOMLS_OWN" != "rootroot" ]
then
MESSAGE="Validating Ownership on ${CUSTOM_DNS}"
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Ownership on ${CUSTOM_DNS}"
echo_stat
sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
error_validate
MESSAGE="Continuing Validation of ${GRAVITY_FI}"
echo_stat
fi
CUSTOMLS_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}')
if [ "$CUSTOMLS_RWE" != "-rw-r--r--" ]
then
MESSAGE="Validating Permissions on ${CUSTOM_DNS}"
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Ownership on ${CUSTOM_DNS}"
echo_stat
sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
error_validate
MESSAGE="Continuing Validation of ${GRAVITY_FI}"
echo_stat
fi
echo_good
fi
fi
MESSAGE="Isolating Regeneration Pathways"
echo_info
sleep 1
MESSAGE="Updating FTLDNS Configuration"
echo_stat
${PIHOLE_BIN} restartdns reloadlists >/dev/null 2>&1
error_validate
MESSAGE="Reloading FTLDNS Services"
echo_stat
${PIHOLE_BIN} restartdns >/dev/null 2>&1
error_validate
logs_export
exit_withchange
}
## Push Function
function push_gs {
md5_compare
intent_validate
MESSAGE="Backing Up ${GRAVITY_FI} from ${REMOTE_HOST}"
echo_stat
RSYNC_REPATH="rsync"
RSYNC_SOURCE="${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI}"
RSYNC_TARGET="$HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.push"
create_rsynccmd
MESSAGE="Pushing ${GRAVITY_FI} to ${REMOTE_HOST}"
echo_stat
RSYNC_REPATH="sudo rsync"
RSYNC_SOURCE="${PIHOLE_DIR}/${GRAVITY_FI}"
RSYNC_TARGET="${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${GRAVITY_FI}"
create_rsynccmd
MESSAGE="Setting Permissions on ${GRAVITY_FI}"
echo_stat
CMD_TIMEOUT='15'
CMD_REQUESTED="sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI}"
create_sshcmd
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo_stat
CMD_TIMEOUT='15'
CMD_REQUESTED="sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI}"
create_sshcmd
if [ "$SKIP_CUSTOM" != '1' ]
then
if [ "$REMOTE_CUSTOM_DNS" == "1" ]
then
MESSAGE="Backing Up ${CUSTOM_DNS} from ${REMOTE_HOST}"
echo_stat
RSYNC_REPATH="rsync"
RSYNC_SOURCE="${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS}"
RSYNC_TARGET="$HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.push"
create_rsynccmd
MESSAGE="Pushing ${CUSTOM_DNS} to ${REMOTE_HOST}"
echo_stat
RSYNC_REPATH="sudo rsync"
RSYNC_SOURCE="${PIHOLE_DIR}/${CUSTOM_DNS}"
RSYNC_TARGET="${REMOTE_USER}@${REMOTE_HOST}:${PIHOLE_DIR}/${CUSTOM_DNS}"
create_rsynccmd
MESSAGE="Setting Permissions on ${CUSTOM_DNS}"
echo_stat
CMD_TIMEOUT='15'
CMD_REQUESTED="sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS}"
create_sshcmd
MESSAGE="Setting Ownership on ${CUSTOM_DNS}"
echo_stat
CMD_TIMEOUT='15'
CMD_REQUESTED="sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS}"
create_sshcmd
fi
fi
MESSAGE="Inverting Tachyon Pulses"
echo_info
sleep 1
MESSAGE="Updating FTLDNS Configuration"
echo_stat
CMD_TIMEOUT='15'
CMD_REQUESTED="${PIHOLE_BIN} restartdns reloadlists"
create_sshcmd
MESSAGE="Reloading FTLDNS Services"
echo_stat
CMD_TIMEOUT='15'
CMD_REQUESTED="${PIHOLE_BIN} restartdns"
create_sshcmd
logs_export
exit_withchange
}
function restore_gs {
MESSAGE="This will restore ${GRAVITY_FI} on $HOSTNAME with the previous version!"
echo_warn
intent_validate
MESSAGE="Restoring ${GRAVITY_FI} on $HOSTNAME"
echo_stat
sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${GRAVITY_FI}.backup ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
MESSAGE="Validating Ownership on ${GRAVITY_FI}"
echo_stat
GRAVDB_OWN=$(ls -ld ${PIHOLE_DIR}/${GRAVITY_FI} | awk '{print $3 $4}')
if [ "$GRAVDB_OWN" == "piholepihole" ]
then
echo_good
else
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo_stat
sudo chown pihole:pihole ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
fi
MESSAGE="Validating Permissions on ${GRAVITY_FI}"
echo_stat
GRAVDB_RWE=$(namei -m ${PIHOLE_DIR}/${GRAVITY_FI} | grep -v f: | grep ${GRAVITY_FI} | awk '{print $1}')
if [ "$GRAVDB_RWE" = "-rw-rw-r--" ]
then
echo_good
else
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Ownership on ${GRAVITY_FI}"
echo_stat
sudo chmod 664 ${PIHOLE_DIR}/${GRAVITY_FI} >/dev/null 2>&1
error_validate
fi
if [ "$SKIP_CUSTOM" != '1' ]
then
if [ -f $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup ]
then
MESSAGE="Restoring ${CUSTOM_DNS} on $HOSTNAME"
echo_stat
sudo cp $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}/${CUSTOM_DNS}.backup ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
error_validate
MESSAGE="Validating Ownership on ${CUSTOM_DNS}"
echo_stat
CUSTOMLS_OWN=$(ls -ld ${PIHOLE_DIR}/${CUSTOM_DNS} | awk '{print $3 $4}')
if [ "$CUSTOMLS_OWN" == "rootroot" ]
then
echo_good
else
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Ownership on ${CUSTOM_DNS}"
echo_stat
sudo chown root:root ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
error_validate
fi
MESSAGE="Validating Permissions on ${CUSTOM_DNS}"
echo_stat
CUSTOMLS_RWE=$(namei -m ${PIHOLE_DIR}/${CUSTOM_DNS} | grep -v f: | grep ${CUSTOM_DNS} | awk '{print $1}')
if [ "$CUSTOMLS_RWE" == "-rw-r--r--" ]
then
echo_good
else
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Setting Ownership on ${CUSTOM_DNS}"
echo_stat
sudo chmod 644 ${PIHOLE_DIR}/${CUSTOM_DNS} >/dev/null 2>&1
error_validate
fi
fi
fi
MESSAGE="Evacuating Saucer Section"
echo_info
sleep 1
MESSAGE="Updating FTLDNS Configuration"
echo_stat
${PIHOLE_BIN} restartdns reloadlists >/dev/null 2>&1
error_validate
MESSAGE="Reloading FTLDNS Services"
echo_stat
${PIHOLE_BIN} restartdns >/dev/null 2>&1
error_validate
logs_export
exit_withchange
}
# Logging Functions
## Core Logging
### Write Logs Out
function logs_export {
MESSAGE="Logging Successful ${TASKTYPE}"
echo_stat
echo -e $(date) "[${TASKTYPE}]" >> ${LOG_PATH}/${SYNCING_LOG}
error_validate
}
### Output Sync Logs
function logs_gs {
import_gs
MESSAGE="Tailing ${LOG_PATH}/${SYNCING_LOG}"
echo_info
echo -e "========================================================"
echo -e "Recent Complete ${YELLOW}PULL${NC} Executions"
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PULL
echo -e "Recent Complete ${YELLOW}PUSH${NC} Executions"
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep PUSH
echo -e "Recent Complete ${YELLOW}RESTORE${NC} Executions"
tail -n 7 "${LOG_PATH}/${SYNCING_LOG}" | grep RESTORE
echo -e "========================================================"
exit_nochange
}
## Crontab Logs
### Core Crontab Logs
function show_crontab {
import_gs
MESSAGE="Replaying Last Cronjob"
echo_stat
if [ -f ${LOG_PATH}/${CRONJOB_LOG} ]
then
if [ -s ${LOG_PATH}/${CRONJOB_LOG} ]
echo_good
MESSAGE="Tailing ${LOG_PATH}/${CRONJOB_LOG}"
echo_info
echo -e "========================================================"
date -r ${LOG_PATH}/${CRONJOB_LOG}
cat ${LOG_PATH}/${CRONJOB_LOG}
echo -e "========================================================"
exit_nochange
then
echo_fail
MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Empty"
echo_info
exit_nochange
fi
else
echo_fail
MESSAGE="${LOG_PATH}/${CRONJOB_LOG} is Missing"
echo_info
exit_nochange
fi
}
# Validate Functions
## Validate GS Folders
function validate_gs_folders {
MESSAGE="Validating ${PROGRAM} Folders on $HOSTNAME"
echo_stat
if [ ! -d $HOME/${LOCAL_FOLDR} ]
then
MESSAGE="Unable to Find $HOME/${LOCAL_FOLDR}"
echo_fail
exit_nochange
fi
if [ ! -d $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD} ]
then
MESSAGE="Unable to Find $HOME/${LOCAL_FOLDR}/${BACKUP_FOLD}"
echo_fail
exit_nochange
fi
echo_good
}
## Validate Pi-hole Folders
function validate_ph_folders {
MESSAGE="Validating Pi-hole Configuration on $HOSTNAME"
echo_stat
if [ ! -f ${PIHOLE_BIN} ]
then
MESSAGE="Unable to Validate Pi-Hole is Installed"
echo_fail
exit_nochange
fi
if [ ! -d ${PIHOLE_DIR} ]
then
MESSAGE="Unable to Validate Pi-Hole Configuration Directory"
echo_fail
exit_nochange
fi
echo_good
}
## Validate SSHPASS
function validate_os_sshpass {
SSHPASSWORD=''
if hash sshpass 2>/dev/null
then
MESSAGE="SSHPASS Utility Detected"
echo_warn
if [ -z "$REMOTE_PASS" ]
then
MESSAGE="Using SSH Key-Pair Authentication"
echo_info
else
MESSAGE="Testing Authentication Options"
echo_stat
timeout 5 ssh -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} 'exit' >/dev/null 2>&1
if [ "$?" != "0" ]
then
SSHPASSWORD="sshpass -p ${REMOTE_PASS}"
MESSAGE="Using SSH Password Authentication"
echo_warn
else
MESSAGE="Valid Key-Pair Detected ${NC}(${RED}Password Ignored${NC})"
echo_info
fi
fi
else
SSHPASSWORD=''
MESSAGE="Using SSH Key-Pair Authentication"
echo_info
fi
MESSAGE="Validating Connection to ${REMOTE_HOST}"
echo_stat
CMD_TIMEOUT='5'
CMD_REQUESTED="exit"
create_sshcmd
}
## Determine SSH Pathways
function create_sshcmd {
if hash ssh 2>/dev/null
then
if [ -z "$SSHPASSWORD" ]
then
timeout --preserve-status ${CMD_TIMEOUT} ${SSH_CMD} -p ${SSH_PORT} -i $HOME/${SSH_PKIF} -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "${CMD_REQUESTED}"
error_validate
else
timeout --preserve-status ${CMD_TIMEOUT} ${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} "${CMD_REQUESTED}"
error_validate
fi
elif hash dbclient 2>/dev/null
then
timeout --preserve-status ${CMD_TIMEOUT} ${SSH_CMD} -p ${SSH_PORT} -i $HOME/${SSH_PKIF} ${REMOTE_USER}@${REMOTE_HOST} "${CMD_REQUESTED}"
error_validate
fi
}
## Determine SSH Pathways
function create_rsynccmd {
if hash ssh 2>/dev/null
then
if [ -z "$SSHPASSWORD" ]
then
rsync --rsync-path="${RSYNC_REPATH}" -e "${SSH_CMD} -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${RSYNC_SOURCE} ${RSYNC_TARGET} >/dev/null 2>&1
error_validate
else
rsync --rsync-path="${RSYNC_REPATH}" -e "${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${RSYNC_SOURCE} ${RSYNC_TARGET} >/dev/null 2>&1
error_validate
fi
elif hash dbclient 2>/dev/null
then
rsync --rsync-path="${RSYNC_REPATH}" -e "${SSH_CMD} -p ${SSH_PORT} -i $HOME/${SSH_PKIF}" ${RSYNC_SOURCE} ${RSYNC_TARGET} >/dev/null 2>&1
error_validate
fi
}
## Detect SSH-KEYGEN
function detect_sshkeygen {
MESSAGE="Validating SSH-KEYGEN install on $HOSTNAME"
echo_stat
if hash ssh-keygen >/dev/null 2>&1
then
echo_good
else
echo_fail
MESSAGE="SSH-KEYGEN is Required"
echo_info
MESSAGE="Attempting to Compensate"
echo_warn
if hash dropbearkey >/dev/null 2>&1
then
MESSAGE="Using DROPBEARKEY Instead"
echo_info
if [ ! -d $HOME/.ssh ]
then
mkdir $HOME/.ssh >/dev/null 2>&1
fi
KEYGEN_COMMAND="dropbearkey -t rsa -f"
else
MESSAGE="No Alternatives Located"
echo_info
exit_nochange
fi
fi
}
function generate_sshkey {
if [ -z $INPUT_REMOTE_PASS ]
then
if [ -f $HOME/${SSH_PKIF} ]
then
MESSAGE="Using Existing ~/${SSH_PKIF}"
echo_info
else
if hash ssh-keygen >/dev/null 2>&1
then
MESSAGE="Generating ~/${SSH_PKIF} (SSH-KEYGEN)"
echo_stat
ssh-keygen -q -P "" -t rsa -f $HOME/${SSH_PKIF} >/dev/null 2>&1
error_validate
elif hash dropbearkey >/dev/null 2>&1
then
MESSAGE="Generating ~/${SSH_PKIF} (DROPBEARKEY)"
echo_stat
if [ ! -d $HOME/.ssh ]
then
mkdir $HOME/.ssh >/dev/null 2>&1
fi
dropbearkey -t rsa -f $HOME/${SSH_PKIF} >/dev/null 2>&1
error_validate
else
MESSAGE="No SSH Key Generator Located"
echo_warn
exit_nochange
fi
fi
fi
}
function export_sshkey {
if [ -z $REMOTE_PASS ]
then
if [ -f $HOME/${SSH_PKIF} ]
then
MESSAGE="Registering Key-Pair on ${REMOTE_HOST}"
echo_info
#MESSAGE="Enter ${REMOTE_USER}@${REMOTE_HOST} Password Below"
#echo -e "${NEED} ${MESSAGE}"
if hash ssh-copy-id 2>/dev/null
then
ssh-copy-id -f -p ${SSH_PORT} -i $HOME/${SSH_PKIF}.pub ${REMOTE_USER}@${REMOTE_HOST}
elif hash dbclient 2>/dev/null
then
dropbearkey -y -f $HOME/${SSH_PKIF} | grep "^ssh-rsa " > $HOME/${SSH_PKIF}.pub
cat $HOME/${SSH_PKIF}.pub | dbclient -p ${SSH_PORT} ${REMOTE_USER}@${REMOTE_HOST} 'cat - >> .ssh/authorized_keys'
fi
else
MESSAGE="Error Registering Key-Pair"
echo_warn
fi
fi
}
## Detect Package Manager
function distro_check {
if hash apt-get 2>/dev/null
then
PKG_MANAGER="apt-get"
PKG_INSTALL="sudo apt-get --yes --no-install-recommends --quiet install"
elif hash rpm 2>/dev/null
then
if hash dnf 2>/dev/null
then
PKG_MANAGER="dnf"
elif hash yum 2>/dev/null
then
PKG_MANAGER="yum"
else
MESSAGE="Unable to find OS Package Manager"
echo_info
exit_nochange
fi
PKG_INSTALL="sudo ${PKG_MANAGER} install -y"
else
MESSAGE="Unable to find OS Package Manager"
echo_info
exit_nochange
fi
}
## Detect SSH & RSYNC
function detect_ssh {
MESSAGE="Validating SSH Client on $HOSTNAME"
echo_stat
if hash ssh 2>/dev/null
then
MESSAGE="${MESSAGE} (OpenSSH)"
echo_good
SSH_CMD='ssh'
elif hash dbclient 2>/dev/null
then
MESSAGE="${MESSAGE} (Dropbear)"
echo_good
SSH_CMD='dbclient'
else
echo_fail
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Installing SSH Client with ${PKG_MANAGER}"
echo_stat
${PKG_INSTALL} ssh-client >/dev/null 2>&1
error_validate
fi
MESSAGE="Validating RSYNC Installed on $HOSTNAME"
echo_stat
if hash rsync 2>/dev/null
then
echo_good
else
echo_fail
MESSAGE="RSYNC is Required"
echo_warn
distro_check
MESSAGE="Attempting to Compensate"
echo_warn
MESSAGE="Installing RSYNC with ${PKG_MANAGER}"
echo_stat
${PKG_INSTALL} rsync >/dev/null 2>&1
error_validate
fi
}
## Error Validation
function error_validate {
if [ "$?" != "0" ]
then
echo_fail
exit 1
else
echo_good
fi
}
## Validate Sync Required
function md5_compare {
HASHMARK='0'
MESSAGE="Analyzing ${GRAVITY_FI} on ${REMOTE_HOST}"
echo_stat
primaryDBMD5=$(${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${GRAVITY_FI}" | sed 's/\s.*$//')
error_validate
MESSAGE="Analyzing ${GRAVITY_FI} on $HOSTNAME"
echo_stat
secondDBMD5=$(md5sum ${PIHOLE_DIR}/${GRAVITY_FI} | sed 's/\s.*$//')
error_validate
if [ "$primaryDBMD5" == "$secondDBMD5" ]
then
HASHMARK=$((HASHMARK+0))
else
MESSAGE="Differenced ${GRAVITY_FI} Detected"
echo_warn
HASHMARK=$((HASHMARK+1))
fi
if [ "$SKIP_CUSTOM" != '1' ]
then
if [ -f ${PIHOLE_DIR}/${CUSTOM_DNS} ]
then
if ${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS}
then
REMOTE_CUSTOM_DNS="1"
MESSAGE="Analyzing ${CUSTOM_DNS} on ${REMOTE_HOST}"
echo_stat
primaryCLMD5=$(${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} "md5sum ${PIHOLE_DIR}/${CUSTOM_DNS} | sed 's/\s.*$//'")
error_validate
MESSAGE="Analyzing ${CUSTOM_DNS} on $HOSTNAME"
echo_stat
secondCLMD5=$(md5sum ${PIHOLE_DIR}/${CUSTOM_DNS} | sed 's/\s.*$//')
error_validate
if [ "$primaryCLMD5" == "$secondCLMD5" ]
then
# MESSAGE="${CUSTOM_DNS} Identical"
# echo_info
HASHMARK=$((HASHMARK+0))
else
MESSAGE="Differenced ${CUSTOM_DNS} Detected"
echo_warn
HASHMARK=$((HASHMARK+1))
fi
else
MESSAGE="No ${CUSTOM_DNS} Detected on ${REMOTE_HOST}"
echo_info
fi
else
if ${SSHPASSWORD} ${SSH_CMD} -p ${SSH_PORT} -i "$HOME/${SSH_PKIF}" ${REMOTE_USER}@${REMOTE_HOST} test -e ${PIHOLE_DIR}/${CUSTOM_DNS}
then
REMOTE_CUSTOM_DNS="1"
MESSAGE="${REMOTE_HOST} has ${CUSTOM_DNS}"
HASHMARK=$((HASHMARK+1))
echo_info
fi
MESSAGE="No ${CUSTOM_DNS} Detected on $HOSTNAME"
echo_info
fi
fi
if [ "$HASHMARK" != "0" ]
then
MESSAGE="Replication Required"
echo_warn
HASHMARK=$((HASHMARK+0))
else
MESSAGE="No Replication Required"
echo_info
exit_nochange
fi
}
## Validate Intent
function intent_validate {
if [ "$VERIFY_PASS" == "0" ]
then
PHASER=$((( RANDOM % 4 ) + 1 ))
if [ "$PHASER" = "1" ]
then
INTENT="FIRE PHOTON TORPEDOS"
elif [ "$PHASER" = "2" ]
then
INTENT="FIRE ALL PHASERS"
elif [ "$PHASER" = "3" ]
then
INTENT="EJECT THE WARPCORE"
elif [ "$PHASER" = "4" ]
then
INTENT="ENGAGE TRACTOR BEAM"
fi
MESSAGE="Type ${INTENT} to Confirm"
echo_need
read INPUT_INTENT
if [ "${INPUT_INTENT}" != "${INTENT}" ]
then
MESSAGE="${TASKTYPE} Aborted"
echo_info
exit_nochange
fi
else
MESSAGE="Verification Bypassed"
echo_warn
fi
}
# Configuration Management
## Generate New Configuration
function config_generate {
detect_ssh
MESSAGE="Creating ${CONFIG_FILE} from Template"
echo_stat
cp $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}.example $HOME/${LOCAL_FOLDR}/${CONFIG_FILE}
error_validate
MESSAGE="IP or DNS of Primary Pi-hole"
echo_need
read INPUT_REMOTE_HOST
if [ "${PING_AVOID}" != "1" ]
then
MESSAGE="Testing Network Connection (PING)"
echo_stat
ping -c 3 ${INPUT_REMOTE_HOST} >/dev/null 2>&1
error_validate
else
MESSAGE="Bypassing Network Testing (PING)"
echo_warn
fi
MESSAGE="SSH User with SUDO rights"
echo_need