1
0

rclone_script.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/bin/bash
  2. # define colors for output
  3. NORMAL=$(tput sgr0)
  4. RED=$(tput setaf 1)
  5. GREEN=$(tput setaf 2)
  6. YELLOW=$(tput setaf 3)
  7. BLUE=$(tput setaf 4)
  8. UNDERLINE=$(tput smul)
  9. # include settings file
  10. source ~/scripts/rclone_script/rclone_script.ini
  11. # parameters
  12. direction="$1"
  13. system="$2"
  14. emulator="$3"
  15. rom="$4"
  16. command="$5"
  17. log ()
  18. {
  19. severity=$1
  20. message=$2
  21. printf "$(date +%FT%T%:z):\t${severity}:\t${message}\n" >> ${logfile}
  22. }
  23. debug ()
  24. {
  25. log "DEBUG" "direction: ${direction}"
  26. log "DEBUG" "system: ${system}"
  27. log "DEBUG" "emulator: ${emulator}"
  28. log "DEBUG" "rom: ${rom}"
  29. log "DEBUG" "command: ${command}"
  30. log "DEBUG" "remotebasedir: ${remotebasedir}"
  31. log "DEBUG" "rompath: ${rompath}"
  32. log "DEBUG" "romfilename: ${romfilename}"
  33. log "DEBUG" "romfilebase: ${romfilebase}"
  34. log "DEBUG" "romfileext: ${romfileext}"
  35. }
  36. killOtherNotification ()
  37. {
  38. # get PID of other PNGVIEW process
  39. otherPID=$(pgrep --full pngview)
  40. if [ "${debug}" = "1" ]; then log "DEBUG" "Other PIDs: ${otherPID}"; fi
  41. if [ "${otherPID}" != "" ]
  42. then
  43. if [ "${debug}" = "1" ]; then log "DEBUG" "Kill other PNGVIEW ${otherPID}"; fi
  44. kill ${otherPID}
  45. fi
  46. }
  47. showNotification ()
  48. {
  49. message="$1"
  50. if [ "$2" = "" ]
  51. then
  52. color="yelloW"
  53. else
  54. color="$2"
  55. fi
  56. if [ "$3" = "" ]
  57. then
  58. timeout="10000"
  59. else
  60. timeout="$3"
  61. fi
  62. if [ "$4" = "" ]
  63. then
  64. posx="10"
  65. else
  66. posx="$4"
  67. fi
  68. if [ "$5" = "" ]
  69. then
  70. posy="10"
  71. else
  72. posy="$5"
  73. fi
  74. # create PNG using IMAGEMAGICK
  75. convert -size 1500x32 xc:"rgba(0,0,0,0)" -type truecolormatte -gravity NorthWest \
  76. -pointsize 32 -font FreeMono -style italic \
  77. -fill ${color} -draw "text 0,0 '${message}'" \
  78. PNG32:- > ~/scripts/rclone_script-notification.png
  79. killOtherNotification
  80. # show PNG using PNGVIEW
  81. nohup pngview -b 0 -l 10000 ~/scripts/rclone_script-notification.png -x ${posx} -y ${posy} -t ${timeout} &>/dev/null &
  82. }
  83. getROMFileName ()
  84. {
  85. rompath="${rom%/*}" # directory containing $rom
  86. romfilename="${rom##*/}" # filename of $rom, including extension
  87. romfilebase="${romfilename%%.*}" # filename of $rom, excluding extension
  88. romfileext="${romfilename#*.}" # extension of $rom
  89. }
  90. prepareFilter ()
  91. {
  92. filter="${romfilebase//\[/\\[}"
  93. filter="${filter//\]/\\]}"
  94. }
  95. getTypeOfRemote ()
  96. {
  97. # list all remotes and their type
  98. remotes=$(rclone listremotes -l)
  99. # get line wiht RETROPIE remote
  100. retval=$(grep -i "^retropie:" <<< ${remotes})
  101. remoteType="${retval#*:}"
  102. remoteType=$(echo ${remoteType} | xargs)
  103. }
  104. downloadSaves ()
  105. {
  106. log "INFO" "Started ${romfilename} (${system})"
  107. log "INFO" "Downloading saves and states from ${remoteType}..."
  108. showNotification "Downloading saves and states from ${remoteType}..."
  109. # test for remote files
  110. remotefiles=$(rclone lsf retropie:${remotebasedir}/${system} --include "${filter}.*")
  111. retval=$?
  112. if [ "${retval}" = "0" ]
  113. then # no error with RCLONE
  114. if [ "${remotefiles}" = "" ]
  115. then # no remote files found
  116. log "INFO" "No remote files found"
  117. showNotification "Downloading saves and states from ${remoteType}... No remote saves found"
  118. else # remote files found
  119. log "INFO" "Found remote files"
  120. # download saves and states to corresponding ROM
  121. rclone copy retropie:${remotebasedir}/${system} ~/RetroPie/saves/${system} --include "${filter}.*" --update
  122. retval=$?
  123. if [ "${retval}" = "0" ]
  124. then
  125. log "INFO" "Done"
  126. showNotification "Downloading saves and states from ${remoteType}... Done" "green"
  127. else
  128. log "ERROR" "Saves could not be downloaded"
  129. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red"
  130. fi
  131. fi
  132. else # error with RCLONE
  133. log "ERROR" "Saves could not be downloaded"
  134. showNotification "Downloading saves and states from ${remoteType}... ERROR" "red"
  135. fi
  136. }
  137. uploadSaves ()
  138. {
  139. log "INFO" "Stopped ${romfilename} (${system})"
  140. log "INFO" "Uploading saves and states to ${remoteType}..."
  141. showNotification "Uploading saves and states to ${remoteType}..."
  142. localfiles=$(find ~/RetroPie/saves/${system} -type f -iname "${filter}.*")
  143. if [ "${localfiles}" = "" ]
  144. then # no local files found
  145. log "INFO" "No local saves found"
  146. showNotification "Uploading saves and states to ${remoteType}... No local saves found"
  147. else # local files found
  148. # upload saves and states to corresponding ROM
  149. rclone copy ~/RetroPie/saves/${system} retropie:${remotebasedir}/${system} --include "${filter}.*" --update
  150. retval=$?
  151. if [ "${retval}" = "0" ]
  152. then
  153. log "INFO" "Done"
  154. showNotification "Uploading saves and states to ${remoteType}... Done" "green"
  155. else
  156. log "ERROR" "Saves could not be uploaded"
  157. showNotification "Uploading saves and states to ${remoteType}... ERROR" "red"
  158. fi
  159. fi
  160. }
  161. doFullSync ()
  162. {
  163. # header
  164. printf "${UNDERLINE}Full synchronization\n\n"
  165. # Download newer files from remote to local
  166. printf "${NORMAL}Downloading newer files from ${YELLOW}${YELLOW}retropie:${remotebasedir} (${remoteType}) ${NORMAL}to ${YELLOW}~/RetroPie/saves/${NORMAL}...\n"
  167. rclone copy retropie:${remotebasedir}/ ~/RetroPie/saves/ --update --verbose
  168. printf "${GREEN}Done\n"
  169. printf "\n"
  170. # Upload newer files from local to remote
  171. printf "${NORMAL}Uploading newer files from ${YELLOW}~/RetroPie/saves/${NORMAL} to ${YELLOW}${YELLOW}retropie:${remotebasedir} (${remoteType})${NORMAL} ...\n"
  172. rclone copy ~/RetroPie/saves/ retropie:${remotebasedir}/ --update --verbose
  173. printf "${GREEN}Done\n"
  174. printf "\n"
  175. printf "${NORMAL}Returning to EmulationStation in ${YELLOW}10 seconds ${NORMAL}...\n"
  176. read -t 10
  177. }
  178. if [ "${debug}" = "1" ]; then debug; fi
  179. if [ "${direction}" == "up" ] && [ "${system}" != "kodi" ]
  180. then
  181. getROMFileName
  182. prepareFilter
  183. getTypeOfRemote
  184. uploadSaves
  185. fi
  186. if [ "${direction}" == "down" ] && [ "${system}" != "kodi" ]
  187. then
  188. getROMFileName
  189. prepareFilter
  190. getTypeOfRemote
  191. downloadSaves
  192. fi
  193. if [ "${direction}" == "full" ]
  194. then
  195. getTypeOfRemote
  196. doFullSync
  197. fi