forked from gadkarisid/pihole-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pihole_sync.sh
98 lines (79 loc) · 2.3 KB
/
pihole_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
#!/bin/bash
###########################
### BEGIN CONFIGURATION ###
###########################
# Define PiHole node name
node_name=PiHole
# Designate whether this node is the 'master' or 'slave'
node_type=master
# Local PiHole directory
LOCAL_DIR=/etc/pihole
# Remote Rsync directory
REMOTE_DIR=/home/pi/PiHole-list-sync
# IP address of Slave PiHole
IP_ADDR=xxx.xxx.xxx.xxx
###########################
#### END CONFIGURATION ####
###########################
# Create initial syslog entry
# If this is the master node
if [[ $node_type == "master" ]]; then
logger "pihole_sync: Node type is master"
# Files to sync
FILES=(black.list blacklist.txt gravity.list regex.list whitelist.txt adlists.list)
# Sync specified files
for FILE in ${FILES[@]}
do
scp -i ~/.ssh/id_rsa $LOCAL_DIR/$FILE pi@$IP_ADDR:$REMOTE_DIR
sleep 5
done
fi
# If this is the slave node
if [[ $node_type == "slave" ]]; then
# Pause while the master node completes its sync
sleep 180
logger "pihole_sync: Node type is slave"
# Files to sync
FILES=(black.list blacklist.txt regex.list whitelist.txt)
ADLISTS=(gravity.list adlists.list)
# Sync flags
SYNC1=0
SYNC2=0
# Determine whether to sync files
for FILE in ${FILES[@]}
do
# Check if the remote file is newer than the local file
if [[ "$REMOTE_DIR/$FILE" -nt "$LOCAL_DIR/$FILE" ]]; then
# If the remote file is newer, then enable sync
((SYNC1++))
logger "pihole_sync:" $FILE "needs to be synced"
else
logger "pihole_sync:" $FILE "does not need to be synced"
fi
done
# Sync files
if [[ "$SYNC1" -ge 1 ]]; then
for FILE in ${FILES[@]}
do
cp -u $REMOTE_DIR/$FILE $LOCAL_DIR/$FILE
done
fi
# Determine whether to sync files
for ADLISTS in ${ADLISTS[@]}
do
# Check if the remote file is newer than the local file
if [[ "$REMOTE_DIR/$ADLISTS" -nt "$LOCAL_DIR/$ADLISTS" ]]; then
# If the remote file is newer, then enable sync
((SYNC2++))
fi
done
# Sync files and update Gravity
if [[ "$SYNC2" -ge 1 ]]; then
for ADLISTS in ${ADLISTS[@]}
do
cp -u $REMOTE_DIR/$ADLISTS $LOCAL_DIR/$ADLISTS
done
pihole restartdns
fi
fi
exit 0