sync.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/bin/bash
  2. # Ensure the media share is mounted
  3. if ! ~/bin/video/isMediaMounted; then
  4. echo 'Media share not available' 1>&2
  5. exit 1
  6. fi
  7. # Config
  8. BASE_LOCAL="`~/bin/video/mediaPath`"
  9. BASE_REMOTE="/Volumes/Bitcasa Infinite Drive"
  10. # Command-line arguments
  11. SUB_DIR="${1}"
  12. declare -i NUM_FILES
  13. NUM_FILES=1
  14. if [ -n "${2}" ]; then
  15. NUM_FILES="${2}"
  16. fi
  17. declare -i DELAY_DAYS
  18. DELAY_DAYS=30
  19. if [ -n "${3}" ]; then
  20. DELAY_DAYS="${3}"
  21. fi
  22. # Usage checks
  23. if [ -z "${SUB_DIR}" ] || [ $NUM_FILES -lt 1 ] || [ $DELAY_DAYS -lt 1 ]; then
  24. echo "Usage: `basename "${0}"` sub_directory [num_files] [delay_days]" 1>&2
  25. exit 1
  26. fi
  27. # Allow usage with absolute local paths
  28. if [ "`echo "${SUB_DIR}" | head -c 1`" == '/' ]; then
  29. BASE_LOCAL="`dirname "${SUB_DIR}"`"
  30. SUB_DIR="`basename "${SUB_DIR}"`"
  31. fi
  32. # Sanity checks
  33. if [ ! -d "${BASE_LOCAL}/${SUB_DIR}" ]; then
  34. echo "Invalid local directory: ${BASE_LOCAL}/${SUB_DIR}" 1>&2
  35. exit 1
  36. fi
  37. if [ ! -d "${BASE_REMOTE}" ]; then
  38. echo "Remote drive not mounted" 1>&2
  39. exit 1
  40. fi
  41. # Ensure we can actually write to the remote drive
  42. WC="${BASE_REMOTE}/.write_check"
  43. touch "${WC}" >/dev/null 2>&1
  44. if [ ! -e "${WC}" ]; then
  45. echo "Remote drive not writable" 1>&2
  46. exit 1
  47. fi
  48. rm -f "${WC}" >/dev/null 2>&1
  49. if [ -e "${WC}" ]; then
  50. echo "Remote drive not writable" 1>&2
  51. exit 1
  52. fi
  53. # Create the named subdirectory if it does not exist on the remote drive
  54. if [ ! -d "${BASE_REMOTE}/${SUB_DIR}" ]; then
  55. mkdir -p "${BASE_REMOTE}/${SUB_DIR}"
  56. fi
  57. # Grab and sort the local file list
  58. # Limit files by mtime, but include all directories
  59. TMP_LOCAL="`mktemp -t sync.local.XXXXXXXX`"
  60. TMP_LOCAL2="`mktemp -t sync.local2.XXXXXXXX`"
  61. (cd "${BASE_LOCAL}" && find "${SUB_DIR}" -type f -mtime "+${DELAY_DAYS}d" > "${TMP_LOCAL2}")
  62. (cd "${BASE_LOCAL}" && find "${SUB_DIR}" -type d >> "${TMP_LOCAL2}")
  63. cat "${TMP_LOCAL2}" | sort > "${TMP_LOCAL}"
  64. # Grab and sort the remote file list
  65. TMP_REMOTE="`mktemp -t sync.remote.XXXXXXXX`"
  66. (cd "${BASE_REMOTE}" && find "${SUB_DIR}" | sort > "${TMP_REMOTE}")
  67. # Diff to find the first mis-matched file, then drop out temp files
  68. TMP_DIFF="`mktemp -t sync.diff.XXXXXXXX`"
  69. TMP_DIFF2="`mktemp -t sync.diff2.XXXXXXXX`"
  70. diff -u "${TMP_LOCAL}" "${TMP_REMOTE}" | grep "^[\+\-]${SUB_DIR}" > "${TMP_DIFF2}"
  71. rm -f "${TMP_LOCAL}" "${TMP_LOCAL2}" "${TMP_REMOTE}"
  72. # Filter junk
  73. grep -v '\/\._' "${TMP_DIFF2}" | grep -v '\/\.DS_Store$' | grep -v '\/\.git\/' > "${TMP_DIFF}"
  74. rm -f "${TMP_DIFF2}"
  75. # Limit NUM_FILES to the number of files available
  76. COUNT="`cat "${TMP_DIFF}" | wc -l`"
  77. if [ $NUM_FILES -gt $COUNT ]; then
  78. NUM_FILES=$COUNT
  79. fi
  80. # Loop for NUM_FILES
  81. for (( i=1; i<=${NUM_FILES}; i++ )); do
  82. # Choose the file/directory to sync
  83. FILE="`head -n $i "${TMP_DIFF}" | tail -n 1`"
  84. ACTION="`echo "${FILE}" | head -c 1`"
  85. FILE="`echo "${FILE}" | cut -d "${ACTION}" -f 2-`"
  86. # Construct absolute paths
  87. PATH_LOCAL="${BASE_LOCAL}/${FILE}"
  88. PATH_REMOTE="${BASE_REMOTE}/${FILE}"
  89. # Determine the action (i.e. copy to or delete from remote)
  90. if [ "${ACTION}" == '+' ] && [ -z "${NO_DELETE}" ]; then
  91. if [ -d "${PATH_REMOTE}" ]; then
  92. echo "Removing directory: ${FILE}"
  93. rmdir "${PATH_REMOTE}"
  94. elif [ -f "${PATH_REMOTE}" ]; then
  95. echo "Deleting file: ${FILE}"
  96. rm "${PATH_REMOTE}"
  97. else
  98. echo "Unable to delete file: ${FILE}" 1>&2
  99. exit 2
  100. fi
  101. elif [ "${ACTION}" == '-' ]; then
  102. # Handle files by type
  103. if [ -h "${PATH_LOCAL}" ]; then
  104. echo "Unable to create symlink: ${FILE}" 1>&2
  105. exit 2
  106. elif [ -d "${PATH_LOCAL}" ]; then
  107. echo "Creating directory: ${FILE}"
  108. mkdir "${PATH_REMOTE}"
  109. elif [ -f "${PATH_LOCAL}" ] && [ -r "${PATH_LOCAL}" ]; then
  110. echo "Copying: ${FILE}"
  111. cp "${BASE_LOCAL}/${FILE}" "${BASE_REMOTE}/${FILE}"
  112. else
  113. echo "Unable to copy file: ${FILE}" 1>&2
  114. exit 2
  115. fi
  116. else
  117. echo "Invalid action: ${ACTION}" 1>&2
  118. exit 2
  119. fi
  120. done
  121. # Cleanup
  122. rm -f "${TMP_DIFF}"
  123. exit 0