fork download
  1. #!/bin/bash
  2.  
  3. LOG_FILE="/tmp/local_update.log"
  4. BACKUP_LOG_DIR="$HOME/Desktop"
  5. BACKUP_LOG_FILE="$BACKUP_LOG_DIR/local_update.log"
  6. REMOTE_USER="username"
  7. REMOTE_HOST="remote_host_address"
  8. REMOTE_SCRIPT_LOCAL="/tmp/remote_update.sh"
  9. REMOTE_SCRIPT_REMOTE="/tmp/remote_update.sh"
  10. SUDO_ASKPASS_PATH="$HOME/sudo_askpass.sh"
  11.  
  12. # Export SUDO_ASKPASS
  13. export SUDO_ASKPASS="$SUDO_ASKPASS_PATH"
  14.  
  15. # Function to display messages in color
  16. function echo_colored() {
  17. local color=$1
  18. local message=$2
  19. case $color in
  20. red) echo -e "\e[31m$message\e[0m" | tee -a $LOG_FILE ;;
  21. green) echo -e "\e[32m$message\e[0m" | tee -a $LOG_FILE ;;
  22. yellow) echo -e "\e[33m$message\e[0m" | tee -a $LOG_FILE ;;
  23. blue) echo -e "\e[34m$message\e[0m" | tee -a $LOG_FILE ;;
  24. magenta) echo -e "\e[35m$message\e[0m" | tee -a $LOG_FILE ;;
  25. cyan) echo -e "\e[36m$message\e[0m" | tee -a $LOG_FILE ;;
  26. white) echo -e "\e[37m$message\e[0m" | tee -a $LOG_FILE ;;
  27. *) echo "$message" | tee -a $LOG_FILE ;;
  28. esac
  29. }
  30.  
  31. # Function to handle errors
  32. function handle_error() {
  33. echo_colored red "Error on line $1"
  34. exit 1
  35. }
  36.  
  37. trap 'handle_error $LINENO' ERR
  38.  
  39. # Function to check if a command exists
  40. function command_exists() {
  41. command -v "$1" &> /dev/null
  42. }
  43.  
  44. # Ensure required commands are available
  45. for cmd in apt-get ssh scp; do
  46. if ! command_exists $cmd; then
  47. echo_colored red "Error: $cmd is not installed."
  48. exit 1
  49. fi
  50. done
  51.  
  52. # Ensure log file exists and create backup
  53. if [ ! -f $LOG_FILE ]; then
  54. touch $LOG_FILE
  55. fi
  56. cp $LOG_FILE $BACKUP_LOG_FILE
  57.  
  58. # Function to display a cascading rainbow effect
  59. function show_rainbow_progress() {
  60. local message=$1
  61. local colors=(31 32 33 34 35 36 37 91 92 93 94 95 96)
  62. local color_index=0
  63.  
  64. # Save cursor position
  65. tput sc
  66.  
  67. while kill -0 $! 2>/dev/null; do
  68. # Clear the current line
  69. tput el
  70.  
  71. # Print the progress message with color
  72. echo -ne "\e[${colors[color_index]}m$message\e[0m\r"
  73. color_index=$(( (color_index + 1) % ${#colors[@]} ))
  74. sleep 0.2
  75.  
  76. # Restore cursor position
  77. tput rc
  78. done
  79.  
  80. # Clear the line and print "Done"
  81. tput el
  82. echo -e "\e[32m$message [Done]\e[0m"
  83. }
  84.  
  85. # Function to get system identification
  86. function get_system_identification() {
  87. echo -e "\n\e[36mSystem Identification:\e[0m"
  88. echo -e "\e[36mHostname:\e[0m \e[32m$(hostname)\e[0m"
  89. echo -e "\e[36mOperating System:\e[0m \e[32m$(lsb_release -d | cut -f2)\e[0m"
  90. echo -e "\e[36mKernel Version:\e[0m \e[32m$(uname -r)\e[0m"
  91. echo -e "\e[36mLog File Path:\e[0m \e[32m$LOG_FILE\e[0m"
  92. echo -e "\e[36mLog File Size:\e[0m \e[32m$(du -h $LOG_FILE | cut -f1)\e[0m"
  93. echo -e "\e[36mBackup Log File:\e[0m \e[32m$BACKUP_LOG_FILE\e[0m"
  94. }
  95.  
  96. # Create the remote update script if it doesn't exist
  97. cat << 'EOF' > $REMOTE_SCRIPT_LOCAL
  98. #!/bin/bash
  99.  
  100. LOG_FILE="/tmp/remote_update.log"
  101. SUMMARY_LOG="/tmp/remote_update_summary.log"
  102. BACKUP_LOG_DIR="$HOME/Desktop"
  103. BACKUP_LOG_FILE="$BACKUP_LOG_DIR/remote_update.log"
  104. SUDO_ASKPASS_PATH="$HOME/sudo_askpass.sh"
  105.  
  106. # Export SUDO_ASKPASS
  107. export SUDO_ASKPASS="$SUDO_ASKPASS_PATH"
  108.  
  109. # Function to display messages in color
  110. function echo_colored() {
  111. local color=$1
  112. local message=$2
  113. case $color in
  114. red) echo -e "\e[31m$message\e[0m" | tee -a $LOG_FILE ;;
  115. green) echo -e "\e[32m$message\e[0m" | tee -a $LOG_FILE ;;
  116. yellow) echo -e "\e[33m$message\e[0m" | tee -a $LOG_FILE ;;
  117. blue) echo -e "\e[34m$message\e[0m" | tee -a $LOG_FILE ;;
  118. magenta) echo -e "\e[35m$message\e[0m" | tee -a $LOG_FILE ;;
  119. cyan) echo -e "\e[36m$message\e[0m" | tee -a $LOG_FILE ;;
  120. white) echo -e "\e[37m$message\e[0m" | tee -a $LOG_FILE ;;
  121. *) echo "$message" | tee -a $LOG_FILE ;;
  122. esac
  123. }
  124.  
  125. function handle_error() {
  126. echo_colored red "Error on line $1"
  127. exit 1
  128. }
  129.  
  130. trap 'handle_error $LINENO' ERR
  131.  
  132. # Ensure log file exists
  133. if [ ! -f $LOG_FILE ]; then
  134. touch $LOG_FILE
  135. fi
  136. cp $LOG_FILE $BACKUP_LOG_FILE
  137.  
  138. # Function to display a cascading rainbow effect
  139. function show_rainbow_progress() {
  140. local message=$1
  141. local colors=(31 32 33 34 35 36 37 91 92 93 94 95 96)
  142. local color_index=0
  143.  
  144. while kill -0 $! 2>/dev/null; do
  145. echo -ne "\e[${colors[color_index]}m$message\e[0m\r"
  146. color_index=$(( (color_index + 1) % ${#colors[@]} ))
  147. sleep 0.2
  148. done
  149. echo -e "\e[32m$message [Done]\e[0m"
  150. }
  151.  
  152. # Function to get system identification
  153. function get_system_identification() {
  154. echo -e "\n\e[36mSystem Identification:\e[0m"
  155. echo -e "\e[36mHostname:\e[0m \e[32m$(hostname)\e[0m"
  156. echo -e "\e[36mOperating System:\e[0m \e[32m$(lsb_release -d | cut -f2)\e[0m"
  157. echo -e "\e[36mKernel Version:\e[0m \e[32m$(uname -r)\e[0m"
  158. echo -e "\e[36mLog File Path:\e[0m \e[32m$LOG_FILE\e[0m"
  159. echo -e "\e[36mLog File Size:\e[0m \e[32m$(du -h $LOG_FILE | cut -f1)\e[0m"
  160. echo -e "\e[36mBackup Log File:\e[0m \e[32m$BACKUP_LOG_FILE\e[0m"
  161. }
  162.  
  163. echo_colored cyan "Remote System Identification:"
  164. get_system_identification
  165.  
  166. echo_colored blue "\nUpdating remote package list..."
  167. sudo -A apt-get update 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Updating package list"
  168.  
  169. echo_colored blue "\nUpgrading remote packages..."
  170. sudo -A apt-get upgrade -y 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Upgrading packages"
  171.  
  172. echo_colored blue "\nPerforming remote distribution upgrade..."
  173. sudo -A apt-get dist-upgrade -y 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Performing distribution upgrade"
  174.  
  175. echo_colored blue "\nRemoving unnecessary remote packages..."
  176. sudo -A apt-get autoremove -y 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Removing unnecessary packages"
  177.  
  178. echo_colored blue "\nCleaning up remote system..."
  179. sudo -A apt-get clean 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Cleaning up"
  180.  
  181. echo_colored blue "\nUpdating Pi-hole..."
  182. sudo -A pihole -up 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Updating Pi-hole"
  183.  
  184. echo_colored blue "\nUpdating Pi-hole gravity (less verbose)..."
  185. sudo -A pihole -g > /tmp/pihole_gravity.log 2>&1 & show_rainbow_progress "Updating Pi-hole gravity"
  186. if grep -q "FTL is listening" /tmp/pihole_gravity.log; then
  187. echo_colored green "\nPi-hole gravity update completed successfully!"
  188. else
  189. echo_colored red "\nPi-hole gravity update encountered an issue. Check /tmp/pihole_gravity.log for details."
  190. fi
  191.  
  192. # Summarize the log
  193. echo_colored blue "\nSummarizing remote update log..."
  194. grep -E '^(Reading|Building|Calculating|Processing)' $LOG_FILE > $SUMMARY_LOG
  195.  
  196. # Generate summary
  197. echo_colored blue "\nGenerating remote summary report..."
  198. echo -e "Summary of Remote Updates:\n" > $SUMMARY_LOG
  199. if grep -q "Reading package lists..." $LOG_FILE; then
  200. echo -e "Update Applied: Yes" >> $SUMMARY_LOG
  201. else
  202. echo -e "Update Applied: No" >> $SUMMARY_LOG
  203. fi
  204.  
  205. echo -e "\nPi-hole Gravity Log Summary:" >> $SUMMARY_LOG
  206. if grep -q "FTL is listening" /tmp/pihole_gravity.log; then
  207. echo -e "Pi-hole gravity update successful" >> $SUMMARY_LOG
  208. else
  209. echo -e "Pi-hole gravity update had issues" >> $SUMMARY_LOG
  210. fi
  211.  
  212. cat $SUMMARY_LOG
  213.  
  214. echo_colored green "\nRemote Pi-hole update completed successfully!"
  215. EOF
  216.  
  217. # Make the remote script executable
  218. chmod +x $REMOTE_SCRIPT_LOCAL
  219.  
  220. # Update and upgrade functions with cascading rainbow effect
  221. function update_system() {
  222. echo_colored green "Local System Identification:"
  223. get_system_identification
  224.  
  225. echo_colored blue "\nUpdating package list..."
  226. sudo -A apt-get update 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Updating package list"
  227.  
  228. echo_colored blue "\nUpgrading packages..."
  229. sudo -A apt-get upgrade -y 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Upgrading packages"
  230.  
  231. echo_colored blue "\nPerforming distribution upgrade..."
  232. sudo -A apt-get dist-upgrade -y 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Performing distribution upgrade"
  233.  
  234. echo_colored blue "\nRemoving unnecessary packages..."
  235. sudo -A apt-get autoremove -y 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Removing unnecessary packages"
  236.  
  237. echo_colored blue "\nCleaning up..."
  238. sudo -A apt-get clean 2>&1 | tee -a $LOG_FILE & show_rainbow_progress "Cleaning up"
  239. }
  240.  
  241. # Function to execute remote script
  242. function execute_remote_script() {
  243. echo_colored blue "Copying remote update script..."
  244. scp $REMOTE_SCRIPT_LOCAL $REMOTE_USER@$REMOTE_HOST:$REMOTE_SCRIPT_REMOTE
  245.  
  246. echo_colored blue "Executing remote update script..."
  247. ssh -t $REMOTE_USER@$REMOTE_HOST "bash $REMOTE_SCRIPT_REMOTE"
  248. }
  249.  
  250. # Main script execution
  251. echo_colored green "Starting local update..."
  252. update_system
  253.  
  254. echo_colored green "Starting remote update..."
  255. execute_remote_script
  256.  
  257. echo_colored green "Update process completed successfully!"
  258.  
Runtime error #stdin #stdout #stderr 0.01s 5276KB
stdin
Standard input is empty
stdout
Error on line 54
stderr
touch: cannot touch '/tmp/local_update.log': Permission denied
tee: /tmp/local_update.log: Permission denied