media2import.sh 6.0 KB

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