david/synchole
david
/
synchole
Archived
1
0
Fork 0
This repository has been archived on 2023-12-23. You can view files and clone it, but cannot push or open issues or pull requests.
synchole/synchole.sh

240 lines
5.0 KiB
Bash
Executable File

#!/bin/bash
#
# synchole.sh
#
# **** configuration ****
LISTS="whitelist.txt blacklist.txt regex.list" #pihole files to sync
LISTS_PATH="/etc/pihole" #pihole files path
MASTER="10.1.3.2" #IP or hostname of the MASTER server
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
DEPENDENCIES="wget"
NAME="synchole"
VERSION="0.1"
AUTHOR="david@socialnerds.org"
LICENSE="GPLv3"
# **** functions ****
## check for elevated privileges
amiroot() {
if [ $(whoami) != "root" ]; then
return 1
fi
}
## check for local environment
amipihole() {
if [ ! $(which pihole) ]; then
return 1
fi
}
## check if system is debian based
amidebian() {
if [ ! $(which apt-get) ]; then
return 1
fi
}
## output handling
log() {
case $1 in
"error")
level="\033[31m✗\033[0m"
;;
"warn")
level="\033[33m!\033[0m"
;;
"debug")
if [ $DEBUG -eq 1 ]; then
level="\033[2md\033[0m"
else
return 0
fi
;;
"success")
level="\033[32m✓\033[0m"
;;
"info")
level=">"
;;
*)
level="\033[31mLoglevel unknown. Programming error? ($*)\033[0m"
;;
esac
sleep 0.4; echo -e "[$NAME][$level] $2"
}
## update package sources
update_repos() {
apt-get update > /dev/null
if [ $? -ne 0 ]; then
return 1
fi
}
## install debian package
install_package() {
if [ -n $1 ]; then
apt-get install -y $1 &> /dev/null
if [ $? -ne 0 ]; then
return 1
fi
else
return 1
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 info "Commencing preflight checks."
## preflight checks
if ! amiroot; then
log error "You must be root. Exiting."
exit 1
else
log debug "I am root. Continuing."
fi
if ! amidebian; then
log error "I am no debian based system. Exiting."
exit 1
else
log debug "I am debian based. Continuing."
fi
if ! amipihole; then
log error "I am no pihole. Exiting."
exit 1
else
log debug "I am a pihole system. Continuing."
fi
for DEPENDENCY in $DEPENDENCIES; do
if install_package $DEPENDENCY; then
log debug "$DEPENDENCY successfully installed. Continuing."
else
log error "$DEPENDENCY could not be installed. Exiting."
exit 1
fi
done
log success "Preflight checks passed."
## generate $BACKUP_PATH and $TEMP_PATH if not available
if [ ! -d $BACKUP_PATH ]; then
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."
exit 1
fi
else
log debug "$BACKUP_PATH exists. Continuing"
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."
exit 1
fi
else
log debug "$TEMP_PATH exists. Continuing."
fi
## download remote files from MASTER and backup local lists
CHANGES=0
for LIST in $LISTS; do
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 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 success "Successfully installed updated $LIST."
CHANGES=1
fi
else
log warn "No changes in $LIST. Skipping."
fi
done
#run updateGravity
if [ $CHANGES -eq 1 ]; then
pihole -g > /dev/null
if [ $? -ne 0 ]; then
log error "Something went wrong while updating Gravity. Exiting."
else
log success "Successfully updated Gravity."
fi
else
log warn "No files have been changed. Skipping Gravity update."
fi
#run restartdns (if hosts.list has been updated)
log success "Touchdown."
exit 0
# **** end of script ****