#!/bin/bash #.Y / 70 83 / / / / 105 / / / / / / ;A4_QUER.Y #.YT 3 2 0 0 3 3 1 / / / #.H!a!0:linux//db/THIS!0.!c # # copyright : (C) 2009 by Thomas Bruecker =: tbrue # email : public0x05bf@bluewin.ch # # tbrue 26mar2009 # # db THIS : db-functions (database) # ++++++++++++++++++++++++++++++++++ # # MyHash("linux//db/THIS") = 0x083DH # # * for every function: # * if returned result is not commented, the function returns 0 on no-error. SCRIPTS=root/bin . /$SCRIPTS/io/f_/THIS # !!! pen: should be moved to "dos_/security" !!!: dos_readPassword() # reads a password and prints it to stdout. { local Password0x0XXX read -s -p "Enter Password: " Password0x0XXX echo >&2 # after entering the pass= # word we want a new line. echo -n "${Password0x0XXX}" # "-n" : no newline ! return 0 } db_AllTextsToDb() # $1 : user # * see "db_allTextsToDb()" . # * to be used as a shell-command; password has not to be entered on the # command-line. { local Password0x083D Password0x083D="$(dos_readPassword)" db_allTextsToDb "$1" "${Password0x083D}" return $? } db_CreateDbsFromAllTexts() # $1 : user # * see "db_createDbsFromAllTexts()" . # * to be used as a shell-command; password has not to be entered on the # command-line. { local Password0x083D Password0x083D="$(dos_readPassword)" db_createDbsFromAllTexts "$1" "${Password0x083D}" return $? } db_DropDbsOfAllTexts() # $1 : user # * see "db_dropDbsOfAllTexts()" . # * to be used as a shell-command; password has not to be entered on the # command-line. { local Password0x083D Password0x083D="$(dos_readPassword)" db_dropDbsOfAllTexts "$1" "${Password0x083D}" return $? } db_EveryToText() # $1 : user # * see "db_everyToText()" . # * to be used as a shell-command; password has not to be entered on the # command-line. { local Password0x083D Password0x083D="$(dos_readPassword)" db_everyToText "$1" "${Password0x083D}" return $? } db_allTextsToDb() # $1 : user # $2 : password # * tries to convert textfile (*.sql.txt) to the appropriate database. # * to be used in scripts; when used as a shell-command, THE PASSWORD IS KEPT # IN THE SHELL-HISTORY; no a good idea! { local Database0x083DH File0x083DH Result0x083DH for File0x083DH in *.sql.txt do if [ -f "${File0x083DH}" ]; then # "" exits (if # does not exist, "for" pro- # duces "*"; not ok.). Database0x083DH="$( db_filenameToDbname "${File0x083DH}" )" if [ "${Database0x083DH}" = "mysql" ]; then echo "refuses stritcly to process database ${Database0x083DH}; skips it." else echo "processes database: ${Database0x083DH}" mysql --password="$2" -u "$1" "${Database0x083DH}" \ < "${File0x083DH}" # Result0x083DH=$? # if [ ! "$Result0x083DH" = "0" ]; then return $Result0x083DH; fi # else # fi fi fi done return 0 } db_createDbsFromAllTexts() # $1 : user # $2 : password # * tries to create the appropriate database for textfile (*.sql.txt) . # * to be used in scripts; when used as a shell-command, THE PASSWORD IS KEPT # IN THE SHELL-HISTORY; no a good idea! { local Database0x083DH File0x083DH Result0x083DH for File0x083DH in *.sql.txt do if [ -f "${File0x083DH}" ]; then # "" exits (if # does not exist, "for" pro- # duces "*"; not ok.). Database0x083DH="$( db_filenameToDbname "${File0x083DH}" )" if [ "${Database0x083DH}" = "mysql" ]; then echo "refuses stritcly to create database ${Database0x083DH}; skips it." else echo "creates database: ${Database0x083DH}" echo CREATE DATABASE "${Database0x083DH}" \ CHARACTER SET = utf8 COLLATE = utf8_unicode_ci \ | mysql --password="$2" -u "$1" # Result0x083DH=$? # if [ ! "$Result0x083DH" = "0" ]; then return $Result0x083DH; fi # else # fi fi fi done return 0 } db_dropDbsOfAllTexts() # $1 : user # $2 : password # * tries to drop the appropriate database of textfile (*.sql.txt) . # * to be used in scripts; when used as a shell-command, THE PASSWORD IS KEPT # IN THE SHELL-HISTORY; no a good idea! { local Database0x083DH File0x083DH Result0x083DH for File0x083DH in *.sql.txt do if [ -f "${File0x083DH}" ]; then # "" exits (if # does not exist, "for" pro- # duces "*"; not ok.). Database0x083DH="$( db_filenameToDbname "${File0x083DH}" )" if [ "${Database0x083DH}" = "mysql" ]; then echo "refuses stritcly to process database ${Database0x083DH}; skips it." else echo "drops database: ${Database0x083DH}" echo DROP DATABASE "${Database0x083DH}" \ | mysql --password="$2" -u "$1" # Result0x083DH=$? # if [ ! "$Result0x083DH" = "0" ]; then return $Result0x083DH; fi # else # fi fi fi done return 0 } db_everyToText() # $1 : user # $2 : password # * converts every database in /var/lib/mysql to its text representation. # * to be used in scripts; when used as a shell-command, THE PASSWORD IS KEPT # IN THE SHELL-HISTORY; no a good idea! { local Database0x083DH File0x083DH Result0x083DH for File0x083DH in /var/lib/mysql/* do if [ -d "${File0x083DH}" ]; then # is a folder. Database0x083DH=$( f_PathToName "${File0x083DH}" ) if [ -f "${Database0x083DH}.sql.txt" ]; then # the file for # exists already. echo "refuses stritcly to write to the existing file:" echo " \"${Database0x083DH}.sql.txt\"," echo " exits." return 1 fi # else echo "processes database: ${Database0x083DH}" mysqldump --password="$2" -u "$1" "${Database0x083DH}" \ > "${Database0x083DH}.sql.txt" # Result0x083DH=$? # if [ ! "${Result0x083DH}" = "0" ]; then return ${Result0x083DH}; fi # else fi done return 0 } db_filenameToDbname() # $1 : filename # * converts filename to database-name and prints it to stdout. { echo $1 | sed 's/\.sql\.txt//' return 0 }