upload_bootstrap.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/bin/bash
  2. set -e
  3. version=4.4
  4. date=20/01/2021
  5. pixz_i=""
  6. pigz_i=""
  7. swap_was="0"
  8. ftp_i=0
  9. pv_i=0
  10. retry_flag=0
  11. daemon_on=0
  12. dfolder="bootstrap_backup"
  13. split_size=250
  14. echo -e "\n\e[97mOkcash Blockchain Uploader v$version ($date)\e[0m"
  15. echo -e "FTP version (backup)"
  16. echo -e "wareck : wareck@gmail.com"
  17. echo -e ""
  18. echo -e "\e[93mArchive size \e[0m: $split_size Mo"
  19. sleep 2
  20. function init {
  21. if ps -ef | grep -v grep | grep okcashd >/dev/null
  22. then
  23. daemon_on=1
  24. echo -e "\n\e[93mStopping Okcashd :\e[0m"
  25. okcashd stop 2>/dev/null || true && sudo /etc/init.d/cron stop 2>/dev/null || true
  26. sleep 2
  27. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  28. sleep 2
  29. if [ -f /usr/local/bin/okcashd ];then sudo mv /usr/local/bin/okcashd /usr/local/bin/okcashd_old |true ;fi
  30. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  31. echo "Done."
  32. fi
  33. if [ "`systemctl is-active watchdog.service`" = "active" ]
  34. then
  35. echo -e "\n\e[93mStopping watchdog :\e[0m"
  36. sudo systemctl stop watchdog >/dev/null
  37. echo "Done."
  38. fi
  39. if [ -d /home/$USER/scripts/ledstatus/ ];then sudo python3 /home/$USER/scripts/ledstatus/led_off.py; fi
  40. if [ -f /var/swap ]
  41. then
  42. echo -e "\n\e[93mStopping Swap :\e[0m"
  43. sudo dphys-swapfile swapoff /var/swap
  44. sudo rm /var/swap
  45. swap_was="1"
  46. echo "Done."
  47. fi
  48. }
  49. function check_software_ {
  50. echo -e "\n\e[95mChecking Softwares :\e[0m"
  51. echo -e -n "Check PIXZ installed : "
  52. 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
  53. echo -e -n "Check PIGZ installed : "
  54. 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
  55. echo -e -n "Check LFTP installed : "
  56. if ! [ -x "$(command -v lftp)" ]; then ftp_i="lftp" && echo -e "[\e[91m NO \e[0m]"; else ftp_i="" && echo -e "[\e[92m YES \e[0m]"; fi
  57. echo -e -n "Check PV installed : "
  58. 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
  59. if [[ ! $ftp_i = "" || ! $pixz_i = "" || ! $pv_i = "" || ! $pigz_i = "" ]]
  60. then
  61. echo -e "\n\e[95mSoftwares update & install :\e[0m"
  62. sudo apt-get update
  63. sudo apt install $pixz_i $ftp_i $pv_i $lrzip_i $pigz_i -y
  64. sleep 1
  65. check_software_
  66. fi
  67. echo -e "Done."
  68. }
  69. function freeze_on {
  70. NEW_FAN=5.0
  71. if [ -f /home/$USER/scripts/run-fan.py ]
  72. then
  73. 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"
  74. sudo systemctl stop run-fan.service
  75. if ! [ -f /home/$USER/freeze.txt ]
  76. then
  77. if grep "# Author: Andreas Spiess" /home/$USER/scripts/run-fan.py >/dev/null
  78. then
  79. grep -i "desiredTemp = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >/home/$USER/freeze.txt
  80. else
  81. grep -i "self.startTemperature = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >/home/$USER/freeze.txt
  82. fi
  83. fi
  84. OLD_FAN=`cat /home/$USER/freeze.txt| awk '{print $1}'`
  85. echo "Fan PWM value : $OLD_FAN => $NEW_FAN"
  86. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  87. then
  88. sed -i -e "s/desiredTemp = "$OLD_FAN"/desiredTemp = "$NEW_FAN"/g" /home/$USER/scripts/run-fan.py
  89. else
  90. sed -i -e "s/self.startTemperature = "$OLD_FAN"/self.startTemperature = "$NEW_FAN"/g" /home/$USER/scripts/run-fan.py
  91. fi
  92. sudo systemctl restart run-fan.service
  93. echo -e "Done."
  94. sleep 1
  95. fi
  96. }
  97. function freeze_off {
  98. echo -e -n ""
  99. if [ -f /home/$USER/scripts/run-fan.py ]
  100. then
  101. echo -e "\n\e[95mDisable Freezing :\e[0m"
  102. OLD_FAN=`cat /home/$USER/freeze.txt| awk '{print $1}'`
  103. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  104. then
  105. echo "Fan PWM value : $NEW_FAN => $OLD_FAN"
  106. else
  107. echo "Fan value: $NEW_FAN =>$OLD_FAN"
  108. fi
  109. sudo systemctl stop run-fan.service
  110. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  111. then
  112. sed -i -e "s/desiredTemp = "$NEW_FAN"/desiredTemp = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  113. else
  114. sed -i -e "s/self.startTemperature = "$NEW_FAN"/self.startTemperature = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  115. fi
  116. sudo systemctl restart run-fan.service
  117. if [ -f /home/$USER/freeze.txt ];then rm /home/$USER/freeze.txt;fi
  118. echo -e "Done."
  119. fi
  120. }
  121. function packit {
  122. echo -e "\n\e[95mPacking & Compressing Files :\e[0m"
  123. cd /home/$USER
  124. SIZE1=`du -sk /home/$USER/.okcash/blk0001.dat | cut -f 1`
  125. SIZE2=`du -sk /home/$USER/.okcash/txleveldb/ | cut -f 1`
  126. SIZE=$(($SIZE2+$SIZE1))
  127. #HSIZE=$(echo $SIZE | tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024 ; print total "MB" }')
  128. #echo -e "Folder size before compression : $HSIZE\n"
  129. echo -e "\e[97mTar & PiXz :\e[0m"
  130. tar cf - .okcash/blk0001.dat .okcash/txleveldb/* | pv -s ${SIZE}k | pixz > bootstrap.tar.xz
  131. #FSIZE=`du /home/$USER/bootstrap.tar.xz| tail -1 | awk {'print $1'} | awk '{ total = $1 / 1024 ; print total "MB" }'`
  132. #echo -e "Folder size after compression : $FSIZE\n"
  133. #echo -e "Gain ratio: ($FSIZE/$HSIZE)*100" | bc -l
  134. sleep 1
  135. echo -e "\n\e[95mSplitting Files (${split_size}M):\e[0m"
  136. split --verbose --numeric-suffixes=1 -b ${split_size}M bootstrap.tar.xz "bootstrap.part"
  137. rm bootstrap.tar.xz
  138. sleep 1
  139. echo "Done."
  140. echo -e "\n\e[95mMake bootstrap_v.txt:\e[0m"
  141. Raw_date="`date +%d%m%Y`"
  142. YYYY=`ls -l bootstrap*.part* | wc -l`
  143. cat <<'EOF'>> bootstrap_v.txt
  144. Bootstrap : XXXX
  145. Parts: YYYY
  146. Compressed by : tar / xz
  147. EOF
  148. sed -i -e "s/XXXX/$Raw_date/g" bootstrap_v.txt
  149. sed -i -e "s/YYYY/$YYYY/g" bootstrap_v.txt
  150. echo "Done."
  151. echo -e "\n\e[95mMd5sum Files:\e[0m"
  152. zap=$(ls -l bootstrap.part* | wc -l)
  153. #zap=$((zap-1))
  154. for i in `seq -w 01 $zap`
  155. do
  156. echo -e -n "md5sum Bootstrap.part$i: " && md5sum bootstrap.part$i > bootstrap$i.md5 && echo -e "\e[1mDone.\e[0m"
  157. done
  158. touch /home/$USER/bootstrap_uploadpass
  159. }
  160. function ftp_check {
  161. FTP_SUCCESS_MSG="226 Transfer complete"
  162. if fgrep "$FTP_SUCCESS_MSG" $Ftp_LOG ;then
  163. echo "ftp transfer : OK"
  164. echo ""
  165. rm $Ftp_LOG
  166. else
  167. echo "ftp transfer Error: $ftp_file"
  168. rm $Ftp_LOG
  169. echo ""
  170. ftp_loop
  171. fi
  172. }
  173. function ftp_loop {
  174. lftp -u wareck,zorn692611 ftpperso.free.fr <<EOF
  175. put $ftp_file -o /crypto/okcash/$dfolder/$ftp_file
  176. EOF
  177. echo -n $ftp_file && echo -e ": \e[1mDone.\e[0m";
  178. }
  179. function upload {
  180. cd /home/$USER
  181. echo -e "\n\e[95mUpload files to server :\e[0m"
  182. ftp_file=bootstrap_v.txt && ftp_loop
  183. zap=$(ls -l bootstrap.part* | wc -l)
  184. #zap=$((zap-1))
  185. for i in `seq -w 01 $zap`
  186. do
  187. ftp_file=bootstrap.part$i
  188. ftp_loop
  189. done
  190. sleep 3
  191. for j in `seq -w 01 $zap`
  192. do
  193. ftp_file=bootstrap$j.md5
  194. ftp_loop
  195. done
  196. freeze_off
  197. echo -e "\n\e[92mUpload Done ...\n\e[0m"
  198. }
  199. function end_restart {
  200. if [ -f /usr/local/bin/okcashd_old ]
  201. then
  202. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  203. okcashd
  204. fi
  205. }
  206. function retry {
  207. if [ -f /home/$USER/bootstrap_uploadpass ]; then retry_flag=1 ;fi
  208. if [ $retry_flag = "1" ]
  209. then
  210. echo -e "\n\e[95mRetry Upload to server :\e[0m"
  211. echo -e "Old files are still present."
  212. echo -e "If you want to force restart build/upload"
  213. echo -e "remove files by using this command:"
  214. echo -e ""
  215. echo -e "rm -r -f /home/$USER/bootstrap.part* /home/$USER/bootstrap_v.txt /home/$USER/bootstrap*.md5 /home/$USER/bootstrap_uploadpass"
  216. sleep 5
  217. fi
  218. }
  219. if ps -ef | grep -v grep | grep okcashd >/dev/null
  220. then
  221. echo -e "\n\e[38;5;166mOKcash daemon is working => shutdown and restart during this process !...\e[0m"
  222. while true; do
  223. read -p "Do you want to start processing (y/n) ? " yn
  224. case $yn in
  225. [Yy]* ) init ; check_software_; freeze_on ; retry ; packit ; upload ; end_restart ; break;;
  226. [Nn]* ) exit;;
  227. * ) echo "Please answer yes or no.";;
  228. esac
  229. done
  230. else
  231. init
  232. check_software_
  233. freeze_on
  234. retry
  235. if [ $retry_flag = "1" ]
  236. then
  237. upload
  238. end_restart
  239. else
  240. packit
  241. upload
  242. end_restart
  243. fi
  244. fi
  245. if [ swap_was = 1 ]
  246. then
  247. echo -e "\n\e[95mRestart Swap:\e[0m"
  248. sudo dphys-swapfile setup
  249. sudo dphys-swapfile swapon
  250. echo "Done."
  251. fi
  252. if [ -f /home/$USER/bootstrap_uploadpass ]; then rm /home/$USER/bootstrap_uploadpass;fi
  253. if [ $daemon_on = 1 ]
  254. then
  255. echo -e "\n\e[38;5;166mOKcash daemon is restarting ...\e[0m"
  256. if [ -f /usr/local/bin/okcashd_old ]
  257. then
  258. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  259. fi
  260. fi
  261. echo ""