From 835c330cd6afeb262a255917b27a2f46080d302b Mon Sep 17 00:00:00 2001 From: Huber Date: Thu, 7 Feb 2019 15:11:39 +0100 Subject: [PATCH 01/14] tools/db/import-md5-list --- tools/db/import-md5-list | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tools/db/import-md5-list diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list new file mode 100644 index 0000000..e69de29 From 89c70506c967b57eb20d39c1513e15c8e57a52e4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 7 Feb 2019 15:16:57 +0100 Subject: [PATCH 02/14] define options/parameters creat script header --- tools/db/import-md5-list | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index e69de29..d7b0f1f 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -0,0 +1,14 @@ +#!/bin/bash +# @author 6543 +# @version 0.1 +# @date 2019-02-07 + +## Options/Parameter +# $1: +# -a | add files of md5-file to db +# -r | remove files of md5-file from db +# -d | mark files from md5-file ad deleted +# +# $2: +# [$3]: + From d95a7ea54f9c57b0fc97c5536c07bd391e94fabd Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 7 Feb 2019 17:02:29 +0100 Subject: [PATCH 03/14] - add db_query function - add db_exist_md5 function - creat basic structure of script --- tools/db/import-md5-list | 111 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 3 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index d7b0f1f..adca8a6 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -5,10 +5,115 @@ ## Options/Parameter # $1: -# -a | add files of md5-file to db -# -r | remove files of md5-file from db -# -d | mark files from md5-file ad deleted +# -a | add files of md5-file to db +# -r | remove files of md5-file from db +# -d | mark files from md5-file ad deleted +# -ee | export md5 of existing files +# -ed | export md5 of deleted fiels # # $2: # [$3]: +mode=$(echo $1 | tr -d '-') +md5="$2" +moviedir="$3" +globalconf="/etc/tt/global" + +#db +dbname="tt_test" +dbhost="localhost" +dbuser="root" +dbpwd="" + +function help() { + #help menue + echo "$0 is part of the TerabyteTerminator Project" + echo "" + echo "import-md5-list [moviedir]" + echo " modes:" + echo " -a add files of md5-file to db" + echo " -r remove files of md5-file from db" + echo " -d mark files from md5-file ad deleted" + echo " -ee export md5 of existing files" + echo " -ed export md5 of deleted fiels" + echo "" + echo " moviedir: Is optional if it is already in the global config" + + exit 0 +} +if [ "$mode" == "help" ] && help + +##check & prepair args +[ -f "$md5" ] || { echo "md5 \"$md5\" do not exist"; exit 1; } # check md5 +[ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist) + if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf) + echo 'neither "movie folder" is set in global config nor given as argument' + exit 2 + else # and (moviedir is in globalconf) + moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)" + fi +} +[ -d "$moviedir" ] || { echo "moviedir \"$moviedir\" do not exist"; exit 3; } # check moviedir + +## functions +function md5_in_add() { + exit 0 +} + +function md5_rm() { + exit 0 +} + +function md5_in_del() { + exit 0 +} + +function md5_out_exist() { + exit 0 +} + +function md5_out_del() { + exit 0 +} + +# help functions + +# db_query -> result +function db_query() { + echo $@ | mysql -h$dbhost -u$dbuser -p$dbpwd $dbname +} + +# db_exist_md5 -> true/false 0/1 +function db_exist_md5() { + result=($(db_query "SELECT COUNT(md5) FROM File WHERE File.MD5 LIKE '$1';")) + if [ "${result[2]}" -gt "0" ]; then + echo true + return 0 + else + echo false + return 1 + fi +} + +case "$mode" in + "a") #in_add + md5_in_add + ;; + "r") #rm + md5_rm + ;; + "d") + md5_in_del + ;; + "ee" | "e" ) + md5_out_exist + ;; + "ed" ) + md5_out_exist + ;; + *) + echo "wrong arguments" + echo "" + help + ;; +esac \ No newline at end of file From ba3ac3b2769ca0bae43783910af36ed9d8208ea6 Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 22:14:31 +0100 Subject: [PATCH 04/14] - modular db_query - empty password wont be asked animore --- tools/db/import-md5-list | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index adca8a6..497ae4c 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -38,7 +38,7 @@ function help() { echo " -ed export md5 of deleted fiels" echo "" echo " moviedir: Is optional if it is already in the global config" - + exit 0 } if [ "$mode" == "help" ] && help @@ -80,7 +80,12 @@ function md5_out_del() { # db_query -> result function db_query() { - echo $@ | mysql -h$dbhost -u$dbuser -p$dbpwd $dbname + local args="-sN" #no headers, no grid + args+=" -h$dbhost" + args+=" -u$dbuser" + [ -n "$dbpwd" ] && args+=" -p$dbpwd" + args+=" $dbname" + echo $@ | mysql $args } # db_exist_md5 -> true/false 0/1 @@ -116,4 +121,4 @@ case "$mode" in echo "" help ;; -esac \ No newline at end of file +esac From 00bcaff33df4bb0bbce61c0402d7d99730156d93 Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 22:16:45 +0100 Subject: [PATCH 05/14] beautify --- tools/db/import-md5-list | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index 497ae4c..6eccad3 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -48,7 +48,7 @@ if [ "$mode" == "help" ] && help [ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist) if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf) echo 'neither "movie folder" is set in global config nor given as argument' - exit 2 + exit 2 else # and (moviedir is in globalconf) moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)" fi @@ -57,23 +57,23 @@ if [ "$mode" == "help" ] && help ## functions function md5_in_add() { - exit 0 + exit 0 } function md5_rm() { - exit 0 + exit 0 } function md5_in_del() { - exit 0 + exit 0 } function md5_out_exist() { - exit 0 + exit 0 } function md5_out_del() { - exit 0 + exit 0 } # help functions @@ -90,14 +90,14 @@ function db_query() { # db_exist_md5 -> true/false 0/1 function db_exist_md5() { - result=($(db_query "SELECT COUNT(md5) FROM File WHERE File.MD5 LIKE '$1';")) - if [ "${result[2]}" -gt "0" ]; then - echo true - return 0 - else - echo false - return 1 - fi + result=($(db_query "SELECT COUNT(md5) FROM File WHERE File.MD5 LIKE '$1';")) + if [ "${result[2]}" -gt "0" ]; then + echo true + return 0 + else + echo false + return 1 + fi } case "$mode" in @@ -118,7 +118,7 @@ case "$mode" in ;; *) echo "wrong arguments" - echo "" - help + echo "" + help ;; esac From 4b4b45e2b1bd8b8faf13e63c343a6b9a0f254e91 Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 22:23:19 +0100 Subject: [PATCH 06/14] use logick (true=1; false=0;) --- tools/db/import-md5-list | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index 6eccad3..ab40c54 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -14,6 +14,11 @@ # $2: # [$3]: +{ #set enviroemnt + true=1 #like + false=0 #like +} + mode=$(echo $1 | tr -d '-') md5="$2" moviedir="$3" @@ -93,10 +98,10 @@ function db_exist_md5() { result=($(db_query "SELECT COUNT(md5) FROM File WHERE File.MD5 LIKE '$1';")) if [ "${result[2]}" -gt "0" ]; then echo true - return 0 + return $true else echo false - return 1 + return $false fi } From 6174c863dfd943f2d73cf61e003dc81358500ec8 Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 22:49:08 +0100 Subject: [PATCH 07/14] add draft for md5_in_add --- tools/db/import-md5-list | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index ab40c54..6698224 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -62,6 +62,16 @@ if [ "$mode" == "help" ] && help ## functions function md5_in_add() { + + cat data_shares_Filme | while read line; do + md5=$(echo $line | cut -c -32) + path=$(echo $line | cut -c 33- | sed 's/^\ //g') + relrevpath=$(echo $path | sed "s|$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) + name=$(echo $relrevpath | cut -d '/' -f 1 | rev) + folder=$(echo $relrevpath | cut -d '/' -f 2- | rev) + db_query "INSERT INTO File (MD5, Folder, Name) VALUES ('$md5', '$folder', '$name' );" #-> handle in future: ";'()" + done + exit 0 } From 53e7bc984ad419f442996e65e6607671a318e904 Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 22:51:17 +0100 Subject: [PATCH 08/14] group global variables --- tools/db/import-md5-list | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index 6698224..bfe05e3 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -17,18 +17,21 @@ { #set enviroemnt true=1 #like false=0 #like + globalconf="/etc/tt/global" } -mode=$(echo $1 | tr -d '-') -md5="$2" -moviedir="$3" -globalconf="/etc/tt/global" +{ #get options & args + mode=$(echo $1 | tr -d '-') + md5="$2" + moviedir="$3" +} -#db -dbname="tt_test" -dbhost="localhost" -dbuser="root" -dbpwd="" +{ #db + dbname="tt_test" + dbhost="localhost" + dbuser="root" + dbpwd="" +} function help() { #help menue From c68841959edc4814454a77def8a17ab85a208c71 Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 22:58:52 +0100 Subject: [PATCH 09/14] first working md5_in_add (with special sign bug) --- tools/db/import-md5-list | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index bfe05e3..d59a9d4 100644 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -65,11 +65,10 @@ if [ "$mode" == "help" ] && help ## functions function md5_in_add() { - - cat data_shares_Filme | while read line; do + cat $md5 | while read line; do #for each line in md5 md5=$(echo $line | cut -c -32) path=$(echo $line | cut -c 33- | sed 's/^\ //g') - relrevpath=$(echo $path | sed "s|$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) + relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) name=$(echo $relrevpath | cut -d '/' -f 1 | rev) folder=$(echo $relrevpath | cut -d '/' -f 2- | rev) db_query "INSERT INTO File (MD5, Folder, Name) VALUES ('$md5', '$folder', '$name' );" #-> handle in future: ";'()" From c6e1c7fe4977d71fde2ecb591389178b18b60dc0 Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 22:59:41 +0100 Subject: [PATCH 10/14] chmod --- tools/db/import-md5-list | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 tools/db/import-md5-list diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list old mode 100644 new mode 100755 From e3298b92729dadfee1140d45d4020763e15fd5af Mon Sep 17 00:00:00 2001 From: 6543 Date: Thu, 7 Feb 2019 23:40:32 +0100 Subject: [PATCH 11/14] add function db_F-ID_getPath -> --- tools/db/import-md5-list | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index d59a9d4..100ae01 100755 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -62,6 +62,8 @@ if [ "$mode" == "help" ] && help fi } [ -d "$moviedir" ] || { echo "moviedir \"$moviedir\" do not exist"; exit 3; } # check moviedir +moviedir=""$(echo $moviedir | sed 's|/$||g')"" +[ -z "$moviedir" ] && exit 255 #catch case moviedir="/" ## functions function md5_in_add() { @@ -117,6 +119,14 @@ function db_exist_md5() { fi } +# db_F-ID_getPath -> +function db_id_getPath() { + local ID=$1 + local Folder=$(db_query "SELECT Folder FROM File WHERE ID=$ID;") + local Name=$(db_query "SELECT Name FROM File WHERE ID=$ID;") + echo "$moviedir/$Folder/$Name" +} + case "$mode" in "a") #in_add md5_in_add From f898d4b5c19337835880b1531074c730f98ad054 Mon Sep 17 00:00:00 2001 From: 6543 Date: Fri, 8 Feb 2019 00:15:44 +0100 Subject: [PATCH 12/14] USE Const Vars - Highlight Const. (UPERCASE) - Add dumy md5 export fucntions --- tools/db/import-md5-list | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index 100ae01..243bdfb 100755 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -15,14 +15,18 @@ # [$3]: { #set enviroemnt - true=1 #like - false=0 #like - globalconf="/etc/tt/global" + readonly TRUE=1 #like + readonly FALSE=0 #like + readonly TTSYNC_EXIST=0 + readonly TTSYNC_BACKUP=1 + readonly TTSYNC_REMOTE=2 + readonly TTSYNC_DEL=3 + readonly globalconf="/etc/tt/global" } { #get options & args - mode=$(echo $1 | tr -d '-') - md5="$2" + readonly mode=$(echo $1 | tr -d '-') + readonly md5="$2" moviedir="$3" } @@ -52,6 +56,7 @@ function help() { if [ "$mode" == "help" ] && help ##check & prepair args +[ ${mode:0:1} == "e" ] && touch "$md5" [ -f "$md5" ] || { echo "md5 \"$md5\" do not exist"; exit 1; } # check md5 [ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist) if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf) @@ -73,7 +78,7 @@ function md5_in_add() { relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) name=$(echo $relrevpath | cut -d '/' -f 1 | rev) folder=$(echo $relrevpath | cut -d '/' -f 2- | rev) - db_query "INSERT INTO File (MD5, Folder, Name) VALUES ('$md5', '$folder', '$name' );" #-> handle in future: ";'()" + db_query "INSERT INTO File (MD5, Folder, Name, TTSync) VALUES ('$md5', '$folder', '$name', $TTSYNC_EXIST );" #-> handle in future: ";'()" done exit 0 @@ -88,10 +93,12 @@ function md5_in_del() { } function md5_out_exist() { + db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" exit 0 } function md5_out_del() { + db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_DEL;" exit 0 } @@ -112,19 +119,16 @@ function db_exist_md5() { result=($(db_query "SELECT COUNT(md5) FROM File WHERE File.MD5 LIKE '$1';")) if [ "${result[2]}" -gt "0" ]; then echo true - return $true + return $TRUE else echo false - return $false + return $FALSE fi } # db_F-ID_getPath -> function db_id_getPath() { - local ID=$1 - local Folder=$(db_query "SELECT Folder FROM File WHERE ID=$ID;") - local Name=$(db_query "SELECT Name FROM File WHERE ID=$ID;") - echo "$moviedir/$Folder/$Name" + db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE ID=$1;" } case "$mode" in From ac62cf4935279445c18eeddbce97aa1381df1fc5 Mon Sep 17 00:00:00 2001 From: 6543 Date: Fri, 8 Feb 2019 00:16:38 +0100 Subject: [PATCH 13/14] add md5 export ability --- tools/db/import-md5-list | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index 243bdfb..5abf887 100755 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -93,12 +93,12 @@ function md5_in_del() { } function md5_out_exist() { - db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" + db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" >> $md5 exit 0 } function md5_out_del() { - db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_DEL;" + db_query "SELECT CONCAT(MD5, ' ', ' ' ,'$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_DEL;" >> $md5 exit 0 } From 46a51314c7ddf73ccf591aa34991c7b89db08231 Mon Sep 17 00:00:00 2001 From: 6543 Date: Fri, 8 Feb 2019 00:31:42 +0100 Subject: [PATCH 14/14] first working tool (with special sign bug) --- tools/db/import-md5-list | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tools/db/import-md5-list b/tools/db/import-md5-list index 5abf887..0875792 100755 --- a/tools/db/import-md5-list +++ b/tools/db/import-md5-list @@ -73,11 +73,11 @@ moviedir=""$(echo $moviedir | sed 's|/$||g')"" ## functions function md5_in_add() { cat $md5 | while read line; do #for each line in md5 - md5=$(echo $line | cut -c -32) - path=$(echo $line | cut -c 33- | sed 's/^\ //g') - relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) - name=$(echo $relrevpath | cut -d '/' -f 1 | rev) - folder=$(echo $relrevpath | cut -d '/' -f 2- | rev) + local md5=$(echo $line | cut -c -32) + local path=$(echo $line | cut -c 33- | sed 's/^\ //g') + local relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) + local name=$(echo $relrevpath | cut -d '/' -f 1 | rev) + local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev) db_query "INSERT INTO File (MD5, Folder, Name, TTSync) VALUES ('$md5', '$folder', '$name', $TTSYNC_EXIST );" #-> handle in future: ";'()" done @@ -85,10 +85,28 @@ function md5_in_add() { } function md5_rm() { + cat $md5 | while read line; do #for each line in md5 + local md5=$(echo $line | cut -c -32) + local path=$(echo $line | cut -c 33- | sed 's/^\ //g') + local relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) + local name=$(echo $relrevpath | cut -d '/' -f 1 | rev) + local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev) + db_query "DELETE FROM File WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';" #-> handle in future: ";'()" + done + exit 0 } function md5_in_del() { + cat $md5 | while read line; do #for each line in md5 + local md5=$(echo $line | cut -c -32) + local path=$(echo $line | cut -c 33- | sed 's/^\ //g') + local relrevpath=$(echo $path | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev) + local name=$(echo $relrevpath | cut -d '/' -f 1 | rev) + local folder=$(echo $relrevpath | cut -d '/' -f 2- | rev) + db_query "UPDATE File SET TTSync=$TTSYNC_DEL WHERE MD5='$md5' AND Folder='$folder' AND Name='$name';" #-> handle in future: ";'()" + done + exit 0 }