本文提供数据库安装脚本,有部分需要优化,就是脚本中的方法执行存在前后依赖,但是代码里面没有对上一个执行结果进行判断,如果提供的路径和安装包没有问题,脚本能够正常执行
1 #!/bin/bash
2 # install postgre
3 # base_path=$(cd $(dirname ${BASH_SOURCE[0]}); pwd )
4 base_path="/opt"
5 # Pg database installation package name
6 pg_name="./postgresql-9.6.15-1-linux-x64-binaries.tar.gz"
7 # Pg database installation path
8 pg_deploy_path="$base_path/postgreSQL"
9 # Directory of data stored in the pg database
10 pg_data_path="${pg_deploy_path}/data"
11 # Directory of data stored in the pg database
12 user_name='pguser'
13
14
15 # unzip pg
16 function pg_untar(){
17 if [ -d ${pg_deploy_path} ];then
18 echo "pg has been installed"
19 else
20 if [ ! -e ${pg_name} ];then
21 echo "Missing pg installation package"
22 else
23 mkdir -p ${pg_deploy_path}
24 tar -zxf ${pg_name} -C ${pg_deploy_path}
25 fi
26 fi
27 }
28
29
30
31 # Create a Linux user to log in to the PG database
32 function pg_create_user(){
33 # user_exist=`cat /etc/passwd | grep '^${user_name}:' -c`
34 egrep "^${user_name}" /etc/passwd >/dev/null
35 if [ $? -eq 1 ]; then
36 pass=$(perl -e 'print crypt($ARGV[0], "password")' $user_name)
37 useradd -m -p $pass ${user_name}
38 #useradd -m -p 'postgres' ${user_name}
39 [ $? -eq 0 ] && echo "user [${user_name}] has been added to system!" || echo "Failed to add user [${user_name}]"
40 else
41 echo "user [${user_name}] exists"
42 fi
43 }
44
45 #init pg
46 function pg_init(){
47 #Create a data directory
48 mkdir -p ${pg_data_path}
49 touch ${pg_deploy_path}/logfile
50 #Grant user access
51 chown -R ${user_name} ${pg_data_path}
52 chown ${user_name} ${pg_deploy_path}/logfile
53
54 # init database
55 su - ${user_name} -c "${pg_deploy_path}/pgsql/bin/initdb -D ${pg_data_path}"
56 }
57
58 #Modify config of the pg database
59 #
60 function pg_modify_config(){
61 id_path=${pg_data_path}/postgresql.conf
62 local_path=${pg_data_path}/pg_hba.conf
63 localip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:")
64 if [ -d ${pg_data_path} ];then
65 chown -R 'root' ${pg_data_path}
66 #Modify listening ip
67 sed -i "s@#listen_addresses = 'localhost'@listen_addresses = \'${localip}\'@g" $id_path
68 #Modify Trust client ip
69 #sed -i "s@#listen_addresses = 'localhost'@listen_addresses = \'${localip}\'@g" $id_path
70 sed '86 ahost all all 0.0.0.0/0 trust' -i $local_path
71 chown -R ${user_name} ${pg_data_path}
72 else
73 echo "You need to initialize the database with the command:\n\t ${pg_deploy_path}/pgsql/bin/initdb -D ${pg_data_path} "
74 fi
75 }
76
77 # start pg database
78 function pg_start(){
79 su - ${user_name} -c "${pg_deploy_path}/pgsql/bin/pg_ctl -D ${pg_data_path} -l ${pg_deploy_path}/logfile start"
80 }
81 # create default database bms_ops
82 function pg_createdb(){
83 sleep 3s
84 default_db="bms_ops"
85 su - ${user_name} -c "${pg_deploy_path}/pgsql/bin/createdb -O ${user_name} ${default_db}"
86 }
87 #Open 5432 port
88 function pg_open_port(){
89 isopen=$(firewall-cmd --query-port=5432/tcp)
90 if [ 'no' == $isopen ];then
91 firewall-cmd --add-port=5432/tcp --permanent>/dev/null
92 firewall-cmd --reload>/dev/null
93 else
94 echo "port 5432 already opened"
95 fi
96
97 }
98
99 echo "============== start install pg =============="
100 if [ -d ${pg_deploy_path} ];then
101 echo "pg has been installed"
102 else
103 pg_untar
104 pg_create_user
105 pg_init
106 pg_modify_config
107 pg_start
108 pg_createdb
109 pg_open_port
110 fi
111 echo "============== finish installing pg =============="