sync.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # Config
  3. BASE_LOCAL="`~/bin/video/mediaPath`"
  4. BASE_REMOTE="/Volumes/Bitcasa Infinite Drive"
  5. # Command-line arguments
  6. SUB_DIR="${1}"
  7. if [ -z "${SUB_DIR}" ]; then
  8. echo "Usage: `basename "${0}"` sub_directory" 1>&2
  9. exit 1
  10. fi
  11. # Sanity checks
  12. if [ ! -d "${BASE_LOCAL}/${SUB_DIR}" ]; then
  13. echo "Invalid local directory: ${BASE_LOCAL}/${SUB_DIR}" 1>&2
  14. exit 1
  15. fi
  16. if [ ! -d "${BASE_REMOTE}" ]; then
  17. echo "Bitcasa drive not mounted" 1>&2
  18. exit 1
  19. fi
  20. # Ensure the subdirectory exists on the remote drive
  21. if [ ! -d "${BASE_REMOTE}/${SUB_DIR}" ]; then
  22. mkdir -p "${BASE_REMOTE}/${SUB_DIR}"
  23. fi
  24. # Grab directory tree lists
  25. TMP_LOCAL="`mktemp -t sync.local.XXXXXXXX`"
  26. (cd "${BASE_LOCAL}" && find "${SUB_DIR}" | sort > "${TMP_LOCAL}")
  27. TMP_REMOTE="`mktemp -t sync.remote.XXXXXXXX`"
  28. (cd "${BASE_REMOTE}" && find "${SUB_DIR}" | sort > "${TMP_REMOTE}")
  29. # Diff to find the first mis-matched file
  30. DIFF="`diff -u "${TMP_LOCAL}" "${TMP_REMOTE}" | grep "^[\+\-]${SUB_DIR}"`"
  31. # Filter junk
  32. DIFF="`echo "${DIFF}" | grep -v '\/\._' | grep -v '\/\.DS_Store$'`"
  33. # Choose the file/directory to sync
  34. FILE="`echo "${DIFF}" | head -n 1`"
  35. ACTION="`echo "${FILE}" | head -c 1`"
  36. FILE="`echo "${FILE}" | cut -d "${ACTION}" -f 2`"
  37. # Determine the action (i.e. copy to or delete from remote)
  38. if [ "${ACTION}" == '+' ]; then
  39. echo "Will not delete from remote: ${FILE}" 1>&2
  40. exit 2
  41. elif [ "${ACTION}" == '-' ]; then
  42. PATH_LOCAL="${BASE_LOCAL}/${FILE}"
  43. PATH_REMOTE="${BASE_REMOTE}/${FILE}"
  44. if [ -h "${PATH_LOCAL}" ]; then
  45. echo "Unable to create symlink: ${FILE}" 1>&2
  46. exit 2
  47. elif [ -d "${PATH_LOCAL}" ]; then
  48. echo "Creating directory: ${FILE}"
  49. mkdir "${PATH_REMOTE}"
  50. elif [ -f "${PATH_LOCAL}" ] && [ -r "${PATH_LOCAL}" ]; then
  51. echo "Copying: ${FILE}"
  52. cp "${BASE_LOCAL}/${FILE}" "${BASE_REMOTE}/${FILE}"
  53. else
  54. echo "Unable to copy file: ${FILE}" 1>&2
  55. exit 2
  56. fi
  57. else
  58. echo "Invalid action: ${ACTION}" 1>&2
  59. echo -n "\tDiff: "
  60. echo "${DIFF}" | head -n 1
  61. exit 2
  62. fi
  63. # Cleanup
  64. rm -f "${TMP_LOCAL}"
  65. rm -f "${TMP_REMOTE}"
  66. exit 0