refined the script but it is still an early draft
This commit is contained in:
parent
821e37cc3f
commit
38f2dfb6fa
11
README.md
11
README.md
|
@ -6,6 +6,7 @@ Shell script to syncronize two or more [pi-hole](https://pi-hole.net) servers. I
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
1. Login via SSH to the `MASTER` server.
|
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.
|
2. Create symlinks for all files you want to sync within the webroot of the `MASTER` pihole.
|
||||||
```
|
```
|
||||||
cd /var/www/html
|
cd /var/www/html
|
||||||
|
@ -14,29 +15,35 @@ cd synchole
|
||||||
sudo ln -s /etc/pihole/whitelist.txt .
|
sudo ln -s /etc/pihole/whitelist.txt .
|
||||||
sudo ln -s /etc/pihole/blacklist.txt .
|
sudo ln -s /etc/pihole/blacklist.txt .
|
||||||
sudo ln -s /etc/pihole/regex.list .
|
sudo ln -s /etc/pihole/regex.list .
|
||||||
sudo ln -s /etc/hosts .
|
|
||||||
```
|
```
|
||||||
|
|
||||||
3. Login via SSH to the `SLAVE` server.
|
3. Login via SSH to the `SLAVE` server.
|
||||||
|
|
||||||
4. Install synchole on the `SLAVE` server.
|
4. Install synchole on the `SLAVE` server.
|
||||||
```
|
```
|
||||||
cd /opt
|
cd /opt
|
||||||
sudo git clone https://socialg.it/david/synchole.git
|
sudo git clone https://socialg.it/david/synchole.git
|
||||||
```
|
```
|
||||||
|
|
||||||
5. Configure the synchole script.
|
5. Configure the synchole script.
|
||||||
```
|
```
|
||||||
sudo vim /opt/synchole.sh
|
sudo vim /opt/synchole.sh
|
||||||
```
|
```
|
||||||
The config section is at the top of the script.
|
The config section is at the top of the script.
|
||||||
|
|
||||||
6. Create a cron job for synchole on the `SLAVE` server.
|
6. Create a cron job for synchole on the `SLAVE` server.
|
||||||
```
|
```
|
||||||
sudo crontab -e
|
sudo crontab -e
|
||||||
```
|
```
|
||||||
Example cronjob: `*/5 * * * * /opt/synchole/synchole.sh -q` (this runs the synchole every 5 minutes)
|
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.
|
7. Setup postfix to send notifications (from cron) on the `SLAVE` server.
|
||||||
```
|
```
|
||||||
<placeholder>
|
<placeholder>
|
||||||
```
|
```
|
||||||
8. Repeat steps 3 through 5 for additional `SLAVE` servers.
|
|
||||||
|
8. Repeat steps 3 through 7 for additional `SLAVE` servers.
|
||||||
|
|
||||||
9. Happy syncholeing!
|
9. Happy syncholeing!
|
||||||
|
|
||||||
|
|
||||||
|
|
109
synchole.sh
109
synchole.sh
|
@ -5,17 +5,20 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
# **** configuration ****
|
# **** configuration ****
|
||||||
|
|
||||||
LISTS="whitelist.txt blacklist.txt regex.list" #pihole files to sync
|
LISTS="whitelist.txt blacklist.txt regex.list" #pihole files to sync
|
||||||
LISTS_PATH="/etc/pihole" #pihole files path
|
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
|
MASTER="10.1.3.2" #IP or hostname of the MASTER server
|
||||||
DEPENDENCIES="wget"
|
|
||||||
DEBUG=1 #enable verbose output (1=on, 0=off)
|
DEBUG=1 #enable verbose output (1=on, 0=off)
|
||||||
BACKUP_PATH="/etc/pihole/synchole-backups" #path to backups
|
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
|
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 ****
|
# **** functions ****
|
||||||
|
@ -46,23 +49,29 @@ log() {
|
||||||
case $1 in
|
case $1 in
|
||||||
"error")
|
"error")
|
||||||
level="\033[31m✗\033[0m"
|
level="\033[31m✗\033[0m"
|
||||||
sleep 0.2; echo -e "[$level] $2"
|
|
||||||
;;
|
;;
|
||||||
"warn")
|
"warn")
|
||||||
level="\033[33m!\033[0m"
|
level="\033[33m!\033[0m"
|
||||||
sleep 0.2; echo -e "[$level] $2"
|
|
||||||
;;
|
;;
|
||||||
"debug")
|
"debug")
|
||||||
if [ $DEBUG -eq 1 ]; then
|
if [ $DEBUG -eq 1 ]; then
|
||||||
level="\033[2md\033[0m"
|
level="\033[2md\033[0m"
|
||||||
sleep 0.2; echo -e "[$level] $2"
|
else
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
*)
|
"success")
|
||||||
level="\033[32m✓\033[0m"
|
level="\033[32m✓\033[0m"
|
||||||
sleep 0.2; echo -e "[$level] $*"
|
;;
|
||||||
|
"info")
|
||||||
|
level=">"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
level="\033[31mLoglevel unknown. Programming error? ($*)\033[0m"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
sleep 0.4; echo -e "[$NAME][$level] $2"
|
||||||
}
|
}
|
||||||
|
|
||||||
## update package sources
|
## update package sources
|
||||||
|
@ -74,7 +83,7 @@ update_repos() {
|
||||||
}
|
}
|
||||||
|
|
||||||
## install debian package
|
## install debian package
|
||||||
install() {
|
install_package() {
|
||||||
if [ -n $1 ]; then
|
if [ -n $1 ]; then
|
||||||
apt-get install -y $1 &> /dev/null
|
apt-get install -y $1 &> /dev/null
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
|
@ -85,9 +94,35 @@ install() {
|
||||||
fi
|
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 ****
|
# **** start of script ****
|
||||||
|
|
||||||
log "Mission takeoff!"
|
log info "Commencing preflight checks."
|
||||||
|
|
||||||
## preflight checks
|
## preflight checks
|
||||||
if ! amiroot; then
|
if ! amiroot; then
|
||||||
|
@ -111,11 +146,8 @@ else
|
||||||
log debug "I am a pihole system. Continuing."
|
log debug "I am a pihole system. Continuing."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log "Preflight checks passed."
|
|
||||||
|
|
||||||
##installing missing dependencies
|
|
||||||
for DEPENDENCY in $DEPENDENCIES; do
|
for DEPENDENCY in $DEPENDENCIES; do
|
||||||
if install $DEPENDENCY; then
|
if install_package $DEPENDENCY; then
|
||||||
log debug "$DEPENDENCY successfully installed. Continuing."
|
log debug "$DEPENDENCY successfully installed. Continuing."
|
||||||
else
|
else
|
||||||
log error "$DEPENDENCY could not be installed. Exiting."
|
log error "$DEPENDENCY could not be installed. Exiting."
|
||||||
|
@ -123,11 +155,11 @@ for DEPENDENCY in $DEPENDENCIES; do
|
||||||
fi
|
fi
|
||||||
done
|
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
|
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
|
mkdir -p $BACKUP_PATH
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
log error "Something went wrong while creating $BACKUP_PATH. Exiting."
|
log error "Something went wrong while creating $BACKUP_PATH. Exiting."
|
||||||
|
@ -138,6 +170,7 @@ else
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -d $TEMP_PATH ]; then
|
if [ ! -d $TEMP_PATH ]; then
|
||||||
|
log info "$BACKUP_PATH does not exist. Creating."
|
||||||
mkdir -p $TEMP_PATH
|
mkdir -p $TEMP_PATH
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
log error "Something went wrong while creating $TEMP_PATH. Exiting."
|
log error "Something went wrong while creating $TEMP_PATH. Exiting."
|
||||||
|
@ -147,35 +180,38 @@ else
|
||||||
log debug "$TEMP_PATH exists. Continuing."
|
log debug "$TEMP_PATH exists. Continuing."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
## download remote files from MASTER and backup local lists
|
||||||
CHANGES=0
|
CHANGES=0
|
||||||
for LIST in $LISTS; do
|
for LIST in $LISTS; do
|
||||||
if [ -r $LISTS_PATH/$LIST ]; then
|
log info "Downloading $LIST."
|
||||||
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
|
|
||||||
|
|
||||||
wget http://$MASTER/synchole/$LIST -q -O $TEMP_PATH/$LIST
|
wget http://$MASTER/synchole/$LIST -q -O $TEMP_PATH/$LIST
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
log error "Something went wrong while downloading http://$MASTER/synchole/$LIST. Exiting."
|
log error "Something went wrong while downloading http://$MASTER/synchole/$LIST. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
log "Successfully downloaded $LIST."
|
log success "Successfully downloaded $LIST."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
diff $TEMP_PATH/$LIST $LISTS_PATH/$LIST
|
diff $TEMP_PATH/$LIST $LISTS_PATH/$LIST
|
||||||
if [ $? -ne 0 ]; then
|
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
|
cp $TEMP_PATH/$LIST $LISTS_PATH/$LIST
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
log error "Something went wrong installing updated file to $LIST_PATH/$LIST"
|
log error "Something went wrong installing updated file to $LIST_PATH/$LIST"
|
||||||
else
|
else
|
||||||
log debug "Successfully installed updated $LIST. Continuing."
|
log success "Successfully installed updated $LIST."
|
||||||
CHANGES=1
|
CHANGES=1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
|
@ -189,16 +225,15 @@ if [ $CHANGES -eq 1 ]; then
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
log error "Something went wrong while updating Gravity. Exiting."
|
log error "Something went wrong while updating Gravity. Exiting."
|
||||||
else
|
else
|
||||||
log "Successfully updated Gravity."
|
log success "Successfully updated Gravity."
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
log warn "No files have been changed. Skipping Gravity update."
|
log warn "No files have been changed. Skipping Gravity update."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#sync hosts
|
#run restartdns (if hosts.list has been updated)
|
||||||
#run restartdns (if hosts updates)
|
|
||||||
|
|
||||||
log "Touchdown! Mission success."
|
log success "Touchdown."
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
# **** end of script ****
|
# **** end of script ****
|
||||||
|
|
Reference in New Issue