You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

115 lines
2.3 KiB

  1. #!/bin/bash
  2. # Offloads megawarcs from the upload queue.
  3. # (Needs a config.sh in the working directory.)
  4. #
  5. # ./offload-one
  6. #
  7. # 1. Grabs an item from UPLOAD_QUEUE_DIR
  8. # 2. Reserves the item by moving the directory to the
  9. # UPLOADER_WORKING_DIR
  10. # 3. Offloads the item to the target defined in OFFLOAD_TARGET
  11. # 4. Removes the source files from the working directory
  12. # If COMPLETED_DIR is set, offloaded files are moved there.
  13. #
  14. # The program exits with 1 on any nontransient error.
  15. #
  16. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  17. source ./config.sh || exit 1
  18. mkdir -p "$UPLOAD_QUEUE_DIR" || exit 1
  19. mkdir -p "$UPLOADER_WORKING_DIR" || exit 1
  20. if [ ! -z "$COMPLETED_DIR" ]
  21. then
  22. mkdir -p "$COMPLETED_DIR" || exit 1
  23. fi
  24. function mayicontinue {
  25. echo
  26. # echo "May I continue?"
  27. # read
  28. # echo
  29. }
  30. mayicontinue
  31. # try to grab an item from UPLOAD_QUEUE_DIR
  32. ITEM=none
  33. while [[ $ITEM = none ]]
  34. do
  35. possible_item=$( ls -1 "$UPLOAD_QUEUE_DIR" | grep -E '[0-9]{14}_[a-f0-9]{8}$' | sort | head -n 1 )
  36. if test -n "${possible_item}"
  37. then
  38. echo "Trying to grab $possible_item"
  39. if mv "$UPLOAD_QUEUE_DIR/$possible_item" "$UPLOADER_WORKING_DIR/"
  40. then
  41. ITEM=$possible_item
  42. else
  43. echo "Failed to move $possible_item"
  44. sleep 5
  45. fi
  46. else
  47. date
  48. echo "No current item found!"
  49. sleep 30
  50. exit 0
  51. fi
  52. done
  53. echo "$( date ): Start offloading for item $ITEM" >> uploader.log
  54. result=1
  55. while [[ $result -ne 0 ]]
  56. do
  57. rsync -r --progress --stats --no-owner --no-group --partial --partial-dir .rsync-tmp --no-compress --compress-level 0 "${UPLOADER_WORKING_DIR}/${ITEM}/" "${OFFLOAD_TARGET}/${ITEM}/"
  58. result=$?
  59. if [[ $result -ne 0 ]]
  60. then
  61. date
  62. echo "Error while offloading $ITEM, rsync said $result"
  63. echo "Will retry in 30 seconds"
  64. sleep 30
  65. fi
  66. done
  67. echo "Offloaded $ITEM"
  68. echo "$( date ): Completed offloading for item $ITEM" >> uploader.log
  69. mayicontinue
  70. # move or remove megawarc
  71. if [ -z "$COMPLETED_DIR" ]
  72. then
  73. # remove
  74. rm -rf "${UPLOADER_WORKING_DIR}/${ITEM}"
  75. result=$?
  76. if [[ $result -ne 0 ]]
  77. then
  78. date
  79. echo "rm -rf megawarc exited with $result for $ITEM"
  80. exit 1
  81. fi
  82. else
  83. # move
  84. mv "${UPLOADER_WORKING_DIR}/${ITEM}" "${COMPLETED_DIR}/"
  85. result=$?
  86. if [[ $result -ne 0 ]]
  87. then
  88. date
  89. echo "rm -rf megawarc exited with $result for $ITEM"
  90. exit 1
  91. fi
  92. fi
  93. exit 0