TerabyteTerminator/tools/db/bash-lib

56 lines
1.8 KiB
Bash

#!/bin/bash
#Bash-Library for TerabyteTerminator Project
LIBVER=1.1
{ #set enviroemnt
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
readonly globalconf="/etc/tt/global"
[ -z "$DEBUG" ] && readonly DEBUG=$FALSE
}
# 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
}
# 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
}
# 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"
[ -n "$dbport" ] && args+=" -P$dbport"
args+=" $dbname"
[ $DEBUG == $TRUE ] && echo $@ >&2
echo $@ | mysql $args
}
# config_load()
function config_load() {
#load db settings
readonly dbname=$(grep -v '#' $globalconf | grep ^dbname= | sed "s|^dbname=||g" | tr -d '"')
readonly dbhost=$(grep -v '#' $globalconf | grep ^dbhost= | sed "s|^dbhost=||g" | tr -d '"')
readonly dbuser=$(grep -v '#' $globalconf | grep ^dbuser= | sed "s|^dbuser=||g" | tr -d '"')
readonly dbpwd=$(grep -v '#' $globalconf | grep ^dbpwd= | sed "s|^dbpwd=||g" | tr -d '"')
readonly dbport=$(grep -v '#' $globalconf | grep ^dbpwd= | sed "s|^dbport=||g" | tr -d '"')
}