diff --git a/README.md b/README.md index 42782d5..f9c03a2 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,13 @@ sudo ln -s /etc/pihole/regex.list . ``` sudo apt-get update sudo apt-get install wget -sudo wget https://socialg.it/david/synchole/raw/master/synchole.sh -O /usr/bin/synchole.sh -sudo chmod +x /usr/bin/synchole.sh +sudo wget https://socialg.it/david/synchole/raw/master/synchole.sh -O /usr/bin/synchole +sudo chmod +x /usr/bin/synchole ``` 5. Configure the synchole script to your liking. ``` -sudo vim /usr/bin/synchole.sh +sudo vim /usr/bin/synchole ``` The config section is at the top of the script. @@ -37,7 +37,7 @@ The config section is at the top of the script. ``` sudo crontab -e ``` -Example cronjob: `*/5 * * * * /opt/synchole/synchole.sh -q` (this runs synchole every 5 minutes) +Example cronjob: `*/5 * * * * /usr/bin/synchole` (this runs synchole every 5 minutes) 8. Repeat steps 3 through 6 for additional `SLAVE` servers. @@ -50,8 +50,18 @@ Example cronjob: `*/5 * * * * /opt/synchole/synchole.sh -q` (this runs synchole 2. Run the following commands (again) on the `SLAVE` server. ``` -sudo wget https://socialg.it/david/synchole/raw/master/synchole.sh -O /usr/bin/synchole.sh -sudo chmod +x /usr/bin/synchole.sh +sudo wget https://socialg.it/david/synchole/raw/master/synchole.sh -O /usr/bin/synchole +sudo chmod +x /usr/bin/synchole +``` + +## Uninstall + +1. To uninstall synchole login via SSH to the `SLAVE` server. + +2. Run the following commands on the `SLAVE` server. +``` +sudo rm /usr/bin/synchole +sudo rm -r /var/www/html/synchole ``` diff --git a/synchole.sh b/synchole.sh index b39b3b4..0bf43e9 100755 --- a/synchole.sh +++ b/synchole.sh @@ -25,6 +25,13 @@ DEPENDENCIES="wget" # **** functions **** +## check if script is run by cron +amicron() { + if [ -t 1 ]; then + return 1 + fi +} + ## check for elevated privileges amiroot() { if [ $(whoami) != "root" ]; then @@ -49,24 +56,32 @@ log() { level="\033[33m!\033[0m" ;; "debug") - if [ $DEBUG -eq 1 ]; then + if [ $DEBUG -eq 1 ] && [ $CRON -eq 0 ]; then level="\033[2md\033[0m" else return 0 fi ;; "success") - level="\033[32m✓\033[0m" + if [ $CRON -eq 0 ]; then + level="\033[32m✓\033[0m" + else + return 0 + fi ;; "info") - level="i" + if [ $CRON -eq 0 ]; then + level="i" + else + return 0 + fi ;; *) - level="\033[31mLoglevel unknown. Programming error? ($*)\033[0m" + level="\033[31mLoglevel unknown. Programming error? ($1)\033[0m" ;; esac - sleep 0.5; echo -e "[$level] $2" + sleep 0.5; echo -e "[$NAME]-[$level] ${@:2}" } ## backup single list file to $BACKUP_PATH @@ -143,7 +158,16 @@ download_list() { # **** start of script **** +log debug "Starting synchole" + ## preflight checks +log debug "Running preflight checks" +if amicron; then + CRON=1 +else + CRON=0 +fi + if ! amiroot; then log error "You must be root" exit 1 @@ -161,7 +185,7 @@ for DEPENDENCY in $DEPENDENCIES; do fi done -log success "Preflight checks passed" +log success "Preflight checks passed successfully" ## generate $BACKUP_PATH and $DOWNLOAD_PATH if not available if ! create_path $DOWNLOAD_PATH; then @@ -177,12 +201,12 @@ fi ## download remote files from MASTER and backup local lists CHANGES=0 for LIST in $LISTS; do - log info "Downloading $LIST." + log debug "Downloading $LIST from $MASTER" if ! download_list $LIST; then log error "Something went wrong while downloading $LIST" exit 1 else - log success "Successfully downloaded $LIST." + log success "Successfully downloaded $LIST" fi #TODO: don't install new list if to many changes or @@ -190,24 +214,24 @@ for LIST in $LISTS; do # maybe check if wget actually downloaded a file or were we redirected diff $DOWNLOAD_PATH/$LIST $LISTS_PATH/$LIST if [ $? -ne 0 ]; then - log info "Backing up $LIST." + log debug "Backing up $LIST" if [ -r $LISTS_PATH/$LIST ]; then if ! backup_list $LIST; then - log error "Something went wrong while backing up $LIST. Exiting." + log error "Something went wrong while backing up $LIST" exit 1 else - log success "Successfully backed up $LIST." + log debug "Successfully backed up $LIST" fi else - log warn "$LIST not found. Skipping." + log debug "$LIST not found, skipping" fi - log info "Installing $LIST." + log debug "Installing $LIST." if ! install_list $LIST; then log error "Something went wrong while installing $LIST" exit 1 else - log success "Successfully installed $LIST" + log success "Successfully installed new $LIST" CHANGES=1 fi else @@ -217,6 +241,7 @@ done ## run updateGravity if [ $CHANGES -eq 1 ]; then + log debug "Updating gravity" if ! update_gravity; then log error "Something went wrong while updating gravity" exit 1 @@ -227,6 +252,8 @@ else log debug "No files have been changed, skipping gravity update" fi +#TODO: sync hosts and restartdns on changes + exit 0 # **** end of script ****