media2import.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #
  2. # This is simple script for importing media files from source (such as memory cards)
  3. # to destination (such as local storage), creating date directories and renaming them by timestamp
  4. # of given file in desired format. Other features of script: integrity check (using sha256sum),
  5. #
  6. #
  7. #
  8. # Author: Anton TETERIN (https://og2k.com)
  9. # Source: https://github.com/InstallAndUse/Graphics
  10. #
  11. # History:
  12. # 2023-05-07 * init /A
  13. # 2023-05-08 + read flags from CLI /A
  14. # 2023-05-16 * moved from rsync to file 'for' itiration /A
  15. # 2023-06-27 * upgrading /A
  16. # 2023-06-28 * path with spaces variables /A
  17. # 2023-06-29 + hash comparison /A
  18. # + removal on successful integrity verification /A
  19. #
  20. function ts() {
  21. # change d
  22. ts=$(date)
  23. echo $(date )
  24. }
  25. # default help
  26. usage () {
  27. echo "Usage: $0 -s - source, -d destination, -n note";
  28. exit 1;
  29. }
  30. # read flags
  31. while getopts s:d:n: flag
  32. do
  33. case "${flag}" in
  34. s) src=${OPTARG};;
  35. d) dst=${OPTARG};;
  36. n) note=${OPTARG};;
  37. ?) usage;;
  38. esac
  39. done
  40. # read source path, by default current path (if not given)
  41. # assuming not recursive, only in-directory files will be imported
  42. if [ -z ${src} ]; then
  43. read -p "[ $(ts) ]: Source [$(pwd)]: " src
  44. if [ -z ${src} ]; then
  45. src="$(pwd)"
  46. fi
  47. fi
  48. # read destination path, by default current path (if not given)
  49. if [ -z ${dst} ]; then
  50. read -p "[ $(ts) ]: Destination [$(pwd)]: " dst
  51. if [ -z ${dst} ]; then
  52. dst="$(pwd)"
  53. fi
  54. fi
  55. # read noteription, by default empty
  56. if [ -z ${note} ]; then
  57. read -p "[ $(ts) ]: Note of session/event: " note
  58. if [ -z ${note} ]; then
  59. note=""
  60. else
  61. # adding hyphen before note, if it is not empty
  62. note=" - ${note}"
  63. fi
  64. fi
  65. # source and destination can not be the same, exit
  66. if [ ${src} = ${dst} ]; then
  67. echo "[ $(ts) ]: Source and destination are the same, exiting..."
  68. exit 2
  69. fi
  70. # # itirating files
  71. # # calculate total size for source files, as they will be copied (not recursive for directory)
  72. # done
  73. # echo "The size of all NN files is YY bytes (YY/1024 MB = YY/1024/104) GB."
  74. echo "[ $(ts) ]: ----- [ Transfer details ] ---------------------------------------------"
  75. # check that source directory exists, otherwise - exit
  76. if [ -d "$src" ]; then
  77. # TODO: src, dst total and free disk space before transfer
  78. files_src_total_amount=0
  79. files_src_total_size=0
  80. for file in "$src"/*; do
  81. file_size="$( stat -f %z "$file" )"
  82. # echo "[ $(ts) ]: file: ${file} adding file_size: $(( $file_size/1024/1024 )) MB."
  83. files_src_total_size=$(( $files_src_total_size+$file_size ))
  84. files_src_total_amount=$(( $files_src_total_amount+1 ))
  85. done
  86. echo "[ $(ts) ]: Source: [${src}]"
  87. echo "[ $(ts) ]: Total of $files_src_total_amount src files, total size is $(( $files_src_total_size/1024/1024 )) MB)."
  88. else
  89. echo "[ $(ts) ]: src dir does not exist, exiting..."
  90. exit 2
  91. fi
  92. echo "[ $(ts) ]: Destination: [${dst}]"
  93. echo "[ $(ts) ]: Note: [${note}]"
  94. # confirm
  95. read -p "[ $(ts) ]: Confirm (Y): " confirm
  96. if [ ${confirm} = "Y" ]; then
  97. echo "[ $(ts) ]: Preparing to transfer..."
  98. # check and create destination directory, if needed
  99. if ! [ -d "$dst" ]; then
  100. read -p "[ $(ts) ]: dst dir does not exist, do you want to create?" confirm
  101. if [ ${confirm} = "Y" ]; then
  102. mkdir -v -p -m 700 "$dst"
  103. echo "[ $(ts) ]: dst dir created."
  104. fi
  105. fi
  106. files_copied_filenam=()
  107. files_copied_total_size=0
  108. files_error_filename=()
  109. # itirating files
  110. for file in "$src"/*; do
  111. echo "[ $(ts) ]: src dir: ["$src"]"
  112. filename="$(basename "$file")"
  113. # figure out when is the creation date
  114. file_mdate="$( stat -f %Sm -t %Y-%m-%d "$file" )"
  115. file_size="$( stat -f %z "$file" )"
  116. # create subdirectory for creation date
  117. mkdir -p "$dst"/${file_mdate}
  118. echo "[ $(ts) ]: src file : [${filename}], modification date is: ${file_mdate}, ( $(( ${file_size}/1024/1024 )) MB )"
  119. # TODO: linux/BSD check
  120. # calulating src hash sum
  121. src_hash=$( shasum -a 256 "$file" | cut -d ' ' -f 1)
  122. # echo "[ $(ts) ]: src sha256sum: [${src_hash}]"
  123. echo "[ $(ts) ]: dst dir: ["$dst"/${file_mdate}]"
  124. # main operation
  125. echo "[ $(ts) ] copying.."
  126. cp "$file" "$dst"/${file_mdate}
  127. # calulating dst hash sum
  128. dst_hash=$( shasum -a 256 "${dst}/${file_mdate}/${filename}" | cut -d ' ' -f 1)
  129. # echo "[ $(ts) ]: dst sha256sum: [${dst_hash}]"
  130. # if shasum is the same, add to statistics and remove file
  131. if [ ${src_hash} = ${dst_hash} ]; then
  132. # add to files_copied array
  133. files_copied_filename+=("$filename")
  134. files_copied_total_size+=$file_size
  135. # add summarize sized of copied file ${file_size}
  136. echo "[ $(ts) ]: src and dst hashes are the same, removing src file"
  137. rm "${file}"
  138. else
  139. echo "[ $(ts) ]: src and dst hashes are different."
  140. files_error_filename+=("$file")
  141. fi
  142. # add original file with fullpath to array:
  143. # files_src[]="${file}]"
  144. # add copied file with fullpath to array:
  145. # files_dst[]="${dst}/${file_mdate}/${filename}"
  146. done
  147. # src, dst total and free disk space before transfer
  148. # src, dst total and free disk space after copy
  149. # time taken to transfer
  150. # avarage transfer speed
  151. # unmount disk ?
  152. # diskutil unmount /Volumes/empty
  153. # open latest directory created?
  154. # TODO: output total amount of files and size
  155. # itirate files
  156. # read creation date of file
  157. #dst_subdir="${dst}/${file_creation_date}${note}"
  158. else
  159. # not confirmed
  160. echo "Operation is not confirmed."
  161. exit 1
  162. fi