Home Database Mysql Tutorial 用shell写的postgre数据库初始数据脚本

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

Jun 07, 2016 pm 05:39 PM
shell

公司最近在使用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> $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"


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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install Classic Shell on Windows 11? How to install Classic Shell on Windows 11? Apr 21, 2023 pm 09:13 PM

<p>Customizing your operating system is a great way to make your daily life more enjoyable. You can change the user interface, apply custom themes, add widgets, and more. So today we will show you how to install ClassicShell on Windows 11. </p><p>This program has been around for a long time and allows you to modify the operating system. Volunteers have now started running the organization, which disbanded in 2017. The new project is called OpenShell and is currently available on Github for those interested. </p>&a

Explorer.exe does not start on system startup [Fix] Explorer.exe does not start on system startup [Fix] Jun 03, 2023 am 08:31 AM

Nowadays, many Windows users start encountering severe Windows system problems. The problem is that Explorer.exe cannot start after the system is loaded, and users cannot open files or folders. Although, Windows users can open Windows Explorer manually using Command Prompt in some cases and this must be done every time the system restarts or after system startup. This can be problematic and is due to the following factors mentioned below. Corrupted system files. Enable fast startup settings. Outdated or problematic display drivers. Changes were made to some services in the system. Modified registry file. Keeping all the above factors in mind, we have come up with some that will surely help the users

PowerShell deployment fails with HRESULT 0x80073D02 issue fixed PowerShell deployment fails with HRESULT 0x80073D02 issue fixed May 10, 2023 am 11:02 AM

Do you see this error message "Add-AppxPackage: Deployment failed with HRESULT: 0x80073D02, The package cannot be installed because the resource it modifies is currently in use. Error 0x80073D02..." in PowerShell when you run the script? As the error message states, this does occur when the user attempts to re-register one or all WindowsShellExperienceHost applications while the previous process is running. We've got some simple solutions to fix this problem quickly. Fix 1 – Terminate the experience host process You must terminate before executing the powershell command

How to quickly delete the line at the end of a file in Linux How to quickly delete the line at the end of a file in Linux Mar 01, 2024 pm 09:36 PM

When processing files under Linux systems, it is sometimes necessary to delete lines at the end of the file. This operation is very common in practical applications and can be achieved through some simple commands. This article will introduce the steps to quickly delete the line at the end of the file in Linux system, and provide specific code examples. Step 1: Check the last line of the file. Before performing the deletion operation, you first need to confirm which line is the last line of the file. You can use the tail command to view the last line of the file. The specific command is as follows: tail-n1filena

Different ways to run shell script files on Windows Different ways to run shell script files on Windows Apr 13, 2023 am 11:58 AM

Windows Subsystem for Linux The first option is to use Windows Subsystem for Linux or WSL, which is a compatibility layer for running Linux binary executables natively on Windows systems. It works for most scenarios and allows you to run shell scripts in Windows 11/10. WSL is not automatically available, so you must enable it through your Windows device's developer settings. You can do this by going to Settings > Update & Security > For Developers. Switch to developer mode and confirm the prompt by selecting Yes. Next, look for W

Super hardcore! 11 very practical Python and Shell script examples! Super hardcore! 11 very practical Python and Shell script examples! Apr 12, 2023 pm 01:52 PM

Some examples of Python scripts: enterprise WeChat alarms, FTP clients, SSH clients, Saltstack clients, vCenter clients, obtaining domain name SSL certificate expiration time, sending today's weather forecast and future weather trend charts; some examples of Shell scripts: SVN Full backup, Zabbix monitoring user password expiration, building local YUM, and the readers' needs in the previous article (when the load is high, find out the process scripts with high occupancy and store or push notifications); it is a bit long, so please read it patiently At the end of the article, there is an Easter egg after all. Python script part of enterprise WeChat alarm This script uses enterprise WeChat application to perform WeChat alarm and can be used

Here are the fixes for Open Shell Windows 11 not working issue Here are the fixes for Open Shell Windows 11 not working issue Apr 14, 2023 pm 02:07 PM

Open shell not running on Windows 11 is not a new problem and has been plaguing users since the advent of this new operating system. The cause of the Open-Shell Windows 11 not working issue is not specific. It can be caused by unexpected errors in programs, the presence of viruses or malware, or corrupted system files. For those who don’t know, Open-Shell is the replacement for Classic Shell, which was discontinued in 2017. You can check out our tutorial on how to install Classic Shell on Windows 11. How to replace Windows 11 Start menu

How to install Open Shell to restore the classic Start menu on Windows 11 How to install Open Shell to restore the classic Start menu on Windows 11 Apr 18, 2023 pm 10:10 PM

OpenShell is a free software utility that can be used to customize the Windows 11 Start menu to resemble a classic-style menu or a Windows 7-style menu. The Start menu on previous versions of Windows provided users with an easy way to browse the contents of their system. Basically, OpenShell is a replacement for ClassicShell that provides different user interface elements that help to get the functionality of the latter version from previous Windows versions. Once development of ClassicShell ceased in 2017, it was maintained and developed by GitHub volunteers under the name OpenShell. It is related to Win

See all articles