restore_backup.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/bin/bash
  2. set -e
  3. version=4.4
  4. date=03/05/2021
  5. pixz_i=""
  6. pigz_i=""
  7. swap_was="0"
  8. ftp_i=0
  9. pv_i=0
  10. cifs_i=0
  11. retry_flag=0
  12. daemon_on=0
  13. echo -e "\e[97mOkcash Blockchain Restore Backup(FTP) v$version ($date)\e[0m"
  14. echo -e "wareck : wareck@gmail.com"
  15. echo -e ""
  16. bt_version="`curl -s http://wareck.free.fr/crypto/okcash/bootstrap_backup/bootstrap_v.txt | awk 'NR==1 {print$3;exit}'`"
  17. bt_parts="`curl -s http://wareck.free.fr/crypto/okcash/bootstrap_backup/bootstrap_v.txt | awk 'NR==2 {print$2; exit}'`"
  18. echo -e "Boostrap.dat file : $bt_version => $bt_parts parts"
  19. function init {
  20. if ps -ef | grep -v grep | grep okcashd >/dev/null
  21. then
  22. daemon_on=1
  23. echo -e "\n\e[93mStopping Okcashd :\e[0m"
  24. okcashd stop 2>/dev/null || true && sudo /etc/init.d/cron stop 2>/dev/null || true
  25. sleep 1
  26. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  27. sleep 1
  28. if [ -f /usr/local/bin/okcashd ];then sudo mv /usr/local/bin/okcashd /usr/local/bin/okcashd_old |true ;fi
  29. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  30. echo "Done."
  31. fi
  32. if [ "`systemctl is-active watchdog.service`" = "active" ]
  33. then
  34. echo -e "\n\e[93mStopping watchdog :\e[0m"
  35. sudo systemctl stop watchdog >/dev/null
  36. echo "Done."
  37. fi
  38. if [ -d /home/$USER/scripts/ledstatus/ ];then sudo python /home/$USER/scripts/ledstatus/led_off.py; fi
  39. if [ -f /var/swap ]
  40. then
  41. echo -e "\n\e[93mStopping Swap :\e[0m"
  42. sudo dphys-swapfile swapoff /var/swap
  43. sudo rm /var/swap
  44. swap_was="1"
  45. echo "Done."
  46. fi
  47. }
  48. function check_software_ {
  49. echo -e "\n\e[95mChecking Softwares :\e[0m"
  50. echo -e -n "Check PIXZ installed : "
  51. if ! [ -x "$(command -v pixz)" ]; then pixz_i="pixz" && echo -e "[\e[91m NO \e[0m]"; else pixz_i="" && echo -e "[\e[92m YES \e[0m]"; fi
  52. echo -e -n "Check PIGZ installed : "
  53. if ! [ -x "$(command -v pigz)" ]; then pigz_i="pigz" && echo -e "[\e[91m NO \e[0m]"; else pigz_i="" && echo -e "[\e[92m YES \e[0m]"; fi
  54. echo -e -n "Check PV installed : "
  55. if ! [ -x "$(command -v pv)" ]; then pv_i="pv" && echo -e "[\e[91m NO \e[0m]"; else pv_i="" && echo -e "[\e[92m YES \e[0m]"; fi
  56. if ! [[ $pixz_i = "" || ! $pv_i = "" || ! $pigz_i = "" ]]
  57. then
  58. echo -e "\n\e[95mSoftwares update & install :\e[0m"
  59. sudo apt-get update
  60. sudo apt install $pixz_i $ftp_i $pv_i $lrzip_i $pigz_i $cifs_i -y
  61. check_software_
  62. fi
  63. echo -e "Done."
  64. }
  65. function freeze_on_ {
  66. NEW_FAN=5.0
  67. if [ -f /home/$USER/scripts/run-fan.py ]
  68. then
  69. echo -e "\n\e[95mEnable \e[38;5;196mF\e[38;5;202mr\e[38;5;208me\e[38;5;214me\e[38;5;220mz\e[38;5;226mi\e[38;5;227mn\e[38;5;229mg\e[0m \e[95m:\e[0m"
  70. sudo systemctl stop run-fan.service
  71. if ! [ -f /home/$USER/freeze.txt ]
  72. then
  73. if grep "# Author: Andreas Spiess" /home/$USER/scripts/run-fan.py >/dev/null
  74. then
  75. grep -i "desiredTemp = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >/home/$USER/freeze.txt
  76. else
  77. grep -i "self.startTemperature = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >/home/$USER/freeze.txt
  78. fi
  79. fi
  80. OLD_FAN=`cat /home/$USER/freeze.txt| awk '{print $1}'`
  81. echo "Fan PWM value : $OLD_FAN => $NEW_FAN"
  82. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  83. then
  84. sed -i -e "s/desiredTemp = "$OLD_FAN"/desiredTemp = "$NEW_FAN"/g" /home/$USER/scripts/run-fan.py
  85. else
  86. sed -i -e "s/self.startTemperature = "$OLD_FAN"/self.startTemperature = "$NEW_FAN"/g" /home/$USER/scripts/run-fan.py
  87. fi
  88. sudo systemctl restart run-fan.service
  89. echo -e "Done."
  90. sleep 1
  91. fi
  92. }
  93. function freeze_off_ {
  94. echo -e -n ""
  95. if [ -f /home/$USER/scripts/run-fan.py ]
  96. then
  97. echo -e "\n\e[95mDisable Freezing :\e[0m"
  98. OLD_FAN=`cat /home/$USER/freeze.txt| awk '{print $1}'`
  99. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  100. then
  101. echo "Fan PWM value : $NEW_FAN => $OLD_FAN"
  102. else
  103. echo "Fan value: $NEW_FAN =>$OLD_FAN"
  104. fi
  105. sudo systemctl stop run-fan.service
  106. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  107. then
  108. sed -i -e "s/desiredTemp = "$NEW_FAN"/desiredTemp = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  109. else
  110. sed -i -e "s/self.startTemperature = "$NEW_FAN"/self.startTemperature = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  111. fi
  112. sudo systemctl restart run-fan.service
  113. if [ -f /home/$USER/freeze.txt ];then rm /home/$USER/freeze.txt;fi
  114. echo -e "Done."
  115. fi
  116. }
  117. function end_restart {
  118. if [ -f /usr/local/bin/okcashd_old ]
  119. then
  120. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  121. okcashd
  122. fi
  123. echo "okcash restart"
  124. }
  125. function download_ {
  126. badsum=0
  127. sudo dphys-swapfile swapoff &> /dev/null
  128. if [ -f /var/swap ];then sudo rm /var/swap;fi
  129. echo -e "\n\e[95mDownload Bootstrap Files $bt_version => $bt_parts :\e[0m"
  130. folder="bootstrap_backup"
  131. cd /home/$USER/
  132. for i in `seq -w 01 $bt_parts`;
  133. do
  134. wget -c http://wareck.free.fr/crypto/okcash/$folder/bootstrap.part$i
  135. done
  136. for i in `seq -w 01 $bt_parts`;
  137. do
  138. wget -c http://wareck.free.fr/crypto/okcash/$folder/bootstrap$i.md5
  139. done
  140. echo -e "\n\e[95mBootstrap checksum test:\e[0m"
  141. for i in `seq -w 01 $bt_parts`;
  142. do
  143. echo -e -n "Bootstrap.part$i md5sum Test: " && if md5sum --status -c bootstrap$i.md5; then echo -e "[\e[92m OK \e[0m]"; else echo -e "[\e[91m NO \e[0m]" && badsum=1 ;fi
  144. if [ $badsum = "1" ]
  145. then
  146. echo -e "\e[38;5;166mBootstrap.tar.xz error !\e[0m"
  147. echo -e "\e[38;5;166mMaybe file damaged or not fully download\e[0m"
  148. echo -e "\e[38;5;166mRestart build_node to try again !\e[0m"
  149. exit
  150. fi
  151. done
  152. echo -e "\n\e[95mJoin bootstrap.tar.xz:\e[0m"
  153. if [ -f bootstrap.tar.xz ] ; then rm bootstrap.tar.xz ;fi
  154. mv bootstrap.part01 bootstrap.tar.xz
  155. echo "bootstrap.part01"
  156. list=$(ls bootstrap.part*)
  157. for i in ${list[@]}
  158. do
  159. cat "$i" >> bootstrap.tar.xz
  160. echo "$i"
  161. rm "$i"
  162. done
  163. rm bootstrap*.md5
  164. echo -e "Done."
  165. if [ -f ~/.okcash/blk0001.dat ]
  166. then
  167. echo -e "\n\e[95mClean old data before extract:\e[0m"
  168. rm -r -f ~/.okcash/blk0001.dat
  169. rm -r -f ~/.okcash/database
  170. rm -r -f ~/.okcash/txleveldb
  171. rm -r -f ~/.okcash/peers.dat
  172. rm -r -f ~/.okcash/smsg*
  173. rm -r -f okcashd.pid
  174. echo "Done."
  175. fi
  176. echo -e "\n\e[95mExtract bootstrap.tar.xz:\e[0m"
  177. tar xvfJ bootstrap.tar.xz
  178. rm bootstrap.tar.xz
  179. echo -e "Done."
  180. sudo dphys-swapfile setup &> /dev/null
  181. sudo dphys-swapfile swapon &> /dev/null
  182. }
  183. if ps -ef | grep -v grep | grep okcashd >/dev/null
  184. then
  185. echo -e "\n\e[38;5;166mOKcash daemon is working => shutdown and restart during this process !...\e[0m"
  186. while true; do
  187. read -p "Do you want to start processing (y/n) ? " yn
  188. case $yn in
  189. [Yy]* ) init ; check_software_; freeze_on ; retry ; packit ; upload ; end_restart ; break;;
  190. [Nn]* ) exit;;
  191. * ) echo "Please answer yes or no.";;
  192. esac
  193. done
  194. else
  195. init
  196. check_software_
  197. freeze_on_
  198. download_
  199. end_restart
  200. fi
  201. if [ swap_was = 1 ]
  202. then
  203. echo -e "\n\e[95mRestart Swap:\e[0m"
  204. sudo dphys-swapfile setup
  205. sudo dphys-swapfile swapon
  206. echo "Done."
  207. fi
  208. if [ -f /home/$USER/bootstrap_uploadpass ]; then rm /home/$USER/bootstrap_uploadpass;fi
  209. if [ $daemon_on = 1 ]
  210. then
  211. echo -e "\n\e[38;5;166mOKcash daemon is restarting ...\e[0m"
  212. if [ -f /usr/local/bin/okcashd_old ]
  213. then
  214. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  215. fi
  216. fi
  217. echo ""