TerabyteTerminator/tools/db/update-file-state

185 lines
7.3 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# @author 6543
2019-05-11 21:02:43 +00:00
# @version 1.1
2019-05-11 17:34:51 +00:00
# @date 2019-05-11
## Options/Parameter
# [$1]: <path to movie folder>
2019-05-11 21:38:21 +00:00
#load functions from LIB
if [ -f "$(readlink -f "$(dirname $0)/bash-lib")" ]; then
source "$(readlink -f "$(dirname $0)/bash-lib")"
2019-05-11 21:41:58 +00:00
[ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
elif [ -f "<LIBDIR>/bash-lib" ]; then
2019-05-11 21:38:21 +00:00
source "<LIBDIR>/bash-lib"
2019-05-11 21:41:58 +00:00
[ $DEBUG == $TRUE ] && echo "LIB VERSION: $LIBVER" >&2
2019-05-11 21:38:21 +00:00
else
echo "ERROR: Global Lib not found"
exit 255
fi
#get options & args
readonly mode=$(echo $1 | tr -d '-')
2019-05-11 22:29:37 +00:00
[ -n "$1" ] && [ -d "$1" ] && moviedir="$1"
readonly DATE=$(date '+%Y-%m-%d')
#help function
[ "$mode" == "help" ] && {
2019-05-10 23:38:31 +00:00
#help menue
echo "$0 is part of the TerabyteTerminator Project"
echo ""
echo "update-file-state (moviedir)"
2019-05-10 23:38:31 +00:00
exit 0
}
#load config (DB,...)
config_load
###########################
## Programm Haupt Ablauf ##
###########################
function main() {
2019-05-10 23:38:31 +00:00
2019-05-11 00:34:52 +00:00
#db checks
[ "$(db_check)" == $FALSE ] && {
echo "DB ERROR!!" >&2
2019-05-11 22:03:26 +00:00
exit $ERROR_DBLINK
}
2019-05-11 00:34:52 +00:00
2019-05-10 23:38:31 +00:00
#clean DB from old stuff
{
2019-05-11 17:30:09 +00:00
db_query "DELETE From File WHERE (Size=0 OR Size IS NULL OR MD5='' OR MD5 IS NULL OR Name='') AND (TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP);"
db_query "DELETE FROM File WHERE Folder LIKE 'Clone/%' OR Folder LIKE '.Trash-%';"
2019-05-11 01:36:54 +00:00
#find dublicates !
2019-05-10 23:38:31 +00:00
}
#erselle zwei dateilisten
list_tmp=$(mktemp -d)
db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP;" > "$list_tmp/indb"
find "$moviedir" -type f -size +1k | grep -v '/Clone/' | grep -v '/.Trash-' > "$list_tmp/exist"
2019-05-10 23:38:31 +00:00
#find new files
grep -vf "$list_tmp/indb" "$list_tmp/exist" > "$list_tmp/new"
#all IDs wich an old check date:
2019-05-11 21:02:43 +00:00
db_query "SELECT ID FROM File Where (TTCheck < '$DATE' OR TTCheck IS NULL) AND (TTSync=$TTSYNC_EXIST OR TTSync=$TTSYNC_BACKUP);" | while read line; do
2019-05-10 23:38:31 +00:00
#for each ID with an old check date ...
2019-05-11 15:19:14 +00:00
ID=$line
[ $DEBUG == $TRUE ] && echo "Progress ID: $ID" >&2
2019-05-10 23:38:31 +00:00
FILE=$(db_query "SELECT CONCAT('$moviedir/', Folder, '/', Name) FROM File WHERE ID=$ID;")
2019-05-11 00:25:21 +00:00
if [ -f "$FILE" ]; then
2019-05-10 23:38:31 +00:00
SIZE=$(db_query "SELECT Size FROM File WHERE ID=$ID;")
SIZE_new="$(file_size "$FILE")"
2019-05-11 21:02:43 +00:00
if [ "$SIZE" != "$SIZE_new" ]; then
2019-05-10 23:38:31 +00:00
echo "FILE CHANGE: $FILE"
if [ "0" == "$SIZE_new" ]; then
2019-05-11 20:42:13 +00:00
#file exist but to smal
2019-05-11 19:51:02 +00:00
echo "FILE EMPTY: $FILE"
2019-05-11 00:25:21 +00:00
db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck='$DATE' WHERE ID=$ID;"
2019-05-10 23:38:31 +00:00
else
#recalc md5 ... + check
#update DB
MD5_new=$(md5sum "$FILE" | cut -c -32)
MD5=$(db_query "SELECT MD5 FROM File WHERE ID=$ID;")
if [ "$MD5" == "$MD5_new" ]; then
2019-05-11 00:25:21 +00:00
db_query "UPDATE File SET Size=$SIZE_new,TTCheck='$DATE' WHERE ID=$ID;"
2019-05-10 23:38:31 +00:00
else
#new file/file changed!
2019-05-11 00:25:21 +00:00
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;")" "$MD5_new"
2019-05-10 23:38:31 +00:00
fi
fi
2019-05-11 21:02:43 +00:00
else
#add timestamp that nothing has changed
db_query "UPDATE File SET TTCheck='$DATE' WHERE ID=$ID;"
fi
2019-05-10 23:38:31 +00:00
else
# file dont exist
2019-05-11 19:51:02 +00:00
echo "FILE DEL/MV: $FILE"
2019-05-11 00:25:21 +00:00
db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck='$DATE' WHERE ID=$ID;"
2019-05-10 23:38:31 +00:00
fi
done
#add new files ....
cat "$list_tmp/new" | while read line; do
file_add "$line"
done
rm -r "$list_tmp/"
}
###########################
## help functions #########
###########################
##check & prepair args
2019-05-11 14:51:51 +00:00
[ -z "$moviedir" ] && [ -f "$globalconf" ] && { # if (no argument) and (globalconf exist)
if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf)
2019-05-10 23:38:31 +00:00
echo 'neither "movie folder" is set in global config nor given as argument'
exit 2
2019-05-11 14:51:51 +00:00
else # and (moviedir is in globalconf)
2019-05-10 23:38:31 +00:00
moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2)"
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="/"
2019-05-11 00:25:21 +00:00
# file_add [rel.path] (md5)
2019-05-10 18:50:14 +00:00
function file_add() {
2019-05-11 00:25:21 +00:00
local MD5="$2"
2019-05-11 20:42:13 +00:00
local relrevpath="$(echo "$1" | sed "s|^$moviedir||g" | sed 's|^/||g' | sed 's|/$||g' | rev )"
local name="$(echo "$relrevpath" | cut -d '/' -f 1 | rev | sed "s/'/''/g")" #cahr ' set to '' because of SQL Querys
local folder="$(echo "$relrevpath" | cut -d '/' -f 2- | rev | sed "s/'/''/g")" #cahr ' set to '' because of SQL Querys
2019-05-11 00:25:21 +00:00
local FILE="$moviedir/$(echo "$relrevpath" | rev)"
2019-05-11 00:58:47 +00:00
[ -f "$FILE" ] || {
2019-05-11 19:51:02 +00:00
echo "ERROR: file_add: file not exist: $FILE" >&2
2019-05-11 00:58:47 +00:00
return 1
}
2019-05-11 15:19:14 +00:00
[ $DEBUG == $TRUE ] && echo "add: $name" >&2
2019-05-11 00:25:21 +00:00
[ -z "$MD5" ] && MD5="$(md5sum "$FILE" | cut -c -32)"
local SIZE="$(file_size "$FILE")"
2019-05-11 14:51:51 +00:00
[ "$SIZE" -le 0 ] && {
[ $DEBUG == $TRUE ] && echo File to smal >&2
2019-05-11 14:51:51 +00:00
return 0
}
2019-05-10 23:38:31 +00:00
2019-05-11 00:58:47 +00:00
#wenn einmal existiert ...
2019-05-11 01:53:55 +00:00
{
2019-05-11 01:36:54 +00:00
# undelet files ...
2019-05-11 02:03:20 +00:00
local IDs_undel="$(db_query "SELECT ID FROM File WHERE TTSync='$TTSYNC_DEL' AND MD5='$MD5' AND Name='$name' AND Folder='$folder';")"
2019-05-11 15:19:14 +00:00
[ $DEBUG == $TRUE ] && echo "IDs_undel: $IDs_undel" >&2
2019-05-11 20:42:13 +00:00
[ -n "$IDs_undel" ] && echo "FILE UNDEL: $FILE"
2019-05-11 16:06:24 +00:00
db_query "UPDATE File SET TTSync=$TTSYNC_EXIST,TTCheck='$DATE',Size='$SIZE' WHERE TTSync='$TTSYNC_DEL' AND MD5='$MD5' AND Name='$name' AND Folder='$folder';"
2019-05-11 01:36:54 +00:00
#same path wrong md5
2019-05-11 15:19:14 +00:00
[ $DEBUG == $TRUE ] && echo "IDs_W5: $(db_query "SELECT ID FROM File WHERE TTSync!='$TTSYNC_REMOTE' AND TTSync!='$TTSYNC_DEL' AND MD5!='$MD5' AND Name='$name' AND Folder='$folder';")" >&2
2019-05-11 14:51:51 +00:00
db_query "UPDATE File SET TTSync=$TTSYNC_DEL,TTCheck='$DATE' WHERE TTSync!='$TTSYNC_REMOTE' AND TTSync!='$TTSYNC_DEL' AND MD5!='$MD5' AND Name='$name' AND Folder='$folder';"
2019-05-11 01:36:54 +00:00
#if something MVs ...
2019-05-11 14:51:51 +00:00
local IDs_mv="$(db_query "SELECT ID FROM File WHERE MD5='$MD5' AND Size='$SIZE' AND (Name!='$name' OR Folder!='$folder');")"
2019-05-11 15:19:14 +00:00
[ $DEBUG == $TRUE ] && echo "IDs_mv: $IDs_mv" >&2
2019-05-11 20:44:41 +00:00
[ -n "$IDs_mv" ] && echo "FILE MV: $(db_query "SELECT CONCAT(Folder,Name) FROM File WHERE ID=$(echo $IDs_mv | cut -d ' ' -f1);") -> $FILE"
2019-05-11 14:51:51 +00:00
db_query "UPDATE File SET TTCheck='$DATE',Name='$name',Folder='$folder',TTSync='$TTSYNC_EXIST' WHERE MD5='$MD5' AND Size='$SIZE' AND (Name!='$name' OR Folder!='$folder');"
2019-05-11 02:03:20 +00:00
#es existiert genau so ein eintrag
local IDs_orig="$(db_query "SELECT ID FROM File WHERE MD5='$MD5' AND Name='$name' AND Folder='$folder';")"
2019-05-11 15:19:14 +00:00
[ $DEBUG == $TRUE ] && echo "IDs_orig: $IDs_orig" >&2
2019-05-11 01:53:55 +00:00
}
2019-05-11 01:36:54 +00:00
2019-05-11 01:53:55 +00:00
#wenn weder gelöschte wiederhergestellt noch verschobene dateien entdekt... -> neuer eintrag
2019-05-11 20:42:13 +00:00
[ "$((${#IDs_mv}+${#IDs_undel}+${#IDs_orig}))" == 0 ] && {
echo "FILE ADD: $FILE"
db_query "INSERT INTO File (Name,Folder,MD5,Size,TTSync,TTCheck) VALUES ('$name','$folder','$MD5','$SIZE',$TTSYNC_EXIST,'$DATE');"
}
2019-05-10 23:38:31 +00:00
2019-05-10 18:50:14 +00:00
}
2019-05-11 21:50:13 +00:00
#all loaded stat now
main