david/synchole
david
/
synchole
Archived
1
0
Fork 0

refined the script but it is still an early draft

This commit is contained in:
david 2019-11-08 23:15:01 +01:00
parent 821e37cc3f
commit 38f2dfb6fa
2 changed files with 81 additions and 39 deletions

View File

@ -6,6 +6,7 @@ Shell script to syncronize two or more [pi-hole](https://pi-hole.net) servers. I
## Setup
1. Login via SSH to the `MASTER` server.
2. Create symlinks for all files you want to sync within the webroot of the `MASTER` pihole.
```
cd /var/www/html
@ -14,29 +15,35 @@ cd synchole
sudo ln -s /etc/pihole/whitelist.txt .
sudo ln -s /etc/pihole/blacklist.txt .
sudo ln -s /etc/pihole/regex.list .
sudo ln -s /etc/hosts .
```
3. Login via SSH to the `SLAVE` server.
4. Install synchole on the `SLAVE` server.
```
cd /opt
sudo git clone https://socialg.it/david/synchole.git
```
5. Configure the synchole script.
```
sudo vim /opt/synchole.sh
```
The config section is at the top of the script.
6. Create a cron job for synchole on the `SLAVE` server.
```
sudo crontab -e
```
Example cronjob: `*/5 * * * * /opt/synchole/synchole.sh -q` (this runs the synchole every 5 minutes)
7. Setup postfix to send notifications (from cron) on the `SLAVE` server.
```
<placeholder>
```
8. Repeat steps 3 through 5 for additional `SLAVE` servers.
8. Repeat steps 3 through 7 for additional `SLAVE` servers.
9. Happy syncholeing!

View File

@ -5,17 +5,20 @@
#
# **** configuration ****
LISTS="whitelist.txt blacklist.txt regex.list" #pihole files to sync
LISTS_PATH="/etc/pihole" #pihole files path
HOSTS="hosts" #hosts file
HOSTS_PATH="/etc" #hosts file path
SYNC_HOSTS=1 #enable sync of the hosts file (1=on, 0=off)
MASTER="10.1.3.2" #IP or hostname of the MASTER server
DEPENDENCIES="wget"
DEBUG=1 #enable verbose output (1=on, 0=off)
BACKUP_PATH="/etc/pihole/synchole-backups" #path to backups
BACKUP_VERSIONS=10 #how many backups should be kept
TEMP_PATH="/tmp/synchole-downloads" #path to list downloads
TIMESTAMP=$(date +"%Y%m%d%H%M")
DEPENDENCIES="wget"
NAME="synchole"
VERSION="0.1"
AUTHOR="david@socialnerds.org"
LICENSE="GPLv3"
# **** functions ****
@ -46,23 +49,29 @@ log() {
case $1 in
"error")
level="\033[31m✗\033[0m"
sleep 0.2; echo -e "[$level] $2"
;;
"warn")
level="\033[33m!\033[0m"
sleep 0.2; echo -e "[$level] $2"
;;
"debug")
if [ $DEBUG -eq 1 ]; then
level="\033[2md\033[0m"
sleep 0.2; echo -e "[$level] $2"
else
return 0
fi
;;
*)
"success")
level="\033[32m✓\033[0m"
sleep 0.2; echo -e "[$level] $*"
;;
"info")
level=">"
;;
*)
level="\033[31mLoglevel unknown. Programming error? ($*)\033[0m"
;;
esac
sleep 0.4; echo -e "[$NAME][$level] $2"
}
## update package sources
@ -74,7 +83,7 @@ update_repos() {
}
## install debian package
install() {
install_package() {
if [ -n $1 ]; then
apt-get install -y $1 &> /dev/null
if [ $? -ne 0 ]; then
@ -85,9 +94,35 @@ install() {
fi
}
## backup single list file to $BACKUP_PATH
backup_list() {
if [ -z $1 ]; then
return 1
else
for VERSION in $(eval echo {$BACKUP_VERSIONS..1}); do
if [ -w $BACKUP_PATH/$1.$VERSION ]; then
if [ $VERSION -eq $BACKUP_VERSIONS ]; then
rm $BACKUP_PATH/$1.$VERSION
else
mv $BACKUP_PATH/$1.$VERSION $BACKUP_PATH/$1.$(($VERSION+1))
fi
fi
done
if [ -w $BACKUP_PATH/$1 ]; then
cp $BACKUP_PATH/$1 $BACKUP_PATH/$1.1
fi
if [ -r $LISTS_PATH/$1 ]; then
cp $LISTS_PATH/$1 $BACKUP_PATH/$1
if [ $? -ne 0 ]; then
return 1
fi
fi
fi
}
# **** start of script ****
log "Mission takeoff!"
log info "Commencing preflight checks."
## preflight checks
if ! amiroot; then
@ -111,11 +146,8 @@ else
log debug "I am a pihole system. Continuing."
fi
log "Preflight checks passed."
##installing missing dependencies
for DEPENDENCY in $DEPENDENCIES; do
if install $DEPENDENCY; then
if install_package $DEPENDENCY; then
log debug "$DEPENDENCY successfully installed. Continuing."
else
log error "$DEPENDENCY could not be installed. Exiting."
@ -123,11 +155,11 @@ for DEPENDENCY in $DEPENDENCIES; do
fi
done
log debug "All dependencies available. Continuing."
log success "Preflight checks passed."
#backup local and download remote files from MASTER
## generate $BACKUP_PATH and $TEMP_PATH if not available
if [ ! -d $BACKUP_PATH ]; then
log debug "$BACKUP_PATH does not exist. Creating."
log info "$BACKUP_PATH does not exist. Creating."
mkdir -p $BACKUP_PATH
if [ $? -ne 0 ]; then
log error "Something went wrong while creating $BACKUP_PATH. Exiting."
@ -138,6 +170,7 @@ else
fi
if [ ! -d $TEMP_PATH ]; then
log info "$BACKUP_PATH does not exist. Creating."
mkdir -p $TEMP_PATH
if [ $? -ne 0 ]; then
log error "Something went wrong while creating $TEMP_PATH. Exiting."
@ -147,35 +180,38 @@ else
log debug "$TEMP_PATH exists. Continuing."
fi
## download remote files from MASTER and backup local lists
CHANGES=0
for LIST in $LISTS; do
if [ -r $LISTS_PATH/$LIST ]; then
cp $LISTS_PATH/$LIST $BACKUP_PATH/$LIST\_$TIMESTAMP
if [ $? -ne 0 ]; then
log error "Something went wrong while backing up $LIST. Exiting."
exit 1
else
log "Successfully backed up $LIST."
fi
else
log warn "$LIST not found. Skipping."
fi
log info "Downloading $LIST."
wget http://$MASTER/synchole/$LIST -q -O $TEMP_PATH/$LIST
if [ $? -ne 0 ]; then
log error "Something went wrong while downloading http://$MASTER/synchole/$LIST. Exiting."
exit 1
else
log "Successfully downloaded $LIST."
log success "Successfully downloaded $LIST."
fi
diff $TEMP_PATH/$LIST $LISTS_PATH/$LIST
if [ $? -ne 0 ]; then
log info "Backing up $LIST."
if [ -r $LISTS_PATH/$LIST ]; then
if ! backup_list $LIST; then
log error "Something went wrong while backing up $LIST. Exiting."
exit 1
else
log success "Successfully backed up $LIST."
fi
else
log warn "$LIST not found. Skipping."
fi
log info "Installing $LIST."
cp $TEMP_PATH/$LIST $LISTS_PATH/$LIST
if [ $? -ne 0 ]; then
log error "Something went wrong installing updated file to $LIST_PATH/$LIST"
else
log debug "Successfully installed updated $LIST. Continuing."
log success "Successfully installed updated $LIST."
CHANGES=1
fi
else
@ -189,16 +225,15 @@ if [ $CHANGES -eq 1 ]; then
if [ $? -ne 0 ]; then
log error "Something went wrong while updating Gravity. Exiting."
else
log "Successfully updated Gravity."
log success "Successfully updated Gravity."
fi
else
log warn "No files have been changed. Skipping Gravity update."
fi
#sync hosts
#run restartdns (if hosts updates)
#run restartdns (if hosts.list has been updated)
log "Touchdown! Mission success."
log success "Touchdown."
exit 0
# **** end of script ****