sync.sh 3.9 KB

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