sync.sh 3.9 KB

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