upload_mega.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/bin/bash
  2. set -e
  3. version=6.0
  4. date=26/12/2022
  5. pi7z_i=""
  6. swap_was="0"
  7. ftp_i=0
  8. pv_i=0
  9. cifs_i=0
  10. retry_flag=0
  11. daemon_on=0
  12. split_size=270
  13. OSV=$(sed 's/\..*//' /etc/debian_version)
  14. DietPi_="NO"
  15. if [ $OSV = 8 ];then img_v="Jessie";fi
  16. if [ $OSV = 9 ];then img_v="Strecth"; fi
  17. if [ $OSV = 10 ];then img_v="Buster"; fi
  18. if [ $OSV = 11 ];then img_v="Bullseye"; fi
  19. echo -e "$(tput setaf 5)"
  20. echo -e " ╔╗ ╔═╗╔═╗╔╦╗╔═╗╔╦╗╦═╗╔═╗╔═╗"
  21. echo -e " ╠╩╗║ ║║ ║ ║ ╚═╗ ║ ╠╦╝╠═╣╠═╝"
  22. echo -e " ╚═╝╚═╝╚═╝ ╩ ╚═╝ ╩ ╩╚═╩ ╩╩ "
  23. echo -e " ╔╗ ╦ ╦╦╦ ╔╦╗╔═╗╦═╗ "
  24. echo -e " ╠╩╗║ ║║║ ║║║╣ ╠╦╝ "
  25. echo -e " ╚═╝╚═╝╩╩═╝═╩╝╚═╝╩╚═ "
  26. echo -e ""
  27. echo -e "\e[97mOkcash Blockchain Uploader v$version ($date)\e[0m"
  28. echo -e "Author : wareck@gmail.com"
  29. echo -e ""
  30. echo -e "Raspbian : $img_v"
  31. echo -e "Upload : Mega.nz"
  32. echo -e "Format : 7z"
  33. echo -e "Packet Size : $split_size Mo"
  34. echo -e ""
  35. sleep 4
  36. function init {
  37. if ps -ef | grep -v grep | grep okcashd >/dev/null
  38. then
  39. daemon_on=1
  40. echo -e "\n\e[93mStopping Okcashd :\e[0m"
  41. okcashd stop 2>/dev/null || true && sudo /etc/init.d/cron stop 2>/dev/null || true
  42. sleep 2
  43. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  44. sleep 2
  45. if [ -f /usr/local/bin/okcashd ];then sudo mv /usr/local/bin/okcashd /usr/local/bin/okcashd_old |true ;fi
  46. if ps -ef | grep -v grep | grep okcashd >/dev/null ; then killall -9 okcashd; fi
  47. echo "Done."
  48. fi
  49. if [ "`systemctl is-active watchdog.service`" = "active" ]
  50. then
  51. echo -e "\n\e[93mStopping watchdog :\e[0m"
  52. sudo systemctl stop watchdog >/dev/null
  53. echo "Done."
  54. fi
  55. if [ -d /home/$USER/scripts/ledstatus/ ]
  56. then
  57. case $OSV in
  58. 11)
  59. sudo python3 /home/$USER/scripts/ledstatus/led_off.py ;;
  60. *)
  61. sudo python /home/$USER/scripts/ledstatus/led_off.py ;;
  62. esac
  63. fi
  64. if [ -f /var/swap ]
  65. then
  66. echo -e "\n\e[93mStopping Swap :\e[0m"
  67. sudo dphys-swapfile swapoff /var/swap
  68. sudo rm /var/swap
  69. swap_was="1"
  70. echo "Done."
  71. fi
  72. }
  73. function check_software_ {
  74. echo -e "\n\e[95mChecking Softwares :\e[0m"
  75. echo -e -n "Check PI7Z installed : "
  76. if ! [ -x "$(command -v 7z)" ]; then pi7z_i="p7zip-full" && echo -e "[\e[91m NO \e[0m]"; else pi7z_i="" && echo -e "[\e[92m YES \e[0m]"; fi
  77. echo -e -n "Check LFTP installed : "
  78. 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
  79. echo -e -n "Check CIFS installed : "
  80. 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
  81. echo -e -n "Check PV installed : "
  82. 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
  83. if [[ ! $ftp_i = "" || ! $pi7z_i = "" || ! $pv_i = "" || ! $cifs_i = "" ]]
  84. then
  85. echo -e "\n\e[95mSoftwares update & install :\e[0m"
  86. sudo apt-get update
  87. sudo apt install $pi7z_i $ftp_i $pv_i $cifs_i cifs-utils -y
  88. check_software_
  89. fi
  90. echo -e "Done."
  91. }
  92. function freeze_on {
  93. NEW_FAN=5
  94. NEW_THR=4
  95. if [ -f /home/$USER/scripts/run-fan.py ]
  96. then
  97. 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"
  98. sudo systemctl stop run-fan.service
  99. if ! [ -f /home/$USER/freeze.txt ]
  100. then
  101. if grep "ON_THRESHOLD" /home/$USER/scripts/run-fan.py >/dev/null
  102. then
  103. grep -i "ON_THRESHOLD = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >/home/$USER/freeze.txt
  104. grep -i "OFF_THRESHOLD = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >>/home/$USER/freeze.txt
  105. fi
  106. fi
  107. OLD_FAN=`cat /home/$USER/freeze.txt| awk 'NR==1 {print $1;exit}'`
  108. OLD_THR=`cat /home/$USER/freeze.txt| awk 'NR==2 {print $1;exit}'`
  109. echo "Fan PWM value : $OLD_FAN => $NEW_FAN"
  110. echo "Fan Threshold : $OLD_THR => $NEW_THR"
  111. if grep "ON_THRESHOLD = " /home/$USER/scripts/run-fan.py >/dev/null
  112. then
  113. sed -i -e "s/ON_THRESHOLD = "$OLD_FAN"/ON_THRESHOLD = "$NEW_FAN"/g" /home/$USER/scripts/run-fan.py
  114. sed -i -e "s/OFF_THRESHOLD = "$OLD_THR"/OFF_THRESHOLD = "$NEW_THR"/g" /home/$USER/scripts/run-fan.py
  115. fi
  116. sudo systemctl restart run-fan.service
  117. echo -e "Done."
  118. fi
  119. }
  120. function freeze_off {
  121. echo -e -n ""
  122. if [ -f /home/$USER/scripts/run-fan.py ]
  123. then
  124. echo -e "\n\e[95mDisable Freezing :\e[0m"
  125. OLD_FAN=`cat /home/$USER/freeze.txt| awk 'NR==1 {print $1;exit}'`
  126. OLD_THR=`cat /home/$USER/freeze.txt| awk 'NR==2 {print $1;exit}'`
  127. if grep "ON_THRESHOLD" /home/$USER/scripts/run-fan.py >/dev/null
  128. then
  129. echo "Fan PWM value : $NEW_FAN => $OLD_FAN"
  130. echo "Fan Threshold : $NEW_THR => $OLD_THR"
  131. else
  132. echo "Fan value: $NEW_FAN =>$OLD_FAN"
  133. fi
  134. sudo systemctl stop run-fan.service
  135. if grep "ON_THRESHOLD" /home/$USER/scripts/run-fan.py >/dev/null
  136. then
  137. sed -i -e "s/ON_THRESHOLD = "$NEW_FAN"/ON_THRESHOLD = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  138. sed -i -e "s/OFF_THRESHOLD = "$NEW_THR"/OFF_THRESHOLD = "$OLD_THR"/g" /home/$USER/scripts/run-fan.py
  139. else
  140. sed -i -e "s/self.startTemperature = "$NEW_FAN"/self.startTemperature = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  141. fi
  142. sudo systemctl restart run-fan.service
  143. if [ -f /home/$USER/freeze.txt ];then rm /home/$USER/freeze.txt;fi
  144. echo -e "Done."
  145. fi
  146. }
  147. function freeze_on_old {
  148. NEW_FAN=5.0
  149. if [ -f /home/$USER/scripts/run-fan.py ]
  150. then
  151. 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"
  152. sudo systemctl stop run-fan.service
  153. if ! [ -f /home/$USER/freeze.txt ]
  154. then
  155. if grep "# Author: Andreas Spiess" /home/$USER/scripts/run-fan.py >/dev/null
  156. then
  157. grep -i "desiredTemp = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >/home/$USER/freeze.txt
  158. else
  159. grep -i "self.startTemperature = " /home/$USER/scripts/run-fan.py |awk 'NR==1 {print$3;exit}' >/home/$USER/freeze.txt
  160. fi
  161. fi
  162. OLD_FAN=`cat /home/$USER/freeze.txt | awk '{print $1}'`
  163. echo "Fan PWM value : $OLD_FAN => $NEW_FAN"
  164. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  165. then
  166. sed -i -e "s/desiredTemp = "$OLD_FAN"/desiredTemp = "$NEW_FAN"/g" /home/$USER/scripts/run-fan.py
  167. else
  168. sed -i -e "s/self.startTemperature = "$OLD_FAN"/self.startTemperature = "$NEW_FAN"/g" /home/$USER/scripts/run-fan.py
  169. fi
  170. sudo systemctl restart run-fan.service
  171. echo -e "Done."
  172. sleep 1
  173. fi
  174. }
  175. function freeze_off_old {
  176. echo -e -n ""
  177. if [ -f /home/$USER/scripts/run-fan.py ]
  178. then
  179. echo -e "\n\e[95mDisable Freezing :\e[0m"
  180. OLD_FAN=`cat /home/$USER/freeze.txt| awk '{print $1}'`
  181. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  182. then
  183. echo "Fan PWM value : $NEW_FAN => $OLD_FAN"
  184. else
  185. echo "Fan value: $NEW_FAN =>$OLD_FAN"
  186. fi
  187. sudo systemctl stop run-fan.service
  188. if grep "# Author: Andreas Spiess" ~/scripts/run-fan.py >/dev/null
  189. then
  190. sed -i -e "s/desiredTemp = "$NEW_FAN"/desiredTemp = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  191. else
  192. sed -i -e "s/self.startTemperature = "$NEW_FAN"/self.startTemperature = "$OLD_FAN"/g" /home/$USER/scripts/run-fan.py
  193. fi
  194. sudo systemctl restart run-fan.service
  195. if [ -f /home/$USER/freeze.txt ];then rm /home/$USER/freeze.txt;fi
  196. echo -e "Done."
  197. fi
  198. }
  199. function packit {
  200. echo -e "\n\e[95mPacking & Compressing Files :\e[0m"
  201. cd /home/$USER
  202. echo -e "\e[97mCompress & split bootstrap.7z:\e[0m"
  203. split_size="${split_size}m"
  204. #7z a -m0=lzma -mx9 -mfb=64 -md32m -v$split_size bootstrap .okcash/blk0001.dat .okcash/txleveldb/
  205. 7z a -mx7 -v$split_size bootstrap .okcash/blk0001.dat .okcash/txleveldb/
  206. sleep 1
  207. echo "Done."
  208. echo -e "\n\e[95mMake bootstrap_v.txt:\e[0m"
  209. if [ -f bootstrap_v.txt ]; then rm bootstrap_v.txt;fi
  210. Raw_date="`date +%d.%m.%Y`"
  211. YYYY=`ls -l bootstrap*.0* | wc -l`
  212. cat <<'EOF'>> bootstrap_v.txt
  213. Bootstrap : XXXX
  214. Parts: YYYY
  215. Size : ZZZZ Mo
  216. Links:
  217. EOF
  218. sed -i -e "s/XXXX/$Raw_date/g" bootstrap_v.txt
  219. sed -i -e "s/YYYY/$YYYY/g" bootstrap_v.txt
  220. split_size="${split_size%?}"
  221. sed -i -e "s/ZZZZ/$split_size/g" bootstrap_v.txt
  222. echo "Done."
  223. echo -e "\n\e[95mMd5sum Files:\e[0m"
  224. zap=$(ls -l bootstrap.7z.* | wc -l)
  225. for i in `seq -w 001 $zap`
  226. do
  227. echo -e -n "md5sum Bootstrap.7z.$i: " && md5sum bootstrap.7z.$i > bootstrap.$i.md5 && echo -e "\e[1mDone.\e[0m"
  228. done
  229. touch /home/$USER/bootstrap_uploadpass
  230. }
  231. function ftp_check {
  232. FTP_SUCCESS_MSG="226 Transfer complete"
  233. if fgrep "$FTP_SUCCESS_MSG" $Ftp_LOG ;then
  234. echo "ftp transfer : OK"
  235. echo ""
  236. rm $Ftp_LOG
  237. else
  238. echo "ftp transfer Error: $ftp_file"
  239. rm $Ftp_LOG
  240. echo ""
  241. ftp_loop
  242. fi
  243. }
  244. function ftp_loop {
  245. lftp -u wareck,Kraken@2611! ftpperso.free.fr <<EOF
  246. put $ftp_file -o /crypto/okcash/bootstrap/$ftp_file
  247. EOF
  248. echo -n $ftp_file && echo -e ": \e[1mDone.\e[0m";
  249. }
  250. function upload {
  251. cd /home/$USER
  252. echo -e "\n\e[95mUpload files to FTP server :\e[0m"
  253. ftp_file=bootstrap_v.txt && ftp_loop
  254. zap=$(ls -l bootstrap.7z.0* | wc -l)
  255. for j in `seq -w 001 $zap`
  256. do
  257. ftp_file=bootstrap.$j.md5
  258. ftp_loop
  259. done
  260. echo -e "\n\e[95mUpload files to Freebox :\e[0m"
  261. if ! [ -d /tmp/bootstrap ]; then mkdir /tmp/bootstrap ;fi
  262. if mount | grep -i "//mafreebox.freebox.fr/" >/dev/null
  263. then
  264. sudo umount /tmp/bootstrap/
  265. sudo systemctl daemon-reload
  266. fi
  267. sudo mount -t cifs //mafreebox.freebox.fr/Disque\ dur/ /tmp/bootstrap/ -o user=freebox,password=password,uid=1000,gid=1000,rw,vers=1.0
  268. sudo systemctl daemon-reload
  269. if ! [ -d /tmp/bootstrap/bootstrap ]; then mkdir /tmp/bootstrap/bootstrap ;fi
  270. cp -v bootstrap.7z.* /tmp/bootstrap/bootstrap/
  271. cp -v bootstrap_v.txt /tmp/bootstrap/bootstrap/
  272. cp -v bootstrap.*.md5 /tmp/bootstrap/bootstrap/
  273. if [ -f bootstrap_okcash ]
  274. then
  275. rm -r bootstrap_okcash
  276. fi
  277. sleep 5
  278. sudo umount /tmp/bootstrap
  279. sudo systemctl daemon-reload
  280. freeze_off
  281. echo -e "\n\e[92mUpload Done ...\n\e[0m"
  282. }
  283. function end_restart {
  284. if [ -f /usr/local/bin/okcashd_old ]
  285. then
  286. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  287. okcashd
  288. fi
  289. }
  290. function retry {
  291. if [ -f /home/$USER/bootstrap_uploadpass ]; then retry_flag=1 ;fi
  292. if [ $retry_flag = "1" ]
  293. then
  294. echo -e "\n\e[95mRetry Upload to server :\e[0m"
  295. echo -e "Old files are still present."
  296. echo -e "If you want to force restart build/upload"
  297. echo -e "remove files by using this command:"
  298. echo -e ""
  299. echo -e "rm -r -f /home/$USER/bootstrap.7z.* /home/$USER/bootstrap_v.txt /home/$USER/bootstrap.7z.*.md5 /home/$USER/bootstrap_uploadpass"
  300. sleep 5
  301. fi
  302. }
  303. if ps -ef | grep -v grep | grep okcashd >/dev/null
  304. then
  305. echo -e "\n\e[38;5;166mOKcash daemon is working => shutdown and restart during this process !...\e[0m"
  306. while true; do
  307. read -p "Do you want to start processing (y/n) ? " yn
  308. case $yn in
  309. [Yy]* ) init ; check_software_; freeze_on ; retry ; packit ; upload ; end_restart ; break;;
  310. [Nn]* ) exit;;
  311. * ) echo "Please answer yes or no.";;
  312. esac
  313. done
  314. else
  315. init
  316. check_software_
  317. freeze_on
  318. retry
  319. if [ $retry_flag = "1" ]
  320. then
  321. upload
  322. end_restart
  323. else
  324. packit
  325. upload
  326. end_restart
  327. fi
  328. fi
  329. if [ swap_was = 1 ]
  330. then
  331. echo -e "\n\e[95mRestart Swap:\e[0m"
  332. sudo dphys-swapfile setup
  333. sudo dphys-swapfile swapon
  334. echo "Done."
  335. fi
  336. if [ -f /home/$USER/bootstrap_uploadpass ]; then rm /home/$USER/bootstrap_uploadpass;fi
  337. if [ $daemon_on = 1 ]
  338. then
  339. echo -e "\n\e[38;5;166mOKcash daemon is restarting ...\e[0m"
  340. if [ -f /usr/local/bin/okcashd_old ]
  341. then
  342. sudo mv /usr/local/bin/okcashd_old /usr/local/bin/okcashd | true
  343. fi
  344. fi
  345. echo ""