upload_mega.sh 9.1 KB

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