TerabyteTerminator/tools/db/bash-lib

88 lines
3.0 KiB
Plaintext
Raw Normal View History

2019-05-11 21:38:21 +00:00
#!/bin/bash
#Bash-Library for TerabyteTerminator Project
LIBVER=1.1
2019-05-11 21:38:21 +00:00
{ #set enviroemnt
2019-05-11 21:52:07 +00:00
readonly TRUE=1 #like <stdbool.h>
readonly FALSE=0 #like <stdbool.h>
readonly TTSYNC_EXIST=0 #file exist local
readonly TTSYNC_BACKUP=1 #file exist local - backup exist
readonly TTSYNC_REMOTE=2 #file exists remote - not used jet
readonly TTSYNC_DEL=3 #local file deleted
2019-05-11 21:38:21 +00:00
readonly globalconf="/etc/tt/global"
[ -z "$DEBUG" ] && readonly DEBUG=$FALSE
2019-05-11 22:03:26 +00:00
#ERROR Returns
readonly ERROR_CONFIG=2
2019-05-11 22:03:26 +00:00
readonly ERROR_DBLINK=20
2019-05-11 21:38:21 +00:00
}
2019-05-11 21:43:30 +00:00
# file_size <path> -> (du -b)/1024 [Kb]
function file_size() {
if [ -f "$1" ]; then
echo $(($(du -b "$1" | cut -f 1)/1024))
else
echo 0
fi
}
2019-05-11 21:46:28 +00:00
# db_check -> TRUE/FALSE
function db_check() {
db_query "SHOW GRANTS FOR CURRENT_USER();" > /dev/null 2> /dev/null
if [ $? == 0 ]; then
echo $TRUE
else
echo $FALSE
fi
}
2019-05-11 21:50:13 +00:00
# db_query <query> -> result
function db_query() {
local args="-sN" #no headers, no grid
args+=" -h$dbhost"
args+=" -u$dbuser"
[ -n "$dbpwd" ] && args+=" -p$dbpwd"
2019-05-11 21:55:29 +00:00
[ -n "$dbport" ] && args+=" -P$dbport"
2019-05-11 21:50:13 +00:00
args+=" $dbname"
[ $DEBUG == $TRUE ] && echo $@ >&2
echo $@ | mysql $args
}
# config_load()
function config_load() {
#load functions from LIB
if [ -f "$globalconf" ]; then
local config="$globalconf"
elif [ -f "$(readlink -f "$(dirname $0)/../../src/etc/tt/global")" ]; then
local config="$(readlink -f "$(dirname $0)/../../src/etc/tt/global")"
else
echo "ERROR: Global Config not found"
exit 255
fi
#load db settings
readonly dbname=$(grep -v '#' $config | grep ^dbname= | sed "s|^dbname=||g" | tr -d '"')
readonly dbhost=$(grep -v '#' $config | grep ^dbhost= | sed "s|^dbhost=||g" | tr -d '"')
readonly dbuser=$(grep -v '#' $config | grep ^dbuser= | sed "s|^dbuser=||g" | tr -d '"')
readonly dbpwd=$(grep -v '#' $config | grep ^dbpwd= | sed "s|^dbpwd=||g" | tr -d '"')
readonly dbport=$(grep -v '#' $config | grep ^dbpwd= | sed "s|^dbport=||g" | tr -d '"')
#set moviedir
{
moviedir="$(echo $moviedir | sed 's|/$||g')" #remove last '/'
[ -z "$moviedir" ] && [ -d "$globalconf" ] && { # if (no argument) and (globalconf exist)
if [ -z "$(grep "moviedir=" "$globalconf" )" ]; then # and (moviedir is NOT in globalconf)
echo 'ERROR: NO Guilty moviedir SET'
exit $ERROR_CONFIG
else # and (moviedir is in globalconf)
readonly moviedir="$(grep "moviedir=" "$globalconf" | cut -d '=' -f 2 | tr -d '"' | sed 's|/$||g')"
fi
}
[ -d "$moviedir" ] || {
echo 'ERROR: NO Guilty moviedir SET'
exit $ERROR_CONFIG
}
}
}