sync.sh 3.6 KB

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