|
@@ -1,7 +1,8 @@
|
|
#
|
|
#
|
|
-# this is simple script for importing media files from source (such as memory cards)
|
|
|
|
|
|
+# This is simple script for importing media files from source (such as memory cards)
|
|
# to destination (such as local storage), creating date directories and renaming them by timestamp
|
|
# to destination (such as local storage), creating date directories and renaming them by timestamp
|
|
-# of given file in desired format
|
|
|
|
|
|
+# of given file in desired format. Other features of script: integrity check (using sha256sum),
|
|
|
|
+#
|
|
#
|
|
#
|
|
|
|
|
|
#
|
|
#
|
|
@@ -13,19 +14,26 @@
|
|
# 2023-05-08 + read flags from CLI /A
|
|
# 2023-05-08 + read flags from CLI /A
|
|
# 2023-05-16 * moved from rsync to file 'for' itiration /A
|
|
# 2023-05-16 * moved from rsync to file 'for' itiration /A
|
|
# 2023-06-27 * upgrading /A
|
|
# 2023-06-27 * upgrading /A
|
|
|
|
+# 2023-06-28 * path with spaces variables /A
|
|
|
|
+# 2023-06-29 + hash comparison /A
|
|
|
|
+# + removal on successful integrity verification /A
|
|
#
|
|
#
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
function ts() {
|
|
function ts() {
|
|
# change d
|
|
# change d
|
|
ts=$(date)
|
|
ts=$(date)
|
|
echo $(date )
|
|
echo $(date )
|
|
}
|
|
}
|
|
|
|
|
|
-# read flags
|
|
|
|
|
|
+# default help
|
|
usage () {
|
|
usage () {
|
|
echo "Usage: $0 -s - source, -d destination, -n note";
|
|
echo "Usage: $0 -s - source, -d destination, -n note";
|
|
exit 1;
|
|
exit 1;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+# read flags
|
|
while getopts s:d:n: flag
|
|
while getopts s:d:n: flag
|
|
do
|
|
do
|
|
case "${flag}" in
|
|
case "${flag}" in
|
|
@@ -37,18 +45,18 @@ do
|
|
done
|
|
done
|
|
|
|
|
|
|
|
|
|
-# read source, by default current path (if not given)
|
|
|
|
|
|
+# read source path, by default current path (if not given)
|
|
# assuming not recursive, only in-directory files will be imported
|
|
# assuming not recursive, only in-directory files will be imported
|
|
if [ -z ${src} ]; then
|
|
if [ -z ${src} ]; then
|
|
- read -p "Source [$(pwd)]: " src
|
|
|
|
|
|
+ read -p "[ $(ts) ]: Source [$(pwd)]: " src
|
|
if [ -z ${src} ]; then
|
|
if [ -z ${src} ]; then
|
|
src="$(pwd)"
|
|
src="$(pwd)"
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
|
|
-# read destination, by default current path (if not given)
|
|
|
|
|
|
+# read destination path, by default current path (if not given)
|
|
if [ -z ${dst} ]; then
|
|
if [ -z ${dst} ]; then
|
|
- read -p "Destination [$(pwd)]: " dst
|
|
|
|
|
|
+ read -p "[ $(ts) ]: Destination [$(pwd)]: " dst
|
|
if [ -z ${dst} ]; then
|
|
if [ -z ${dst} ]; then
|
|
dst="$(pwd)"
|
|
dst="$(pwd)"
|
|
fi
|
|
fi
|
|
@@ -56,68 +64,110 @@ fi
|
|
|
|
|
|
# read noteription, by default empty
|
|
# read noteription, by default empty
|
|
if [ -z ${note} ]; then
|
|
if [ -z ${note} ]; then
|
|
- read -p "Note of session/event: " note
|
|
|
|
|
|
+ read -p "[ $(ts) ]: Note of session/event: " note
|
|
if [ -z ${note} ]; then
|
|
if [ -z ${note} ]; then
|
|
note=""
|
|
note=""
|
|
else
|
|
else
|
|
- # adding hyphen before noteription, if it is not empty
|
|
|
|
|
|
+ # adding hyphen before note, if it is not empty
|
|
note=" - ${note}"
|
|
note=" - ${note}"
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
|
|
# source and destination can not be the same, exit
|
|
# source and destination can not be the same, exit
|
|
if [ ${src} = ${dst} ]; then
|
|
if [ ${src} = ${dst} ]; then
|
|
- echo "Source and destination are the same, exiting..."
|
|
|
|
|
|
+ echo "[ $(ts) ]: Source and destination are the same, exiting..."
|
|
exit 2
|
|
exit 2
|
|
fi
|
|
fi
|
|
|
|
|
|
|
|
+# # itirating files
|
|
|
|
+# # calculate total size for source files, as they will be copied (not recursive for directory)
|
|
|
|
+# done
|
|
|
|
+# echo "The size of all NN files is YY bytes (YY/1024 MB = YY/1024/104) GB."
|
|
|
|
|
|
-# confirm
|
|
|
|
-echo "Source: [${src}]"
|
|
|
|
-echo "Destination: [${dst}]"
|
|
|
|
-echo "Note: [${note}]"
|
|
|
|
|
|
|
|
-read -p "Confirm (Y): " confirm
|
|
|
|
-if [ ${confirm} = "Y" ]; then
|
|
|
|
- echo "Reading source and beginning to move..."
|
|
|
|
|
|
+echo "[ $(ts) ]: ----- [ Transfer details ] ---------------------------------------------"
|
|
|
|
|
|
- # check that source directory exists, otherwise - exit
|
|
|
|
- if ! [ -d ${src} ]; then
|
|
|
|
- echo "Source directory does not exist, exiting..."
|
|
|
|
- exit 2
|
|
|
|
- fi
|
|
|
|
|
|
+# check that source directory exists, otherwise - exit
|
|
|
|
+if [ -d "$src" ]; then
|
|
|
|
+ # TODO: src, dst total and free disk space before transfer
|
|
|
|
+ files_src_total_amount=0
|
|
|
|
+ files_src_total_size=0
|
|
|
|
+ for file in "$src"/*; do
|
|
|
|
+ file_size="$( stat -f %z "$file" )"
|
|
|
|
+ # echo "[ $(ts) ]: file: ${file} adding file_size: $(( $file_size/1024/1024 )) MB."
|
|
|
|
+ files_src_total_size=$(( $files_src_total_size+$file_size ))
|
|
|
|
+ files_src_total_amount=$(( $files_src_total_amount+1 ))
|
|
|
|
+ done
|
|
|
|
+ echo "[ $(ts) ]: Source: [${src}]"
|
|
|
|
+ echo "[ $(ts) ]: Total of $files_src_total_amount src files, total size is $(( $files_src_total_size/1024/1024 )) MB)."
|
|
|
|
+
|
|
|
|
+else
|
|
|
|
+ echo "[ $(ts) ]: src dir does not exist, exiting..."
|
|
|
|
+ exit 2
|
|
|
|
+fi
|
|
|
|
+
|
|
|
|
+echo "[ $(ts) ]: Destination: [${dst}]"
|
|
|
|
+echo "[ $(ts) ]: Note: [${note}]"
|
|
|
|
+
|
|
|
|
+# confirm
|
|
|
|
+read -p "[ $(ts) ]: Confirm (Y): " confirm
|
|
|
|
+if [ ${confirm} = "Y" ]; then
|
|
|
|
+ echo "[ $(ts) ]: Preparing to transfer..."
|
|
|
|
|
|
# check and create destination directory, if needed
|
|
# check and create destination directory, if needed
|
|
- if [ -d ${dst} ]; then
|
|
|
|
- mkdir -p -m 700 ${dst}
|
|
|
|
|
|
+ if ! [ -d "$dst" ]; then
|
|
|
|
+ read -p "[ $(ts) ]: dst dir does not exist, do you want to create?" confirm
|
|
|
|
+ if [ ${confirm} = "Y" ]; then
|
|
|
|
+ mkdir -v -p -m 700 "$dst"
|
|
|
|
+ echo "[ $(ts) ]: dst dir created."
|
|
|
|
+ fi
|
|
fi
|
|
fi
|
|
|
|
|
|
- # # itirating files
|
|
|
|
- # for file in ${src}/*; do
|
|
|
|
- # # calculate total size for source files, as they will be copied (not recursive for directory)
|
|
|
|
- # done
|
|
|
|
- # echo "The size of all NN files is YY bytes (YY/1024 MB = YY/1024/104) GB."
|
|
|
|
|
|
|
|
- # src, dst total and free disk space before transfer
|
|
|
|
|
|
+
|
|
|
|
+ files_copied_filenam=()
|
|
|
|
+ files_copied_total_size=0
|
|
|
|
+ files_error_filename=()
|
|
|
|
|
|
# itirating files
|
|
# itirating files
|
|
- for file in ${src}/*; do
|
|
|
|
- filename="$(basename ${file})"
|
|
|
|
|
|
+ for file in "$src"/*; do
|
|
|
|
+ echo "[ $(ts) ]: src dir: ["$src"]"
|
|
|
|
+
|
|
|
|
+ filename="$(basename "$file")"
|
|
# figure out when is the creation date
|
|
# figure out when is the creation date
|
|
- file_mdate="$( stat -f %Sm -t %Y-%m-%d ${file} )"
|
|
|
|
|
|
+ file_mdate="$( stat -f %Sm -t %Y-%m-%d "$file" )"
|
|
|
|
+ file_size="$( stat -f %z "$file" )"
|
|
# create subdirectory for creation date
|
|
# create subdirectory for creation date
|
|
- mkdir -p ${dst}/${file_mdate}
|
|
|
|
- echo "[ $(ts) ]: src file : [${filename}], modification date is: ${file_mdate}, size: 11.5 MB"
|
|
|
|
- # linux/BSD check
|
|
|
|
- echo "sha256sum: [$( shasum -a 256 ${file} )]"
|
|
|
|
- echo "dst dir: [${dst}/${file_mdate}]"
|
|
|
|
|
|
+ mkdir -p "$dst"/${file_mdate}
|
|
|
|
+ echo "[ $(ts) ]: src file : [${filename}], modification date is: ${file_mdate}, ( $(( ${file_size}/1024/1024 )) MB )"
|
|
|
|
+ # TODO: linux/BSD check
|
|
|
|
|
|
- # main operation
|
|
|
|
- cp ${file} ${dst}/${file_mdate}
|
|
|
|
-
|
|
|
|
- echo "sha256sum: [$( shasum -a 256 ${dst}/${file_mdate}/${filename} )]"
|
|
|
|
|
|
+ # calulating src hash sum
|
|
|
|
+ src_hash=$( shasum -a 256 "$file" | cut -d ' ' -f 1)
|
|
|
|
+ # echo "[ $(ts) ]: src sha256sum: [${src_hash}]"
|
|
|
|
+ echo "[ $(ts) ]: dst dir: ["$dst"/${file_mdate}]"
|
|
|
|
|
|
- # if shasum is the same, remove file
|
|
|
|
|
|
+ # main operation
|
|
|
|
+ echo "[ $(ts) ] copying.."
|
|
|
|
+ cp "$file" "$dst"/${file_mdate}
|
|
|
|
+
|
|
|
|
+ # calulating dst hash sum
|
|
|
|
+ dst_hash=$( shasum -a 256 "${dst}/${file_mdate}/${filename}" | cut -d ' ' -f 1)
|
|
|
|
+ # echo "[ $(ts) ]: dst sha256sum: [${dst_hash}]"
|
|
|
|
+
|
|
|
|
+ # if shasum is the same, add to statistics and remove file
|
|
|
|
+ if [ ${src_hash} = ${dst_hash} ]; then
|
|
|
|
+ # add to files_copied array
|
|
|
|
+ files_copied_filename+=("$filename")
|
|
|
|
+ files_copied_total_size+=$file_size
|
|
|
|
+
|
|
|
|
+ # add summarize sized of copied file ${file_size}
|
|
|
|
+ echo "[ $(ts) ]: src and dst hashes are the same, removing src file"
|
|
|
|
+ rm "${file}"
|
|
|
|
+ else
|
|
|
|
+ echo "[ $(ts) ]: src and dst hashes are different."
|
|
|
|
+ files_error_filename+=("$file")
|
|
|
|
+ fi
|
|
|
|
|
|
# add original file with fullpath to array:
|
|
# add original file with fullpath to array:
|
|
# files_src[]="${file}]"
|
|
# files_src[]="${file}]"
|
|
@@ -141,13 +191,6 @@ if [ ${confirm} = "Y" ]; then
|
|
# itirate files
|
|
# itirate files
|
|
# read creation date of file
|
|
# read creation date of file
|
|
#dst_subdir="${dst}/${file_creation_date}${note}"
|
|
#dst_subdir="${dst}/${file_creation_date}${note}"
|
|
- # read creation date and time of file
|
|
|
|
- #file_new_name="$(read creation date and time, format)-${file}"
|
|
|
|
- # output debug
|
|
|
|
- # hash before move
|
|
|
|
- # TODO: check if file exists
|
|
|
|
- # hash after
|
|
|
|
- # TODO: compare hashes, output result
|
|
|
|
|
|
|
|
else
|
|
else
|
|
# not confirmed
|
|
# not confirmed
|