> 文章列表 > shell 常用工具指令

shell 常用工具指令

shell 常用工具指令

常用组件/服务安装

背景: 不同语言组件开发需求,需要经常快速切换开发环境,或者在新机器安装开发环境。于是将常用开发语言的安装 固化成脚本,方便后续使用

项目: shell-tools

举例: 安装 trino 较高版本,需要 jdk17,通过这个项目 直接执行 make java-new 即可

安装完成后,环境变量中自动就配置了 jdk17

# java
export JAVA_HOME=/usr/java/jdk-17.0.6+10 # java

hive 表迁移

参考-hive数据迁移

背景: 需要跨集群迁移 hive 表结构,涉及两步: 迁移表数据 和 迁移表结构

表数据迁移

如果两个集群环境相通,可直接迁移 hdfs 数据

su ods -c "hadoop distcp hdfs://source_cluster/apps/hive/warehouse/db_name.db hdfs://target_cluster/apps/hive/warehouse/db_name/db"

如果网络不互通,需要先把数据下载下来,再手动传到目标集群。如果数据量不大还是可行的
su hdfs -c “hdfs dfs -get hdfs://cluster_name/apps/hive/warehouse/db_name.db/table_name”

for current_table in tablearr[@]dosuhdfs−c"hdfsdfs−gethdfs://clustername/apps/hive/warehouse/dbname.db/{table_arr[@]} do su hdfs -c "hdfs dfs -get hdfs://cluster_name/apps/hive/warehouse/db_name.db/tablearr[@]dosuhdfsc"hdfsdfsgethdfs://clustername/apps/hive/warehouse/dbname.db/{current_table}"
done

表结构迁移

table_arr=(需要导出的表列表)echo "" >> /tmp/ddl.sql
for current_table in ${table_arr[@]}
dobeeline -u jdbc:hive2://hive_server_host:10000/dw -n hive -e "show create table ${current_table}" 2>/dev/null | sed 's/.*createtab_stmt.*//g' | sed 's/.*--.*//g' | sed 's/| //g' | sed -E 's/ +\\|//g' >> /tmp/ddl.sqlecho ";" >> /tmp/ddl.sql
done

然后在目标环境 执行导出的 ddl.sql

beeline -n user -u jdbc:hive2://hive_server_host:10000/db_name -f /tmp/ddl.txt

frp 反向代理批量安装

公司内网禁用了 服务部署环境 ssh 端口和本地开发环境的连接,登录机器需要通过 jumpserver 非常不方便

参考 frp 项目,写了一个批量在多节点安装 frpc 的脚本

# frps 安装
echo "sshd:127.0.0.1" >> /etc/hosts.allowmkdir -p /opt/modules/frps
pushd /opt/modules/frps
curl -LO https://github.com/fatedier/frp/releases/download/v0.40.0/frp_0.40.0_linux_amd64.tar.gz
tar -xzvf frp_0.40.0_linux_amd64.tar.gz
cd frp_0.40.0_linux_amd64
echo """
[common]
bind_port = 7001
authenticate_new_work_conns = true
authenticate_heartbeats = true
authentication_method = token
token = frp认证token
""" > frps.ini
nohup ./frps -c ./frps.ini > /dev/null 2>&1 &
popd
# frpc 批量安装ip_list=(需要安装的节点列表)
remote_port=6001
ssh_port=22for current_ip in ${ip_list[@]}
dossh -p${ssh_port} ${current_ip} "mkdir -p /opt/modules/frpc && cd /opt/modules/frpc && curl -LO https://github.com/fatedier/frp/releases/download/v0.40.0/frp_0.40.0_linux_amd64.tar.gz && tar -xzvf frp_0.40.0_linux_amd64.tar.gz && cd frp_0.40.0_linux_amd64 && hostname=`hostname | sed 's/\\..*//g'` && echo -e "\\n[common]\\nserver_addr = router1\\nserver_port = 7001\\nauthenticate_new_work_conns = true\\nauthenticate_heartbeats = true\\nauthentication_method = token\\ntoken = frp认证token\\n[ssh_${remote_port}]\\ntype = tcp\\nlocal_port = ${ssh_port}\\nremote_port = ${remote_port}" | tee frpc.ini"ssh -p${ssh_port} ${current_ip} "ps -ef | grep 'frpc' | grep -v grep | awk '{print \\$2}' | xargs --no-run-if-empty kill -9"ssh -p${ssh_port} ${current_ip} "nohup /opt/modules/frpc/frp_0.40.0_linux_amd64/frpc -c /opt/modules/frpc/frp_0.40.0_linux_amd64/frpc.ini > /dev/null 2>&1 &"ssh -p$ ${current_ip} 'echo "sshd:127.0.0." | tee -a /etc/hosts.allow'remote_port=$((remote_port+1))
done