upload_mega.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. split_size=500
  14. echo -e "\e[97mOkcash Blockchain Uploader v$version ($date)\e[0m"
  15. echo -e "Update for Mega.io. upload"
  16. echo -e "wareck : wareck@gmail.com"
  17. echo -e ""
  18. sleep 2
  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 2
  26. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  27. sleep 2
  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 LFTP installed : "
  55. 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
  56. echo -e -n "Check CIFS installed : "
  57. if ! [ -x "$(command -v cifscreds)" ]; then cifs_i="cifs-utils" && echo -e "[\e[91m NO \e[0m]"; else cifs_i="" && echo -e "[\e[92m YES \e[0m]"; fi
  58. echo -e -n "Check PV installed : "
  59. 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
  60. if [[ ! $ftp_i = "" || ! $pixz_i = "" || ! $pv_i = "" || ! $pigz_i = "" || ! $cifs_i = "" ]]
  61. then
  62. echo -e "\n\e[95mSoftwares update & install :\e[0m"
  63. sudo apt-get update
  64. sudo apt install $pixz_i $ftp_i $pv_i $lrzip_i $pigz_i $cifs_i -y
  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-1024))
  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[95mSplitting Files (${split_size}M):\e[0m"
  132. split --verbose --numeric-suffixes=1 -b ${split_size}M bootstrap.tar.xz "bootstrap.part"
  133. rm bootstrap.tar.xz
  134. sleep 1
  135. echo "Done."
  136. echo -e "\n\e[95mMake bootstrap_v.txt:\e[0m"
  137. if [ -f bootstrap_v.txt ]; then rm bootstrap_v.txt;fi
  138. Raw_date="`date +%d.%m.%Y`"
  139. YYYY=`ls -l bootstrap*.part* | wc -l`
  140. cat <<'EOF'>> bootstrap_v.txt
  141. Bootstrap : XXXX
  142. Parts: YYYY
  143. EOF
  144. sed -i -e "s/XXXX/$Raw_date/g" bootstrap_v.txt
  145. sed -i -e "s/YYYY/$YYYY/g" bootstrap_v.txt
  146. echo "Done."
  147. echo -e "\n\e[95mMd5sum Files:\e[0m"
  148. zap=$(ls -l bootstrap.part* | wc -l)
  149. for i in `seq -w 01 $zap`
  150. do
  151. echo -e -n "md5sum Bootstrap.part$i: " && md5sum bootstrap.part$i > bootstrap$i.md5 && echo -e "\e[1mDone.\e[0m"
  152. done
  153. touch /home/$USER/bootstrap_uploadpass
  154. }
  155. function ftp_check {
  156. FTP_SUCCESS_MSG="226 Transfer complete"
  157. if fgrep "$FTP_SUCCESS_MSG" $Ftp_LOG ;then
  158. echo "ftp transfer : OK"
  159. echo ""
  160. rm $Ftp_LOG
  161. else
  162. echo "ftp transfer Error: $ftp_file"
  163. rm $Ftp_LOG
  164. echo ""
  165. ftp_loop
  166. fi
  167. }
  168. function ftp_loop {
  169. lftp -u wareck,zorn692611 ftpperso.free.fr <<EOF
  170. put $ftp_file -o /crypto/okcash/bootstrap/$ftp_file
  171. EOF
  172. echo -n $ftp_file && echo -e ": \e[1mDone.\e[0m";
  173. }
  174. function upload {
  175. cd /home/$USER
  176. echo -e "\n\e[95mUpload files to server :\e[0m"
  177. ftp_file=bootstrap_v.txt && ftp_loop
  178. zap=$(ls -l bootstrap.part* | wc -l)
  179. for j in `seq -w 01 $zap`
  180. do
  181. ftp_file=bootstrap$j.md5
  182. ftp_loop
  183. done
  184. echo -e "\n\e[95mUpload files to Freebox :\e[0m"
  185. if ! [ -d /tmp/bootstrap ]; then mkdir /tmp/bootstrap ;fi
  186. if mount | grep -i "//mafreebox.freebox.fr/" >/dev/null
  187. then
  188. sudo umount /tmp/bootstrap/
  189. fi
  190. sudo mount -t cifs //mafreebox.freebox.fr/Disque\ dur/ /tmp/bootstrap/ -o user=freebox,password=password,uid=1000,gid=1000,rw,vers=1.0
  191. if ! [ -d /tmp/bootstrap/bootstrap ]; then mkdir /tmp/bootstrap/bootstrap ;fi
  192. cp -v bootstrap.part* /tmp/bootstrap/bootstrap/
  193. cp -v bootstrap_v.txt /tmp/bootstrap/bootstrap/
  194. cp -v bootstrap*.md5 /tmp/bootstrap/bootstrap/
  195. sleep 5
  196. sudo umount /tmp/bootstrap
  197. freeze_off
  198. echo -e "\n\e[92mUpload Done ...\n\e[0m"
  199. }
  200. function end_restart {
  201. if [ -f /usr/local/bin/okcashd_old ]
  202. then
  203. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  204. okcashd
  205. fi
  206. }
  207. function retry {
  208. if [ -f /home/$USER/bootstrap_uploadpass ]; then retry_flag=1 ;fi
  209. if [ $retry_flag = "1" ]
  210. then
  211. echo -e "\n\e[95mRetry Upload to server :\e[0m"
  212. echo -e "Old files are still present."
  213. echo -e "If you want to force restart build/upload"
  214. echo -e "remove files by using this command:"
  215. echo -e ""
  216. echo -e "rm -r -f /home/$USER/bootstrap.tar.xz /home/$USER/bootstrap_v.txt /home/$USER/bootstrap*.md5 /home/$USER/bootstrap_uploadpass"
  217. sleep 5
  218. fi
  219. }
  220. if ps -ef | grep -v grep | grep okcashd >/dev/null
  221. then
  222. echo -e "\n\e[38;5;166mOKcash daemon is working => shutdown and restart during this process !...\e[0m"
  223. while true; do
  224. read -p "Do you want to start processing (y/n) ? " yn
  225. case $yn in
  226. [Yy]* ) init ; check_software_; freeze_on ; retry ; packit ; upload ; end_restart ; break;;
  227. [Nn]* ) exit;;
  228. * ) echo "Please answer yes or no.";;
  229. esac
  230. done
  231. else
  232. init
  233. check_software_
  234. freeze_on
  235. retry
  236. if [ $retry_flag = "1" ]
  237. then
  238. upload
  239. end_restart
  240. else
  241. packit
  242. upload
  243. end_restart
  244. fi
  245. fi
  246. if [ swap_was = 1 ]
  247. then
  248. echo -e "\n\e[95mRestart Swap:\e[0m"
  249. sudo dphys-swapfile setup
  250. sudo dphys-swapfile swapon
  251. echo "Done."
  252. fi
  253. if [ -f /home/$USER/bootstrap_uploadpass ]; then rm /home/$USER/bootstrap_uploadpass;fi
  254. if [ $daemon_on = 1 ]
  255. then
  256. echo -e "\n\e[38;5;166mOKcash daemon is restarting ...\e[0m"
  257. if [ -f /usr/local/bin/okcashd_old ]
  258. then
  259. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  260. fi
  261. fi
  262. echo ""