1 概述
最近在部署項目,需要寫自動安裝部署腳本,對shell腳本並不是熟悉,這裏記錄下一些常用的命名,主要是對於配置文件的修改.
注意: 對於一些可變參數我們可以單獨寫在一個文件中,達到可配置的效果.
(將配置文件信息和初始化腳本放在同一個目錄下)
2 shell常用命令
2.1讀取當前文件目錄
path=$(cd `dirname $0`;pwd)
2.2 sed命令的使用
使用sed命令修改配置文件中的參數
- 配置文件
application.properties
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/tag
spring.datasource.username=postgres
spring.datasource.password=1234
- 修改pg參數
# pg properties
sed -i "s/spring\.datasource\.username=.*/spring\.datasource\.username=${pg_user}/" ${path}/application.properties
sed -i "s/spring\.datasource\.password=.*/spring\.datasource\.password=${pg_pw}/" ${path}/application.properties
#sed -i "s!spring|.datasource|.url=.*|spring|.datasource|.url=${pg_url}!" ${path}/application.properties
sed -i "s/spring\.datasource\.url=.*/spring\.datasource\.url=jdbc:postgresql:\/\/${pg_host}:${pg_port}\/${pg_db}/" ${path}/application.properties
2.3 修改hosts文件
修改host文件,需要把host和主機名加到/etc/hosts
目錄下,我們需要準備參數達到可配置
- 準配參數
tag-server.config
hostname_1=hadoop001
ip_1=10.192.77.1
hostname_2=hadoop002
ip_2=10.192.77.2
hostname_3=hadoop003
ip_3=10.192.77.3
hostname_1=`sed '/^hostname_1=/!d;s/.*=//' $path/tag-server.config`
ip_1=`sed '/^ip_1=/!d;s/.*=//' $path/tag-server.config`
hostname_2=`sed '/^hostname_2=/!d;s/.*=//' $path/tag-server.config`
ip_2=`sed '/^ip_2=/!d;s/.*=//' $path/tag-server.config`
hostname_3=`sed '/^hostname_3=/!d;s/.*=//' $path/tag-server.config`
ip_3=`sed '/^ip_3=/!d;s/.*=//' $path/tag-server.config`
if grep -Fxq "${ip_1} ${hostname_1}" /etc/hosts
then
echo 'Already configured'
else
echo "${ip_1} ${hostname_1}" >> /etc/hosts
fi
if grep -Fxq "${ip_2} ${hostname_2}" /etc/hosts
then
echo 'Already configured'
else
echo "${ip_2} ${hostname_2}" >> /etc/hosts
fi
if grep -Fxq "${ip_3} ${hostname_3}" /etc/hosts
then
echo 'Already configured'
else
echo "${ip_3} ${hostname_3}" >> /etc/hosts
fi
2.4 ssh命令
經常需要遠程到其他節點上執行一些shell命令,這時候就要用到ssh命令,舉個例子,加入我現在在A機器上進行項目部署,但是需要使用到B機器上部署的大數據環境,那我就要在A機器上執行一些命令,例如創建Kafka Topic,Hbase建表等,這時候就要用到SSH命令.有些人該有個疑問了,那爲什麼不直接在B機器上創建好呢?
- 因爲你是在A機器上進行項目部署,肯要要在啓動A機器上啓動,既然是一鍵部署,那你還要到B機器去執行命令就不能叫一鍵部署了吧
[root@hadoop /opt/tag/tag-0826]#ssh --help
unknown option -- -
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
[-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
[-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
[-i identity_file] [-J [user@]host[:port]] [-L address]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-Q query_option] [-R address] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] destination [command]
- 參數說明
-l 指定登入用戶
-p 設置端口號
-f 後臺運行,並推薦加上 -n 參數
-n 將標準輸入重定向到 /dev/null,防止讀取標準輸入。如果在後臺運行ssh的話(-f選項),就需要這個選項。
-N 不執行遠程命令,只做端口轉發
-q 安靜模式,忽略一切對話和錯誤提示
-T 禁用僞終端配置
-t (tty)爲遠程系統上的ssh進程分配一個僞tty(終端)。如果沒有使用這個選項,當你在遠程系統上運行某條命令的時候,
ssh不會爲該進程分配tty(終端)。相反,ssh將會把遠端進程的標準輸入和標準輸出附加到ssh會話上去,
這通常就是你所希望的(但並非總是如此)。
這個選項將強制ssh在遠端系統上分配tty,這樣那些需要tty的程序就能夠正常運行。
-v verbose)顯示與連接和傳送有關的調試信息。如果命令運行不太正常的話,這個選項就會非常有用
- 配置文件
tag-server.config
### ssh 端口
ssh_port=22
#### 當前服務器家目錄
current_contents=/root/
##### cluster message(大數據服務器)
cluster_ip=10.192.130.2
cluster_name=hadoop
ip_port=10.192.130.2:2181,10.192.130.3:2181,10.192.130.4:2181
#replication_factor
replication_factor=3
#partitions
partitions=10
topic=test
###### postgresql message
pg_host=10.192.137.20
pg_port=5432
pg_db=test
pg_user=postgres
pg_pw=1234
# 需要放在ssh到的那臺機器
pgsql_path=/home/install/pg.sql
##### elasticsearch message
#es.cluster
es_cluster=es-dev-6.5.4
#es es.host
es_host=10.192.137.2
#es port
es_port=9300
#es.restfulPort
es_restfulPort=9200
#es.search.max.day=30
es_search_max_day=30
- 創建Kafka Topic
topic=`sed '/^topic=/!d;s/.*=//' $path/tag-server.config`
#集羣信息
ip_port=`sed '/^ip_port=/!d;s/.*=//' $path/tag-server.config`
#replication_factor
replication_factor=`sed '/^replication_factor=/!d;s/.*=//' $path/tag-server.config`
#partitions
partitions=`sed '/^partitions=/!d;s/.*=//' $path/tag-server.config`
ssh ${cluster_name}@${cluster_ip} -p ${ssh_port} kafka-topics --create --zookeeper ${ip_port} --replication-factor ${replication_factor} --partitions --topic ${topic}
- 執行pg腳本
pg_host=`sed '/^pg_host=/!d;s/.*=//' $path/tag-server.config`
pg_port=`sed '/^pg_port=/!d;s/.*=//' $path/tag-server.config`
pg_db=`sed '/^pg_db=/!d;s/.*=//' $path/tag-server.config`
pg_user=`sed '/^pg_user=/!d;s/.*=//' $path/tag-server.config`
pg_pw=`sed '/^pg_pw=/!d;s/.*=//' $path/tag-server.config`
pg_url=jdbc:postgresql:\/\/${pg_host}:${pg_port}/${pg_db}
pgsql_path=`sed '/^pgsql_path=/!d;s/.*=//' $path/tag-server.config`
ssh ${cluster_name}@${cluster_ip} -p ${ssh_port} "psql 'host=${pg_host} port=${pg_port} user=${pg_user} password=${pg_pw}' -c 'CREATE DATABASE '${pg_db}''"
ssh ${cluster_name}@${cluster_ip} -p ${ssh_port} "psql 'host=${pg_host} port=${pg_port} user=${pg_user} password=${pg_pw} dbname=${pg_db}' -f '${pgsql_path}'"
- es配置信息
es_host=`sed '/^es_host=/!d;s/.*=//' $path/tag-server.config`
es_port=`sed '/^es_port=/!d;s/.*=//' $path/tag-server.config`
es_cluster=`sed '/^es_cluster=/!d;s/.*=//' $path/tag-server.config`
es_restfulPort=`sed '/^es_restfulPort=/!d;s/.*=//' $path/tag-server.config`
#es配置信息,數據主題使用
#es.cluster=es-dev-6.5.4
sed -i "s/es\.cluster=.*/es\.cluster=${es_cluster}/" ${path}/application.properties
#es.host=10.192.77.223
sed -i "s/es\.host=.*/es\.host=${es_host}/" ${path}/application.properties
#es.port=39300
sed -i "s/es\.port=.*/es\.port=${es_port}/" ${path}/application.properties
#es restful請求端口,主題發佈創建索引使用
#es.restfulPort=39200
sed -i "s/es\.restfulPort=.*/es\.restfulPort=${es_restfulPort}/" ${path}/application.properties
scp
scp --help
unknown option -- -
usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program] source ... target
參數如下:
-1 強制scp命令使用協議ssh1
-2 強制scp命令使用協議ssh2
-4 強制scp命令只使用IPv4尋址
-6 強制scp命令只使用IPv6尋址
-B 使用批處理模式(傳輸過程中不詢問傳輸口令或短語)
-C 允許壓縮。(將-C標誌傳遞給ssh,從而打開壓縮功能)
-p 保留原文件的修改時間,訪問時間和訪問權限。
-q 不顯示傳輸進度條。
-r 遞歸複製整個目錄。
-v 詳細方式顯示輸出。
-c cipher 以cipher將數據傳輸進行加密,這個選項將直接傳遞給ssh。
-F ssh_config 指定一個替代的ssh配置文件,此參數直接傳遞給ssh。
-i identity_file 從指定文件中讀取傳輸時使用的密鑰文件,此參數直接傳遞給ssh。
-l limit 限定用戶所能使用的帶寬,以Kbit/s爲單位。
-o ssh_option 如果習慣於使用ssh_config(5)中的參數傳遞方式,
-P port 注意是大寫的P, port是指定數據傳輸用到的端口號
-S program 指定加密傳輸時所使用的程序。此程序必須能夠理解ssh(1)的選項。
- 配置文件
#### ssh 端口
ssh_port=55555
#### 當前服務器家目錄
current_contents=/root/
##### cluster message(大數據服務器)
cluster_ip=10.192.77.223
cluster_name=hik
# 大數據服務器用戶家目錄
cluster_contents=/home/hadoop/
- 免密要登錄
#獲取ssh port
ssh_port=`sed '/^ssh_port=/!d;s/.*=//' $path/tag-server.config`
#獲取服務器用戶名和密碼
#cluster1_ip=10.192.77.223
#cluster_name=hik
cluster_name=`sed '/^cluster_name=/!d;s/.*=//' $path/tag-server.config`
cluster_ip=`sed '/^cluster_ip=/!d;s/.*=//' $path/tag-server.config`
# 免密碼登錄腳本,配置ssh信任
# 服務器用戶家目錄
cluster_contents=`sed '/^cluster_contents=/!d;s/.*=//' $path/tag-server.config`
#### 當前服務器家目錄
current_contents=`sed '/^current_contents=/!d;s/.*=//' $path/tag-server.config`
ssh-keygen -t rsa
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keys
scp -P ${ssh_port} ${current_contents}.ssh/id_rsa.pub ${cluster_name}@${cluster_ip}:${cluster_contents}.ssh/authorized_keys