Advertisement
m4ly

Rsync Resume

May 2nd, 2021
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ME=$(whoami)
  4. RSYNC_DATE=$(date "+%Y-%m-%d")
  5. RSYNC_HOST="host-www01"
  6. RSYNC_DEST="/home/proxy/"
  7. RSYNC_SOURCE="/home/proxy/"
  8. EXCLUDE_LIST="$HOME/common/exclude_list.txt"
  9.  
  10. LOG_DIR="$HOME/logs/rsync_to_drc"
  11. LOG_FILE="$LOG_DIR/rsync_backup."$RSYNC_DATE".log"
  12. STATUS_FILE="/tmp/sync_"$ME".status"
  13.  
  14. COUNTER=1
  15. SLEEP_TIME=4
  16.  
  17. function say_now() {
  18. date +"[%Y-%m-%d %H:%M:%S]"
  19. }
  20.  
  21.  
  22. function preprepare() {
  23.  
  24. mkdir -p "${LOG_DIR}" > /den/null 2>&1
  25. touch "${LOG_FILE}" > /dev/null 2>&1
  26. }
  27.  
  28. preprepare
  29.  
  30. while [ "$COUNTER" -le 3 ]
  31. do
  32. echo "$(say_now)[rsync] Start." >> "$LOG_FILE"
  33. /usr/bin/rsync --progress -ahvzP --delete --exclude-from="$EXCLUDE_LIST" "$RSYNC_SOURCE" "$USER@$RSYNC_HOST:$RSYNC_DEST" >> "$LOG_FILE"
  34.  
  35. ret_rsync="$?"
  36.  
  37. if [[ "$ret_rsync" == "0" ]] ; then
  38. echo "$(say_now)[rsync] Completed. Exit." >> "$LOG_FILE"
  39.  
  40. if [ -f "$STATUS_FILE" ]; then
  41. echo "0" > "$STATUS_FILE"
  42. else
  43. echo "$(say_now)[rsync][ERROR] Status file does not exist ! Creating the file ... " >> "$LOG_FILE" touch $STATUS_FILE
  44. echo "0" > "$STATUS_FILE"
  45. fi
  46.  
  47. else
  48. echo "$(say_now)[rsync] Failure. Retrying in "$SLEEP_TIME" seconds..." >> "$LOG_FILE"
  49. sleep "$SLEEP_TIME"
  50. echo "$(say_now)[rsync] Retrying." >> "$LOG_FILE"
  51. if [ "$COUNTER" -ge 3 ]; then
  52. echo "Rsync has failed three times " >> "$LOG_FILE"
  53.  
  54. if [ -f "$STATUS_FILE" ]; then
  55. echo "$ret_rsync" > $STATUS_FILE
  56. else
  57. echo "$(say_now)[rsync][ERROR] Status file does not exist ! Creating the file ... " >> "$LOG_FILE"
  58. touch $STATUS_FILE
  59. echo "$ret_rsync" > $STATUS_FILE
  60. fi
  61. exit
  62. fi
  63. fi
  64. COUNTER=$(( $COUNTER + 1 ))
  65.  
  66. done
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement