热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用shell写的postgre数据库初始数据脚本

公司最近在使用postgresql数据库,开发给了我们很多ddldml语句,于是我就花了2天时间完成了这套脚本,用来创建表空间,用户,并且自动导入数据的脚本#!binbas

公司最近在使用postgresql数据库,开发给了我们很多ddl/dml语句,于是我就花了2天时间完成了这套脚本,用来创建表空间,用户,并且自动导入数据的脚本#!/bin/bas

公司最近在使用postgresql数据库,开发给了我们很多ddl/dml语句,于是我就花了2天时间完成了这套脚本,用来创建表空间,,用户,并且自动导入数据的脚本


#!/bin/bash # 2013/07/26, DD. # Usage: finshare_install_db.sh --dbname [ --userid ] [ --passwd ] [ --port ] [ --ctlfile ] [ --datadir ] [ -l ] [ --init ] [ --create ] [--help ] # FinShare DB SQL (DDL/DML) installation script for Postgre # postgre database install script # must use account postgres to login linux to run this script # must add account postgres to /etc/sudoers, and can execute mkdir、chown commands # must special one control file for this script, script will read this file to complete database initialization or execute DDL/DML script # Usage() { echo "Usage:" echo " $0 -d (to run DDL/DML script)" echo " $0 -d --create (to create a database)" echo " $0 -d --init -D (to initialize the database)" echo " " echo "Commands:" echo "Either long or short options are allowed." echo " -d, --dbname. database name" echo " -u, --userid. database user name, default is (postgres)" echo " -p, --passwd. user postgres's password" echo " -P, --port. database connection port, default is 5432" echo " -f, --ctlfile. control file. default is .ctl in current directory" echo " -l, --logdir. log file directory. default is /tmp" echo " -c, --cerate. if the database does not exist, add this parameter to create" echo " a database" echo " -i, --init. to initialize the database" echo " -D, --datadir. directory to store data" echo " -h, --help. print help information" echo " " if [ "X$1" != "X" ]; then echo $1 fi if [ "$help" == "true" ] then echo " Control file can have comment lines which start with # and empty lines." echo " if run script has special --init option, script will read lines start wiht (tablespace:) in control file to create table space and account, other lines will be temporary ignored. after finished create, it will execute in order. " echo " if cannot find lines start with tablespace: in control file, then it fails." echo " To initial database, use following line:" echo " tablespace:tablespaceName1:tablespaceName2:tablespaceName3:tablespaceName{n}:SchemaName" echo " if not special --init option, it will ignore lines start with (tablespace:), and then execute sql (DDL\DML)files in order." echo " each line can only contains two fields, if contains more fields ,then it fails." echo " To install ddl/dml, use following line:" echo " filePath:Schemaname" echo " If control file is not provided in -F, then it will find the file with extension .ctl" echo " in current directory. if there are more than one .ctl files, then it fails." echo " The control file directory is the scripts root directory." echo " Command is to run a single sql script. It is the line in control file for example." echo " the command script root directory is current directory." echo " " echo "Note:" echo " In control file, all directory path use / (don't use \)." echo " " fi exit 1 } func_CheckError() { sqlErrFound=0 if [ -n "`grep -E '^psql|^ERROR:|does not exist$|already exists$|No such file$' ${logfileTmp}`" ] then sqlErrFound=1 fi } func_PorcessCtl() { line=`echo $line | tr -d '\136\015\010'` if [ "X$line" != "X" ] then if [ "$1" == "yes" ] then params=`echo $line | awk -F: '{ for (i=2; i<=NF-1; i++) printf "%s ", $i}'` ##parmsNUM=`echo $parmas {'print NF'}` schema=`echo $line | awk -F: {'print $NF'}` if [ "X$params" != "X" ] then func_createSchema for m in $params do tablespaceName=$m func_createTabspa done fi fi if [ "$1" == "no" ] then filePath=$scriptdir/`echo $line | awk -F: {'print $1'}` schema=`echo $line | awk -F: {'print $2'}` if [ ! -f $filePath ] then echo Error: $filePath : no sush file or directory | tee -a $logfile exit 1 fi if [ "X$schema" == "X" ] then func_runSqlfile else func_changeSchema yes func_runSqlfile func_changeSchema fi fi fi } func_createSchema() { totalschema=`expr $totalschema + 1` #drop current schmea echo "Drop schema $schema if exists" $psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -c "drop schema IF EXISTS $schema cascade;" >> $logfile 2>&1 #recreate current schema echo "***** create schema $schema" | tee -a $logfile $psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -c "create schema $schema;" >> $logfile 2>&1 } func_createTabspa() { #change search_path to current schema totalspace=`expr $totalspace + 1` #echo "change $userid's default search_path to $schema" | tee -a $logfile #$psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -c "ALTER ROLE $userid SET search_path to $schema;">>$logfile 2>&1 echo "***** create data directory $datadir/$tablespaceName" | tee -a $logfile sudo mkdir -p $datadir/$tablespaceName echo "***** change data directory ownership to $userid" sudo chown -R $userid:$userid $datadir/$tablespaceName echo "***** drop tablespace if already exists" $psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -c "DROP TABLESPACE IF EXISTS $tablespaceName;" >> $logfile 2>&1 echo "***** create tablespace $tablespaceName" | tee -a $logfile $psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -c "CREATE TABLESPACE $tablespaceName LOCATION '$datadir/$tablespaceName';" >> $logfile 2>&1 if [ $? -eq 0 ]; then echo "---------------------- $tablespaceName created" | tee -a $logfile else echo "---------------------- $tablespaceName create failed" | tee -a $logfile fi } func_changeSchema() { if [ "$1" == "yes" ] then echo "---------------------------------------------" | tee -a $logfile echo "change $userid's default search_path to $schema" | tee -a $logfile $psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -c "ALTER ROLE $userid SET search_path to $schema;" >> $logfile 2>&1 else echo "---------------------------------------------" | tee -a $logfile echo "change default search_path back to public" | tee -a $logfile $psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -c "ALTER ROLE $userid SET search_path to public;" >> $logfile 2>&1 fi } func_runSqlfile() { totalfiles=`expr $totalfiles + 1` echo "=== Executing file $filePath" | tee -a $logfile $psqlCMD -h $hostname -p $port -d $dbname -U $userid -w -a -e -f $filePath >> $logfileTmp 2>&1 errorSqlFile=$? func_CheckError if [ $errorSqlFile -ne 0 ] || [ $sqlErrFound -ne 0 ] then errfiles=`expr $errfiles + 1` echo "Error in $filePath" >> $logfileTmp echo "Error in $filePath. Check details in file - $logfile" fi if [ -f $logfileTmp ] then cat $logfileTmp >> $logfile rm -f $logfileTmp fi } func_createDatadir() { while true do read -p "Speciel the data directory: " datadir if [ -d $datadir ] then if [ `ls $datadir | wc -l` -ne 0 ] then echo "$datadir is already exist, but it is not empty" | tee -a $logfile echo "please select a another directory" else datadir=$datadir break fi else echo "create data directoy $datadir" | tee -a >> $logfile sudo mkdir -p $datadir break fi done } # ======================================== #echo Parsing command line arguments numargs=$# i=1 scriptname=`basename "$0"` scriptdir=`pwd "$0"` psqlCMD=psql createdbCMD=createdb hostname="localhost" initdb="no" createdb="no" help="false" dbname="" userid="" port="" cOntrolfile="" cOntrolcmd="" logdir="" if [ "$USER" == "root" ] then echo "User is "root", running this script must use "postgres"" exit 1 fi while [ $i -le $numargs ] do j=$1 if [ $j = "--dbname" ] || [ $j = "-d" ] then dbname=$2 shift 1 i=`expr $i + 1` fi if [ $j = "--userid" ] || [ $j = "-u" ] then userid=$2 shift 1 i=`expr $i + 1` fi if [ $j = "--ctlfile" ] || [ $j = "-f" ] then userid=$2 shift 1 i=`expr $i + 1` fi if [ $j = "--port" ] || [ $j = "-p" ] then port=$2 shift 1 i=`expr $i + 1` fi if [ $j = "--passwd" ] || [ $j = "-p" ] then port=$2 shift 1 i=`expr $i + 1` fi if [ $j = "--logfile" ] || [ $j = "-l" ] then logdir=$2 shift 1 i=`expr $i + 1` fi if [ $j = "--datadir" ] || [ $j = "-D" ] then datadir=$2 shift 1 i=`expr $i + 1` fi if [ $j = "--init" ] || [ $j = "-i" ] then initdb=yes fi if [ $j = "--create" ] || [ $j = "-c" ] then createdb=yes fi if [ $j = "--help" ] || [ $j = "-h" ] then help=true fi i=`expr $i + 1` shift 1 done if [ $help = "ture" ] then Usage fi if [ "X$dbname" == "X" ] then Usage "ERROR: dbname is empty." fi if [ "X$userid" == "X" ] then userid=postgres fi if [ "X$port" == "X" ] then port=5432 fi if [ "X$logdir" == "X" ] then logdir=/tmp else if [ ! -d $logdir ] then echo create log dirctory $logdir sudo mkdir -p $logdir fi fi logfile=$logdir/${scriptname}_${dbname}_`date +%Y-%m-%d_%H_%M_%S`.log logfileTmp=${logfile}.tmp if [ "X$pgpasswd" == "X" ] then while true do stty -echo read -p "Enter $userid's password: " PGPASSWORD stty echo if [ ! -z $PGPASSWORD ] || [ "X$PGPASSWORD" != "X" ] then export PGPASSWORD=$PGPASSWORD break fi done else export PGPASSWORD=$PGPASSWORD fi if [ "$createdb" == "yes" ] then echo -n "Special the owner of database $dbname, default user is "fscs": " read isFSCS echo "Special the owner of database $dbname, default user is "fscs": $isFSCS " >> $logfile if [ -z $isFSCS ] || [ "$isFSCS" == "X" ] then dbuser=fscs else dbuser=$isFSCS fi createuser -s $dbuser isCreate=$? if [ "$isCreate" -ne "0" ] then echo "create user $dbuser faied" exit 1 else echo User $dbuser created | tee -a $logfile fi $createdbCMD $dbname -O $dbuser isCreate=$? if [ $isCreate -eq 0 ] then echo The owner of the database $dbname is $dbuser | tee -a $logfile echo Database $dbname created | tee -a $logfile echo "------------------------------------------------" | tee -a $logfile echo "You can enter (y/Y) to initialize the $dbname database, enter any key to exit script" echo "Confrim there has initialize information in (*.ctl) control file" echo -n "Do you want to initialize the $dbname[y]: " read initial if [ "$initial" == "y" ] || [ "$initial" == "Y" ] then if [ "X$datadir" == "X" ] then func_createDatadir initdb=yes else datadir=$datadir fi else echo "You can use $0 -d $dbname --init to initialize the database" exit 0 fi else echo create database $dbname faied | tee -a $logfile echo check whether $dbname database is already exist or not? | tee -a $logfile exit 1 fi fi if [ $initdb = "yes" ] then if [ "X$datadir" == "X" ] then func_createDatadir else if [ -d $datadir ] then if [ `ls $datadir | wc -l` -ne 0 ] then echo "$datadir is already exist, and it is not empty" | tee -a $logfile exit 1 fi else echo "create data directoy $datadir" | tee -a >> $logfile sudo mkdir -p $datadir fi fi fi if [ "X$controlfile" == "X" ] then cnt=0 for f in *.ctl do if [ "X$f" != "X" ] && [ "$f" != "*.ctl" ] then cnt=`expr $cnt + 1` fi done if [ $cnt -eq 0 ] then Usage "ERROR: There is no control file (.ctl) in current directory." elif [ $cnt -eq 1 ] then cOntrolfileDir=`pwd` cOntrolfile=$controlfileDir/$f else Usage "ERROR: There are more than one control files (.ctl) in current directory." fi else if [ -f $controlfile ] then cOntrolfileDir=`dirname $controlfile` cOntrolfile=$controlfileDir/`basename $controlfile` fi fi echo log file: $logfile echo FinShare SQL installation starts at `date +%Y-%m-%d.%H:%M:%S` | tee -a $logfile echo Premium Technology Inc. | tee -a $logfile echo Postgres database name: $dbname | tee -a $logfile echo Postgres database User: $userid | tee -a $logfile echo Postgres database port: $port | tee -a $logfile echo SQL Scripts Root Directory: $scriptdir | tee -a $logfile echo Control file full path: $controlfile | tee -a $logfile totalschema=0 totalspace=0 totalfiles=0 errfiles=0 readline=`cat $controlfile | grep -v "^#" | grep -v "^$"` if [ $initdb = "yes" ] then isTablespace=`echo "$readline" | grep -i "^tablespace:"` if [ $? -eq 0 ] then for AllspaceName in $readline do line=$AllspaceName func_PorcessCtl $initdb done else echo No tablesapce defined in $controlfile | tee -a $logfile echo for example: TABLESPACE:tablespaceName1:tablespaceName2:tablespaceName[n]:CDA | tee -a $logfile exit 1 fi initdb=no fi if [ $initdb = "no" ] then sqlname=`cat $controlfile | grep -v "^#" | grep -v "^$" | grep -v -i "^tablespace:"` if [ "X$sqlname" == "X" ] then echo "Error: No SQL file defined in $controfile" | tee -a $logfile exit 1 fi for i in $sqlname do line=$i func_PorcessCtl $initdb done fi echo "finished at `date +%Y-%m-%d.%H:%M:%S`" | tee -a $logfile if [ $totalspace -ne 0 ] then echo $totalspace tablespace have been created | tee -a $logfile fi if [ $totalschema -ne 0 ] then echo $totalschema database user have been created | tee -a $logfile fi echo "$totalfiles files have been executed" | tee -a $logfile echo "$errfiles files with errors" | tee -a $logfile echo "Check log file: $logfile"


本文出自 “一步一步” 博客,谢绝转载!

推荐阅读
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 本文介绍了Linux Shell中括号和整数扩展的使用方法,包括命令组、命令替换、初始化数组以及算术表达式和逻辑判断的相关内容。括号中的命令将会在新开的子shell中顺序执行,括号中的变量不能被脚本余下的部分使用。命令替换可以用于将命令的标准输出作为另一个命令的输入。括号中的运算符和表达式符合C语言运算规则,可以用在整数扩展中进行算术计算和逻辑判断。 ... [详细]
  • 本文介绍了在MacOS系统上安装MySQL的步骤,并详细说明了如何设置MySQL服务的开机启动和如何修改MySQL的密码。通过下载MySQL的macos版本并按照提示一步一步安装,在系统偏好设置中可以找到MySQL的图标进行设置。同时,还介绍了通过终端命令来修改MySQL的密码的具体操作步骤。 ... [详细]
  • 本文总结了Linux下多线程执行shell脚本的4种方法,包括切换到工作目录执行、使用绝对路径执行、直接使用bash或sh执行。同时介绍了为什么需要加上"./"来执行脚本的原因。 ... [详细]
  • 程序员如何选择机械键盘轴体?红轴和茶轴对比
    本文介绍了程序员如何选择机械键盘轴体,特别是红轴和茶轴的对比。同时还介绍了U盘安装Linux镜像的步骤,以及在Linux系统中安装软件的命令行操作。此外,还介绍了nodejs和npm的安装方法,以及在VSCode中安装和配置常用插件的方法。最后,还介绍了如何在GitHub上配置SSH密钥和git的基本配置。 ... [详细]
  • systemd-nspawn可以创建最轻量级的容器(ns的意思就是namespace),本文的实验平台是Ubuntu16.04,x86_64机器。本文的目的是:在Ubuntu中用syst ... [详细]
  • mysql自动打开文件_让docker中的mysql启动时自动执行sql文件
    本文提要本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动My ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 近年来,大数据成为互联网世界的新宠儿,被列入阿里巴巴、谷歌等公司的战略规划中,也在政府报告中频繁提及。据《大数据人才报告》显示,目前全国大数据人才仅46万,未来3-5年将出现高达150万的人才缺口。根据领英报告,数据剖析人才供应指数最低,且跳槽速度最快。中国商业结合会数据剖析专业委员会统计显示,未来中国基础性数据剖析人才缺口将高达1400万。目前BAT企业中,60%以上的招聘职位都是针对大数据人才的。 ... [详细]
  • Shell编程(三)bash数学运算
    本文介绍shell中的expr和bash自带的 ... [详细]
author-avatar
没有你的日子1976_472
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有