ser_dbtext.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/bin/sh
  2. #
  3. # $Id$
  4. #
  5. # SER dbtext Database Administration Tool
  6. #
  7. # TODO: Check if user and group exist
  8. #
  9. # Copyright (C) 2006-2008 iptelorg GmbH
  10. #
  11. #################################################################
  12. # configuration variables
  13. #################################################################
  14. DEFAULT_ROOTDIR="/var/local/lib/ser" # Default dbtext root directory
  15. DEFAULT_DBNAME="ser" # Default database name
  16. DEFAULT_OWNER="ser" # The owner of dbtext files
  17. DEFAULT_GROUP="ser" # The group of dbtext files
  18. DEFAULT_SRCDB_DIR="" # Default directory of the source data
  19. DEFAULT_SRCDB="ser_db" # Source data generated from XML description
  20. usage() {
  21. cat <<EOF
  22. NAME
  23. $COMMAND - SER dbtext Database Administration Tool
  24. SYNOPSIS
  25. $COMMAND [options] create
  26. $COMMAND [options] drop
  27. $COMMAND [options] backup [filename.tar]
  28. $COMMAND [options] restore [filename.tar]
  29. DESCRIPTION
  30. This tool is a simple shell wrapper that can be used to create, drop, or
  31. backup SER database stored in plain-text files on the filesystem (used by
  32. dbtext SER module). See section COMMANDS for brief overview of supported
  33. actions.
  34. The database template for SER dbtext database is stored in dbtext_template
  35. directory which can usualy be found in /var/lib/ser (depending on
  36. installation). You can use the template to create SER database manually if
  37. you cannot or do not want to use this shell wrapper.
  38. COMMANDS
  39. create
  40. Create a new SER database from scratch. The database must not exist. This
  41. command creates the database, the default name of the database is
  42. '${DEFAULT_DBNAME}' (the default name can be changed using a command line
  43. parameter, see below). The database will be created in the default dbtext
  44. database directory (${DEFAULT_ROOTDIR}) unless changed using -d command
  45. line option (see below). You can use command line options to change the
  46. default database name, owner username and group.
  47. drop
  48. This command can be used to delete SER database. WARNING: This command
  49. will delete all data in the database and this action cannot be undone
  50. afterwards. Make sure that you have backups if you want to keep the data
  51. from the database.
  52. backup <filename>
  53. Backup the contents of SER database. If you specify a filename then the
  54. contents of the database will be saved in that file, otherwise the tool
  55. will dumps the contents on the standard output.
  56. restore <filename>
  57. Load the contents of SER database from a file (if you specify one) or from
  58. the standard input.
  59. OPTIONS
  60. -h, --help
  61. Display this help text.
  62. -n NAME, --name=NAME
  63. Database name of SER database.
  64. (Default value is '$DEFAULT_DBNAME')
  65. -d DIR, --dir=DIR
  66. Root directory of dbtext databases.
  67. (Default value is '$DEFAULT_ROOTDIR')
  68. -o USERNAME, --owner=USERNAME
  69. Owner of files in the database.
  70. (Default value is '$DEFAULT_OWNER')
  71. -g GROUP, --group=GROUP
  72. Group of files in the database.
  73. (Default value is '$DEFAULT_GROUP')
  74. -v, --verbose
  75. Enable verbose mode. This option can be given multiple times
  76. to produce more and more output.
  77. AUTHOR
  78. Written by Jan Janak <[email protected]>
  79. COPYRIGHT
  80. Copyright (C) 2006-2008 iptelorg GmbH
  81. This is free software. You may redistribute copies of it under the termp of
  82. the GNU General Public License. There is NO WARRANTY, to the extent
  83. permitted by law.
  84. FILES
  85. ${SRCDB_DIR}/${SRCDB}
  86. REPORTING BUGS
  87. Report bugs to <[email protected]>
  88. EOF
  89. } #usage
  90. dbg()
  91. {
  92. if [ ! -z $VERBOSE ] ; then
  93. echo $@
  94. fi
  95. }
  96. err()
  97. {
  98. echo "ERROR: $@"
  99. }
  100. # Dump the contents of the database to stdout
  101. backup_db()
  102. {
  103. if [ ! -z $VERBOSE ] ; then
  104. (cd "${ROOTDIR}/$1"; tar cfv - *)
  105. else
  106. (cd "${ROOTDIR}/$1"; tar cf - * 2>/dev/null)
  107. fi
  108. }
  109. # Re-create the contents of database from a tar file
  110. restore_db()
  111. {
  112. if [ ! -d "${ROOTDIR}/$1" ] ; then
  113. err "Database $1 does not exist, create it first"
  114. exit 1
  115. fi
  116. if [ ! -z $VERBOSE ] ; then
  117. (cd $ROOTDIR/$1; rm -f *; tar xfv -)
  118. else
  119. (cd $ROOTDIR/$1; rm -f *; tar xf -)
  120. fi
  121. }
  122. # Drop SER database
  123. drop_db()
  124. {
  125. dbg "Removing dbtext database directory ${ROOTDIR}/${DBNAME}"
  126. rm -rf "${ROOTDIR}/${DBNAME}"
  127. } # drop_db
  128. # Create SER database
  129. create_db ()
  130. {
  131. dbg "Creating database directory ${ROOTDIR}/${DBNAME}"
  132. mkdir -p "${ROOTDIR}/${DBNAME}"
  133. if [ ! -d ] ; then
  134. err "Could not create directory ${ROOTDIR}/${DBNAME}"
  135. exit 1
  136. fi
  137. dbg "Copying template files from ${SRCDB_DIR}/${SRCDB} to ${ROOTDIR}/${DBNAME}"
  138. cp -a ${SRCDB_DIR}/${SRCDB}/* "${ROOTDIR}/${DBNAME}"
  139. dbg "Setting owner and group of new files to ${OWNER}:${GROUP}"
  140. chown -R "${OWNER}:${GROUP}" "${ROOTDIR}/${DBNAME}"
  141. chmod -R 0660 ${ROOTDIR}/${DBNAME}/*
  142. } # create_db
  143. # Convert relative path to the script directory to absolute if necessary by
  144. # extracting the directory of this script and prefixing the relative path with
  145. # it.
  146. abs_srcdb_dir()
  147. {
  148. my_dir=`dirname $0`;
  149. if [ "${SRCDB_DIR:0:1}" != "/" ] ; then
  150. SCRIPT_DIR="${my_dir}/${SRCDB_DIR}"
  151. fi
  152. }
  153. # Main program
  154. COMMAND=`basename $0`
  155. if [ -z "$DBNAME" ] ; then DBNAME="$DEFAULT_DBNAME"; fi;
  156. if [ -z "$OWNER" ] ; then OWNER="$DEFAULT_OWNER"; fi;
  157. if [ -z "$GROUP" ] ; then GROUP="$DEFAULT_GROUP"; fi;
  158. if [ -z "$SRCDB_DIR" ] ; then SRCDB_DIR="$DEFAULT_SRCDB_DIR"; fi;
  159. if [ -z "$SRCDB" ] ; then SRCDB="$DEFAULT_SRCDB"; fi
  160. abs_srcdb_dir
  161. TEMP=`getopt -o hn:d:o:g:v --long help,name:,dir:,owner:,group:,verbose -n $COMMAND -- "$@"`
  162. if [ $? != 0 ] ; then exit 1; fi
  163. eval set -- "$TEMP"
  164. while true ; do
  165. case "$1" in
  166. -h|--help) usage; exit 0 ;;
  167. -n|--name) DBNAME=$2; shift 2 ;;
  168. -d|--dir) ROOTDIR=$2; shift 2 ;;
  169. -o|--owner) OWNER=$2; shift 2 ;;
  170. -g|--group) GROUP=$2; shift 2 ;;
  171. -v|--verbose) export OPTS="${OPTS} -v "; VERBOSE=1; shift ;;
  172. --) shift; break ;;
  173. *) echo "Internal error"; exit 1 ;;
  174. esac
  175. done
  176. if [ $# -eq 0 ]; then
  177. usage
  178. exit 1
  179. fi
  180. TEMP=`id $OWNER > /dev/null 2>&1`
  181. if [ $? != 0 ] ; then
  182. err "User '$OWNER' does not exist"
  183. exit 1
  184. fi
  185. TEMP=`id -g $GROUP > /dev/null 2>&1`
  186. if [ $? != 0 ] ; then
  187. err "Group '$GROUP' does not exist"
  188. exit 1
  189. fi
  190. case $1 in
  191. create) # Create SER database
  192. create_db
  193. exit $?
  194. ;;
  195. drop) # Drop SER database
  196. drop_db
  197. exit $?
  198. ;;
  199. backup) # backup SER database
  200. shift
  201. if [ $# -eq 1 ]; then
  202. dbg "Backing up ${ROOTDIR}/$DBNAME"
  203. backup_db $DBNAME > $1
  204. elif [ $# -eq 0 ]; then
  205. dbg "Backing up ${ROOTDIR}/$DBNAME"
  206. backup_db $DBNAME
  207. else
  208. usage
  209. exit 1
  210. fi
  211. exit $?
  212. ;;
  213. restore) # restore SER database
  214. shift
  215. if [ $# -eq 1 ]; then
  216. dbg "Restoring ${ROOTDIR}/$DBNAME"
  217. cat $1 | restore_db $DBNAME
  218. elif [ $# -eq 0 ]; then
  219. dbg "Restoring ${ROOTDIR}/$DBNAME"
  220. restore_db $DBNAME
  221. else
  222. usage
  223. exit 1
  224. fi
  225. exit $?
  226. ;;
  227. *)
  228. echo "Unknown command '$1'"
  229. usage
  230. exit 1;
  231. ;;
  232. esac