upload_mega.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. cifs_i=0
  11. retry_flag=0
  12. daemon_on=0
  13. echo -e "\e[97mOkcash Blockchain Uploader v$version ($date)\e[0m"
  14. echo -e "Update for Mega.io...WIP"
  15. echo -e "wareck : wareck@gmail.com"
  16. echo -e ""
  17. sleep 2
  18. function init {
  19. if ps -ef | grep -v grep | grep okcashd >/dev/null
  20. then
  21. daemon_on=1
  22. echo -e "\n\e[93mStopping Okcashd :\e[0m"
  23. okcashd stop 2>/dev/null || true && sudo /etc/init.d/cron stop 2>/dev/null || true
  24. sleep 2
  25. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  26. sleep 2
  27. if [ -f /usr/local/bin/okcashd ];then sudo mv /usr/local/bin/okcashd /usr/local/bin/okcashd_old |true ;fi
  28. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  29. echo "Done."
  30. fi
  31. if [ "`systemctl is-active watchdog.service`" = "active" ]
  32. then
  33. echo -e "\n\e[93mStopping watchdog :\e[0m"
  34. sudo systemctl stop watchdog >/dev/null
  35. echo "Done."
  36. fi
  37. if [ -d /home/$USER/scripts/ledstatus/ ];then sudo python /home/$USER/scripts/ledstatus/led_off.py; fi
  38. if [ -f /var/swap ]
  39. then
  40. echo -e "\n\e[93mStopping Swap :\e[0m"
  41. sudo dphys-swapfile swapoff /var/swap
  42. sudo rm /var/swap
  43. swap_was="1"
  44. echo "Done."
  45. fi
  46. }
  47. function check_software_ {
  48. echo -e "\n\e[95mChecking Softwares :\e[0m"
  49. echo -e -n "Check PIXZ installed : "
  50. 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
  51. echo -e -n "Check PIGZ installed : "
  52. 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
  53. echo -e -n "Check LFTP installed : "
  54. 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
  55. echo -e -n "Check PV installed : "
  56. 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
  57. echo -e -n "Check CIFS installed : "
  58. if ! [ -x "$(command -v mount.cifs)" ]; then cifs_i="cifs-utils" && echo -e "[\e[91m NO \e[0m]"; else cifs_i="" && echo -e "[\e[92m YES \e[0m]"; fi
  59. if [[ ! $ftp_i = "" || ! $pixz_i = "" || ! $pv_i = "" || ! $pigz_i = "" || !$cifs_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 $cifs_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+1))
  127. echo -e "\e[97mTar & PiXz :\e[0m"
  128. tar cf - .okcash/txleveldb/* .okcash/blk0001.dat | pv -s ${SIZE}k | pixz > bootstrap.tar.xz
  129. sleep 1
  130. echo "Done."
  131. echo -e "\n\e[95mMake bootstrap_v.txt:\e[0m"
  132. Raw_date="`date +%d.%m.%Y`"
  133. cat <<'EOF'>> bootstrap_v.txt
  134. Bootstrap : XXXX
  135. Compressed with : tar / xz
  136. Link :
  137. EOF
  138. sed -i -e "s/XXXX/$Raw_date/g" bootstrap_v.txt
  139. echo "Done."
  140. echo -e "\n\e[95mMd5sum Files:\e[0m"
  141. if [ -f bootstrap.tar.xz ]; then echo -e -n "md5sum Bootstrap.tar.xz: " && md5sum bootstrap.tar.xz > bootstrap.md5 && echo -e "\e[1mDone.\e[0m"; fi
  142. touch /home/$USER/bootstrap_uploadpass
  143. }
  144. function ftp_check {
  145. FTP_SUCCESS_MSG="226 Transfer complete"
  146. if fgrep "$FTP_SUCCESS_MSG" $Ftp_LOG ;then
  147. echo "ftp transfer : OK"
  148. echo ""
  149. rm $Ftp_LOG
  150. else
  151. echo "ftp transfer Error: $ftp_file"
  152. rm $Ftp_LOG
  153. echo ""
  154. ftp_loop
  155. fi
  156. }
  157. function ftp_loop {
  158. lftp -u wareck,zorn692611 ftpperso.free.fr <<EOF
  159. put $ftp_file -o /crypto/okcash/bootstrap/$ftp_file
  160. EOF
  161. echo -n $ftp_file && echo -e ": \e[1mDone.\e[0m";
  162. }
  163. function upload {
  164. cd /home/$USER
  165. echo -e "\n\e[95mUpload files to server :\e[0m"
  166. if [ -f bootstrap_v.txt ];then ftp_file=bootstrap_v.txt && ftp_loop ;fi
  167. if [ -f bootstrap.md5 ];then ftp_file=bootstrap.md5 && ftp_loop ;fi
  168. if ! [ -d /tmp/bootstrap ]; then mkdir /tmp/bootstrap ;fi
  169. sudo mount -t cifs //mafreebox.freebox.fr/Disque\ dur/ /tmp/bootstrap/ -o user=freebox,password=password,uid=1000,gid=1000,rw,vers=1.0
  170. if ! [ -d /tmp/bootstrap/bootstrap ]; then mkdir /tmp/bootstrap/bootstrap ;fi
  171. cp -v bootstrap.tar.xz /tmp/bootstrap/bootstrap/
  172. cp -v bootstrap_v.txt /tmp/bootstrap/bootstrap/
  173. cp -v bootstrap.md5 /tmp/bootstrap/bootstrap/
  174. sleep 5
  175. sudo umount /tmp/bootstrap
  176. freeze_off
  177. echo -e "\n\e[92mUpload Done ...\n\e[0m"
  178. }
  179. function end_restart {
  180. if [ -f /usr/local/bin/okcashd_old ]
  181. then
  182. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  183. okcashd
  184. fi
  185. }
  186. function retry {
  187. if [ -f /home/$USER/bootstrap_uploadpass ]; then retry_flag=1 ;fi
  188. if [ $retry_flag = "1" ]
  189. then
  190. echo -e "\n\e[95mRetry Upload to server :\e[0m"
  191. echo -e "Old files are still present."
  192. echo -e "If you want to force restart build/upload"
  193. echo -e "remove files by using this command:"
  194. echo -e ""
  195. echo -e "rm -r -f /home/$USER/bootstrap.tar.xz /home/$USER/bootstrap_v.txt /home/$USER/bootstrap*.md5 /home/$USER/bootstrap_uploadpass"
  196. sleep 5
  197. fi
  198. }
  199. if ps -ef | grep -v grep | grep okcashd >/dev/null
  200. then
  201. echo -e "\n\e[38;5;166mOKcash daemon is working => shutdown and restart during this process !...\e[0m"
  202. while true; do
  203. read -p "Do you want to start processing (y/n) ? " yn
  204. case $yn in
  205. [Yy]* ) init ; check_software_; freeze_on ; retry ; packit ; upload ; end_restart ; break;;
  206. [Nn]* ) exit;;
  207. * ) echo "Please answer yes or no.";;
  208. esac
  209. done
  210. else
  211. init
  212. check_software_
  213. freeze_on
  214. retry
  215. if [ $retry_flag = "1" ]
  216. then
  217. upload
  218. end_restart
  219. else
  220. packit
  221. upload
  222. end_restart
  223. fi
  224. fi
  225. if [ swap_was = 1 ]
  226. then
  227. echo -e "\n\e[95mRestart Swap:\e[0m"
  228. sudo dphys-swapfile setup
  229. sudo dphys-swapfile swapon
  230. echo "Done."
  231. fi
  232. if [ -f /home/$USER/bootstrap_uploadpass ]; then rm /home/$USER/bootstrap_uploadpass;fi
  233. if [ $daemon_on = 1 ]
  234. then
  235. echo -e "\n\e[38;5;166mOKcash daemon is restarting ...\e[0m"
  236. if [ -f /usr/local/bin/okcashd_old ]
  237. then
  238. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  239. fi
  240. fi
  241. echo ""