beautysh tools/db/update-file-state

This commit is contained in:
6543 2019-05-11 01:38:31 +02:00
parent a6abdc45ea
commit eb0354cf87

View File

@ -7,47 +7,47 @@
# [$1]: <path to movie folder> # [$1]: <path to movie folder>


{ #set enviroemnt { #set enviroemnt
readonly TRUE=1 #like <stdbool.h> readonly TRUE=1 #like <stdbool.h>
readonly FALSE=0 #like <stdbool.h> readonly FALSE=0 #like <stdbool.h>
readonly TTSYNC_EXIST=0 #file exist local readonly TTSYNC_EXIST=0 #file exist local
readonly TTSYNC_BACKUP=1 #file exist local - backup exist readonly TTSYNC_BACKUP=1 #file exist local - backup exist
readonly TTSYNC_REMOTE=2 #file exists remote - not used jet readonly TTSYNC_REMOTE=2 #file exists remote - not used jet
readonly TTSYNC_DEL=3 #local file deleted readonly TTSYNC_DEL=3 #local file deleted
readonly globalconf="/etc/tt/global" readonly globalconf="/etc/tt/global"
} }


{ #get options & args { #get options & args
readonly mode=$(echo $1 | tr -d '-') readonly mode=$(echo $1 | tr -d '-')
moviedir="$1" moviedir="$1"
[ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist) [ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist)
if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf) 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' echo 'neither "movie folder" is set in global config nor given as argument'
exit 2 exit 2
else # and (moviedir is in globalconf) else # and (moviedir is in globalconf)
moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)" moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)"
fi fi
} }
[ -d "$moviedir" ] || { echo "moviedir \"$moviedir\" do not exist"; exit 3; } # check moviedir [ -d "$moviedir" ] || { echo "moviedir \"$moviedir\" do not exist"; exit 3; } # check moviedir
moviedir=""$(echo $moviedir | sed 's|/$||g')"" moviedir=""$(echo $moviedir | sed 's|/$||g')""
[ -z "$moviedir" ] && exit 255 #catch case moviedir="/" [ -z "$moviedir" ] && exit 255 #catch case moviedir="/"
readonly DATE=$(date '+%Y-%m-%d') readonly DATE=$(date '+%Y-%m-%d')
} }


{ #db { #db
dbname="tt_test" dbname="tt_test"
dbhost="localhost" dbhost="localhost"
dbuser="root" dbuser="root"
dbpwd="" dbpwd=""
} }


function help() { function help() {
#help menue #help menue
echo "$0 is part of the TerabyteTerminator Project" echo "$0 is part of the TerabyteTerminator Project"
echo "" echo ""
echo "update-file-state [moviedir]" echo "update-file-state [moviedir]"
echo " moviedir: Is optional if it is already in the global config" echo " moviedir: Is optional if it is already in the global config"


exit 0 exit 0
} }
[ "$mode" == "help" ] && help [ "$mode" == "help" ] && help


@ -55,67 +55,67 @@ function help() {
## Programm Haupt Ablauf ## ## Programm Haupt Ablauf ##
########################### ###########################
function main() { function main() {
#cd to moviedir #cd to moviedir
cd "$moviedir" cd "$moviedir"


#clean DB from old stuff #clean DB from old stuff
{ {
db_query "DELETE From File WHERE Size=0 OR MD5="" OR MD5 IS NULL;" db_query "DELETE From File WHERE Size=0 OR MD5="" OR MD5 IS NULL;"
} }


#erselle zwei dateilisten #erselle zwei dateilisten
list_tmp=$(mktemp -d) list_tmp=$(mktemp -d)
db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" > "$list_tmp/indb" db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" > "$list_tmp/indb"
find "$moviedir" -type f > "$list_tmp/exist" find "$moviedir" -type f > "$list_tmp/exist"


#find new files #find new files
grep -vf "$list_tmp/indb" "$list_tmp/exist" > "$list_tmp/new" grep -vf "$list_tmp/indb" "$list_tmp/exist" > "$list_tmp/new"


#all IDs wich an old check date: #all IDs wich an old check date:
db_query "SELECT ID FROM File Where TTCheck < '$DATE' OR TTCheck IS NULL;" | while read line; do db_query "SELECT ID FROM File Where TTCheck < '$DATE' OR TTCheck IS NULL;" | while read line; do
#for each ID with an old check date ... #for each ID with an old check date ...
ID=$line ID=$line
TTSync=$(db_query "SELECT TTSync FROM File WHERE ID=$ID;") TTSync=$(db_query "SELECT TTSync FROM File WHERE ID=$ID;")
FILE=$(db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE ID=$ID;") FILE=$(db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE ID=$ID;")
if [ -f "$FILE"]; then if [ -f "$FILE"]; then
SIZE=$(db_query "SELECT Size FROM File WHERE ID=$ID;") SIZE=$(db_query "SELECT Size FROM File WHERE ID=$ID;")
SIZE_new="$(file_size "$FILE")" SIZE_new="$(file_size "$FILE")"
[ "$SIZE" != "$SIZE_new" ] && { [ "$SIZE" != "$SIZE_new" ] && {
echo "FILE CHANGE: $FILE" echo "FILE CHANGE: $FILE"
if [ "0" == "$SIZE_new" ]; then if [ "0" == "$SIZE_new" ]; then
#file exist but empty #file exist but empty
#rm "$FILE" <- secure? 2check #rm "$FILE" <- secure? 2check
db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck=$DATE WHERE ID=$ID;" db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck=$DATE WHERE ID=$ID;"
else else
#recalc md5 ... + check #recalc md5 ... + check
#update DB #update DB
MD5_new=$(md5sum "$FILE" | cut -c -32) MD5_new=$(md5sum "$FILE" | cut -c -32)
MD5=$(db_query "SELECT MD5 FROM File WHERE ID=$ID;") MD5=$(db_query "SELECT MD5 FROM File WHERE ID=$ID;")
if [ "$MD5" == "$MD5_new" ]; then
db_query "UPDATE File SET Size=$SIZE_new,TTCheck=$DATE WHERE ID=$ID;"
else
#new file/file changed!
db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck=$DATE WHERE ID=$ID;"
file_add "$(db_query "SELECT CONCAT(Folder, '/', Name) FROM File WHERE ID=$ID;")"
fi


fi if [ "$MD5" == "$MD5_new" ]; then
} db_query "UPDATE File SET Size=$SIZE_new,TTCheck=$DATE WHERE ID=$ID;"
else else
# file dont exist #new file/file changed!
db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck=$DATE WHERE ID=$ID;" db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck=$DATE WHERE ID=$ID;"
fi file_add "$(db_query "SELECT CONCAT(Folder, '/', Name) FROM File WHERE ID=$ID;")"
fi


done fi
}
else
# file dont exist
db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck=$DATE WHERE ID=$ID;"
fi


#add new files .... done
cat "$list_tmp/new" | while read line; do
file_add "$line" #add new files ....
done cat "$list_tmp/new" | while read line; do
file_add "$line"
done




rm -r "$list_tmp/" rm -r "$list_tmp/"


} }


@ -125,12 +125,12 @@ done


##check & prepair args ##check & prepair args
[ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist) [ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist)
if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf) 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' echo 'neither "movie folder" is set in global config nor given as argument'
exit 2 exit 2
else # and (moviedir is in globalconf) else # and (moviedir is in globalconf)
moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)" moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)"
fi fi
} }
[ -d "$moviedir" ] || { echo "moviedir \"$moviedir\" do not exist"; exit 3; } # check moviedir [ -d "$moviedir" ] || { echo "moviedir \"$moviedir\" do not exist"; exit 3; } # check moviedir
moviedir=""$(echo $moviedir | sed 's|/$||g')"" moviedir=""$(echo $moviedir | sed 's|/$||g')""
@ -139,47 +139,47 @@ moviedir=""$(echo $moviedir | sed 's|/$||g')""


# file_size <path> -> (du -b)/1024 [Kb] # file_size <path> -> (du -b)/1024 [Kb]
function file_size() { function file_size() {
if [ -f "$1" ]; then if [ -f "$1" ]; then
echo $(($(du -b "$1" | cut -f 1)/1024)) echo $(($(du -b "$1" | cut -f 1)/1024))
else else
echo 0 echo 0
fi fi
} }


function file_add() { function file_add() {
echo 2DO echo 2DO
local relrevpath="$(echo $1 | sed 's|^/||g' | sed 's|/$||g' | rev | sed "s/'/''/g")" #last statement set ' as '' so it wont have an evect in the sql statement local relrevpath="$(echo $1 | sed 's|^/||g' | sed 's|/$||g' | rev | sed "s/'/''/g")" #last statement set ' as '' so it wont have an evect in the sql statement
local name="$(echo $relrevpath | cut -d '/' -f 1 | rev)" local name="$(echo $relrevpath | cut -d '/' -f 1 | rev)"
local folder="$(echo $relrevpath | cut -d '/' -f 2- | rev)" local folder="$(echo $relrevpath | cut -d '/' -f 2- | rev)"
local FILE="$moviedir/$(echo $relrevpath | rev)" local FILE="$moviedir/$(echo $relrevpath | rev)"
[ -f "$FILE" ] || return 1 #errer file not exist [ -f "$FILE" ] || return 1 #errer file not exist
local MD5_new=$(md5sum "$FILE" | cut -c -32) local MD5_new=$(md5sum "$FILE" | cut -c -32)
local SIZE_new="$(file_size "$FILE")" local SIZE_new="$(file_size "$FILE")"


#db path exist && md5 same -> update this entry #db path exist && md5 same -> update this entry
#db path exist && ttsync=exist/backup && md5 !same -> ttsync=del + new db entry #db path exist && ttsync=exist/backup && md5 !same -> ttsync=del + new db entry
#or new db entry #or new db entry


#db entry is{ #db entry is{
# chek if size ->md5 exist -> update path&name (MV) # chek if size ->md5 exist -> update path&name (MV)
# else complete new entry # else complete new entry
#} #}
} }




# db_query <query> -> result # db_query <query> -> result
function db_query() { function db_query() {
local args="-sN" #no headers, no grid local args="-sN" #no headers, no grid
args+=" -h$dbhost" args+=" -h$dbhost"
args+=" -u$dbuser" args+=" -u$dbuser"
[ -n "$dbpwd" ] && args+=" -p$dbpwd" [ -n "$dbpwd" ] && args+=" -p$dbpwd"
args+=" $dbname" args+=" $dbname"
echo $@ | mysql $args echo $@ | mysql $args
} }


# db_F-ID_getPath <F-ID> -> <Path> # db_F-ID_getPath <F-ID> -> <Path>
function db_id_getPath() { function db_id_getPath() {
db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE ID=$1;" db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE ID=$1;"
} }