rclone_script.sh 5.7 KB

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