Advertisement
Panja0

pihole-gemini MASTER script - edited-v2

Sep 16th, 2019
8,839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.86 KB | None | 0 0
  1. #! /bin/bash
  2. # Script was last updated 03-12-2019
  3.  
  4. SCRIPTVER="pihole-gemini v0.0.2.2a"
  5. # --------------------------------------------------------------------------------------------------------
  6. # This script was written to try and keep the white and black lists in sync between two Pi-holes.
  7.  
  8. # The script should be launched from /opt/pihole/gravity.sh
  9. # It should be called in the second to last line of the gravity.sh file and should be run as the user
  10. # configured for the sync process. In my case, I'm using user pi, so you would change the user from 'pi'
  11. # to the username you're using to sync the files (the user you're assigning to HAUSER in this script.)
  12.  
  13. # Example of the line to add as the second to last line of the /opt/pihole/gravity.sh file:
  14.  
  15. # su -c '/usr/local/bin/pihole-gemini' - pi
  16.  
  17. # In the above example, you change the 'pi' at the end to your sync user, and it should appear directly
  18. # above the line that reads:
  19. # "${PIHOLE_COMMAND}" status
  20. # in the /opt/pihole/gravity.sh file.
  21.  
  22. # --------------------------------------------------------------------------------------------------------
  23. # USER-DEFINED VARIABLES - START - Edit the values in this section to configure the script to your install.
  24.  
  25. # User account - Should have sudo and ssh access, and a "matching" account should be set up on both Pi-hole systems
  26. HAUSER=pi
  27.  
  28. # Pi-hole directory (this path is expected to be the same on both Pi-holes, and should be where the FILES are located)
  29. PIHOLEDIR=/etc/pihole
  30.  
  31. # IP Addresses - Note that removing the value or the variable definition of either SSHPORT will cause the script to
  32. # use the default SSH Port of 22 for connections to that ip address.
  33. # Primary Pi-hole ip and ssh port
  34. PIHOLE1=192.168.1.11
  35. SSHPORT1=22
  36.  
  37. # Secondary Pi-hole ip and ssh port
  38. PIHOLE2=192.168.1.12
  39. SSHPORT2=22
  40.  
  41. # Define the local path to store the logfile, DO NOT include a trailing "/". Make sure this path is writeable by the user
  42. # that runs the sync. Change this from /tmp to another folder to prevent logs from being cleared on system restart
  43. LOGFILEPATH="/tmp"
  44.  
  45. # How long to keep old log files (in addition to the current day's log, so a value of 6 means 7 days)
  46. LOGKEEPDAYS=6
  47.  
  48. # USER-DEFINED VARIABLES - END - Edits made below this line may break the script.
  49. # ---------------------------------------------------------------------------------------------------------
  50.  
  51. # SCRIPT VARIABLES - START - These values are used by the script and should not be changed manually.
  52.  
  53. # List of files to sync
  54. FILES=(
  55. 'black.list'
  56. 'blacklist.txt'
  57. 'regex.list'
  58. 'whitelist.txt'
  59. 'lan.list'
  60. 'adlists.list'
  61. 'gravity.list'
  62. )
  63.  
  64. # Full logfile path and name, uncluding timestamp
  65. LOGFILE="${LOGFILEPATH}/pihole-gemini_`date +\%Y\%m\%d`.log"
  66.  
  67. # Flags used to determine script blocks to run.
  68. # RUNUPDATE - 0 = Don't update; 1 = update Pi-holes
  69. RUNUPDATE=0
  70.  
  71. # RESTART - 0 = Don't restart; 1 = restart
  72. RESTART=0
  73.  
  74. # RUNGRAVITY - 0 = Don't update gravity; 1 = Update gravity (don't download lists)
  75. RUNGRAVITY=0
  76.  
  77. # DEFAULT SSHPORT - This value is only provided here to use the default port of 22 if a port is not defined for the specified ip address
  78. DEFAULTSSHPORT=22
  79.  
  80. # INITIALIZE PORT VARIABLES - This segment keeps the SSHPORT variables as set in the user-defined variables section. They are included here
  81. # mainly as a fallback in case the variables are accidentally removed from the user-defined variables section.
  82. SSHPORT1=$SSHPORT1
  83. SSHPORT2=$SSHPORT2
  84.  
  85. # DETERMINE IP AND PORTS TO USE - START
  86. # This section checks the interface used to connect to the local gateway, then determines the current ip address
  87. # based on that interface. Finally, it checks the current ip address against the two addresses the script has
  88. # configured to determine which one is the remote Pi-hole to send commands to, and configures the appropriate port.
  89.  
  90. LOCALIP=$PIHOLE1
  91. case $LOCALIP in
  92. $PIHOLE1)
  93. # echo "Local ip address matches PIHOLE1 ip, so the remote Pi-hole is PIHOLE2."
  94. REMOTEPI=$PIHOLE2
  95. if [ -z $SSHPORT2 ]; then
  96. # SSHPORT2 value is empty, use the default ssh port.
  97. SSHPORT=$DEFAULTSSHPORT
  98. else
  99. # SSHPORT2 value exists, use that for connections to PIHOLE2.
  100. SSHPORT=$SSHPORT2
  101. fi
  102. ;;
  103. $PIHOLE2)
  104. # echo "Local ip address matches PIHOLE2 ip, so the remote Pi-hole is PIHOLE1."
  105. REMOTEPI=$PIHOLE1
  106. if [ -z $SSHPORT1 ]; then
  107. # SSHPORT1 value is empty, use the default ssh port.
  108. SSHPORT=$DEFAULTSSHPORT
  109. else
  110. # SSHPORT1 value exists, use that for connections to PIHOLE1.
  111. SSHPORT=$SSHPORT1
  112. fi
  113. ;;
  114. *)
  115. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  116. echo "* `date '+%Y-%m-%d %H:%M:%S'` - ERROR! - $SCRIPTVER was unable to determine the local ip address." 2>&1 | tee -a $LOGFILE
  117. echo " This is a fatal error. Script can not continue. Please check your" 2>&1 | tee -a $LOGFILE
  118. echo " configuration for the script, and make sure you are connected to" 2>&1 | tee -a $LOGFILE
  119. echo " your router and have an ip address assigned." 2>&1 | tee -a $LOGFILE
  120. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  121. REMOTEPI="Unknown"
  122. exit 1
  123. ;;
  124. esac
  125.  
  126. # DETERMINE IP AND PORTS TO USE - END
  127.  
  128. # SCRIPT VARIABLES - END
  129.  
  130. # LOG HEADER START - The following echo commands are for formatting purposes in the log file.
  131. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  132. echo "`date '+%Y-%m-%d %H:%M:%S'` - $SCRIPTVER was successfully launched as user: $USER" 2>&1 | tee -a $LOGFILE
  133. echo " $LOCALIP is updating Pi-hole on $REMOTEPI"
  134. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  135. # LOG HEADER - END
  136.  
  137. # SSH CONNECTION TEST - START
  138. echo "`date '+%Y-%m-%d %H:%M:%S'` - Testing SSH Connection to $HAUSER@$REMOTEPI. Please wait." 2>&1 | tee -a $LOGFILE
  139.  
  140. ssh -q -p $SSHPORT $HAUSER@$REMOTEPI exit
  141. SSHSTATUS=$?
  142.  
  143. if [ $SSHSTATUS -eq 0 ]; then
  144. # SSH is up
  145. echo "`date '+%Y-%m-%d %H:%M:%S'` - SSH Connection to $HAUSER@$REMOTEPI was tested successfully." 2>&1 | tee -a $LOGFILE
  146. RUNUPDATE=1
  147.  
  148. else
  149. # SSH is down
  150. echo "* `date '+%Y-%m-%d %H:%M:%S'` - ERROR! Unable to establish SSH connection as $HAUSER@$REMOTEPI on port $SSHPORT." 2>&1 | tee -a $LOGFILE
  151. echo "* This is a fatal error as SSH is required for this script. Unable to update $REMOTEPI on port $SSHPORT." 2>&1 | tee -a $LOGFILE
  152. echo "* $REMOTEPI may be offline, you may have specified the wrong port, or you have not correctly configured your SSH keys." 2>&1 | tee -a $LOGFILE
  153. echo "* $REMOTEPI has NOT been updated."
  154. RUNUPDATE=0
  155. fi
  156. # SSH CONNECTION TEST - END
  157.  
  158. # UPDATE REMOTE PIHOLE - START
  159. if [ $RUNUPDATE -eq 1 ]; then
  160. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  161.  
  162. # FILE SYNC - START - This section handles compares the local and remote versions of the files specified in the $FILES variable
  163. # and updates the remote files if neccesary.
  164. for FILE in ${FILES[@]}
  165. do
  166. if [[ -f $PIHOLEDIR/$FILE ]]; then
  167. echo "`date '+%Y-%m-%d %H:%M:%S'` - Comparing local to remote $FILE and updating if neccesary." 2>&1 | tee -a $LOGFILE
  168.  
  169. RSYNC_COMMAND=$(rsync --rsync-path='/usr/bin/sudo /usr/bin/rsync' -aiu -e "ssh -l $HAUSER@$REMOTEPI -p$SSHPORT" $PIHOLEDIR/$FILE $HAUSER@$REMOTEPI:$PIHOLEDIR)
  170.  
  171. if [[ -n "${RSYNC_COMMAND}" ]]; then
  172. # rsync copied changes so restart
  173.  
  174. case $FILE in
  175. adlists.list)
  176. # Updating adlists.list requires only a gravity update
  177. echo "`date '+%Y-%m-%d %H:%M:%S'` - Updated $FILE on $REMOTEPI. Gravity will be updated on $REMOTEPI." 2>&1 | tee -a $LOGFILE
  178. RUNGRAVITY=1
  179. ;;
  180.  
  181. gravity.list)
  182. # Updating gravity.list requires only a gravity update
  183. echo "`date '+%Y-%m-%d %H:%M:%S'` - Updated $FILE on $REMOTEPI. Gravity will be updated on $REMOTEPI." 2>&1 | tee -a $LOGFILE
  184. RUNGRAVITY=1
  185. ;;
  186.  
  187. *)
  188. # Updating white and/or black lists (or other files) requires a remote restart but not a gravity update
  189. echo "`date '+%Y-%m-%d %H:%M:%S'` - Updated $FILE on $REMOTEPI. $REMOTEPI will be restarted." 2>&1 | tee -a $LOGFILE
  190. RESTART=1
  191. ;;
  192. esac
  193.  
  194. else
  195. # no changes so do nothing
  196. echo "`date '+%Y-%m-%d %H:%M:%S'` - Local file $FILE matches $FILE on $REMOTEPI. Remote file was not updated." 2>&1 | tee -a $LOGFILE
  197. fi
  198.  
  199. else
  200. # file does not exist, skipping
  201. echo "`date '+%Y-%m-%d %H:%M:%S'` - Local file $FILE does not exist. Skipping." 2>&1 | tee -a $LOGFILE
  202. fi
  203. done
  204. # FILE SYNC - END
  205.  
  206. # RESTART REMOTEPI (IF NEEDED) - START
  207. if [ $RESTART -eq 1 ]; then
  208. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  209. echo "`date '+%Y-%m-%d %H:%M:%S'` - Updated files have been copied to $REMOTEPI. Restarting..." 2>&1 | tee -a $LOGFILE
  210.  
  211. echo "`date '+%Y-%m-%d %H:%M:%S'` - Sending restart command to pihole-FTL on $REMOTEPI." 2>&1 | tee -a $LOGFILE
  212.  
  213. ssh $HAUSER@$REMOTEPI -p $SSHPORT "sudo -S service pihole-FTL restart"
  214. if [ $? -ne 0 ]; then
  215. echo "* `date '+%Y-%m-%d %H:%M:%S'` - ERROR! - Unable to restart pihole-FTL service on $REMOTEPI." 2>&1 | tee -a $LOGFILE
  216.  
  217. else
  218. echo "`date '+%Y-%m-%d %H:%M:%S'` - Successfully restarted pihole-FTL service on $REMOTEPI." 2>&1 | tee -a $LOGFILE
  219. fi
  220. fi
  221. # RESTART REMOTEPI - END
  222.  
  223. # UPDATE REMOTE GRAVITY (IF NEEDED) - START
  224. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  225. case $RUNGRAVITY in
  226. 0)
  227. # Gravity did not need updating - do nothing
  228. echo "`date '+%Y-%m-%d %H:%M:%S'` - Gravity on $REMOTEPI did not need updating." 2>&1 | tee -a $LOGFILE
  229. ;;
  230. 1)
  231. # Gravity needs refreshing, but not a full update - Update gravity without redownloading lists
  232. echo "`date '+%Y-%m-%d %H:%M:%S'` - Refreshing gravity on $REMOTEPI. Lists will not be redownloaded." 2>&1 | tee -a $LOGFILE
  233. ssh $HAUSER@$REMOTEPI -p $SSHPORT "sudo -S pihole -g --skip-download"
  234. if [ $? -ne 0 ]; then
  235. echo "* `date '+%Y-%m-%d %H:%M:%S'` - ERROR! - Unable to refresh gravity on $REMOTEPI." 2>&1 | tee -a $LOGFILE
  236.  
  237. else
  238. echo "`date '+%Y-%m-%d %H:%M:%S'` - Success! Successfully refreshed gravity on $REMOTEPI." 2>&1 | tee -a $LOGFILE
  239. fi
  240. ;;
  241. esac
  242. # UPDATE REMOTE GRAVITY - END
  243.  
  244. # CLEAN OLD LOG FILES - START
  245. # Check the LOGFILEPATH for outdated log files and delete old logs
  246. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  247. echo "`date '+%Y-%m-%d %H:%M:%S'` - Checking ${LOGFILEPATH} for outdated log files." 2>&1 | tee -a $LOGFILE
  248.  
  249. look_in="${LOGFILEPATH}/pihole-gemini_*.log"
  250.  
  251. del_file=`find $look_in ! -wholename $LOGFILE -daystart -mtime +$LOGKEEPDAYS`
  252. if [ -z "$del_file" ]; then
  253. echo "`date '+%Y-%m-%d %H:%M:%S'` - There were no outdated log files to remove." 2>&1 | tee -a $LOGFILE
  254.  
  255. else
  256. echo "`date '+%Y-%m-%d %H:%M:%S'` - Outdated log file(s) found. Removing..." 2>&1 | tee -a $LOGFILE
  257. sudo find $look_in ! -wholename $LOGFILE -daystart -mtime +$LOGKEEPDAYS -prune -exec rm -rv "{}" \;
  258. if [ $? -ne 0 ]; then
  259. echo "`date '+%Y-%m-%d %H:%M:%S'` - ERROR! Unable to remove outdated log files from $LOGFILEPATH." 2>&1 | tee -a $LOGFILE
  260. else
  261. echo "`date '+%Y-%m-%d %H:%M:%S'` - Outdated log files successfully cleaned." 2>&1 | tee -a $LOGFILE
  262. fi
  263. fi
  264. # CLEAN OLD LOG FILES - END
  265.  
  266. fi
  267. # UPDATE REMOTE PIHOLE - END
  268.  
  269. # LOG FOOTER - START
  270. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  271. echo "`date '+%Y-%m-%d %H:%M:%S'` - $SCRIPTVER completed update of $REMOTEPI from $LOCALIP." 2>&1 | tee -a $LOGFILE
  272. echo "--------------------------------------------------------------------------------------------" 2>&1 | tee -a $LOGFILE
  273. # LOG FOOTER - END
  274.  
  275. # END OF SCRIPT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement