pg常用自制shell脚本-tina

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介:
1)小型监控: 
1.在pg库主机上部署,每5分钟执行一次,插入到我的测试pg库内 
[root@mysqltest tina_shell]# cat jk_pg.sh 
#!/bin/bash 
#适用于中转库192.168.12.8和12.2 
running_port=`netstat -nat|grep "LISTEN"|grep "5432"|sed -n 2p|awk -F : '{print $4}'` 
jk_host=`ifconfig |grep "inet addr:192.168"|awk '{print $2}'|awk -F : '{print $2}'` 
record_time=`date +"%Y-%m-%d %H:%M:%S"` 
waiting_count=`ps -ef|grep postgres|grep -v startup |grep waiting|wc -l` 
streaming=`ps -ef|grep wal|grep streaming |awk '{print $15}'` 
#tbjk=`ps -ef|grep postgres|grep startup|grep waiting|wc -l` 
cipan=`df -ah |grep % |grep -v tmpfs|grep -v boot` 
usersum=`ps -ef|grep postgres |grep -E "engine|fenxi|sqluser" |wc -l` 

#echo $jk_host $record_time $waiting_count $streaming $tbjk >>/tmp/pg_check_state.log 
psql -h 192.168.12.31 -U postgres -p 1922 -d tina -c "insert into jk_pg(jk_host,record_time,waiting_count,streaming,running_port,cipan,usersum) values('$jk_host','$record_time','$waiting_count','$streaming','$running_port','$cipan','$usersum');" 

2.部署crontab 
cat /etc/crontab 
0 20 * * * root sh /tina_shell/backup.sh 
4 * * * * root sh /tina_shell/pg_delete_archivelog.sh 
*/5 * * * * root sh /tina_shell/jk_pg.sh 

3.建表 
CREATE TABLE jk_pg 

  id serial NOT NULL, 
  jk_host character varying, -- 监控主机的ip地址 
  record_time timestamp without time zone, -- 监控的时间 
  waiting_count integer, -- 发生waiting等待的进程数ps -ef|grep postgres|grep -v startup |grep waiting|wc -l 
  streaming character varying, -- 正在进行同步的日志ps -ef|grep wal|grep streaming |awk '{print $13}' 
  usersum integer, -- 当前连接用户总数(sqluser、engine、fenxi) 
  tbjk integer, -- ps -ef|grep postgres|grep startup|grep waiting|wc -l 
  running_port integer, -- 检测pg运行是否正常,如果没有显示5432端口,那pg就挂了 
  cipan character varying, -- 磁盘情况 
  locks character varying, -- 锁表情况 
  beizhu character varying -- 填写一些异常的备注 

WITH ( 
  OIDS=FALSE 
); 
COMMENT ON TABLE jk_pg  IS '自制监控表-tina'; 

查看监控数据 
tina=# select * from jk_pg order by record_time desc,jk_host desc limit 4; 
  id  |    jk_host     |     record_time     | waiting_count |  streaming   | usersum | tbjk | running_port |                        cipan                         | locks | beizhu 
------+----------------+---------------------+---------------+--------------+---------+------+--------------+------------------------------------------------------+-------+-------- 
7654 | 192.168.12.2  | 2016-01-13 11:00:01 |             0 | F2B/CE5349B0 |     161 |      |         5432 | Filesystem      Size  Used Avail Use% Mounted on    +|       | 
      |                |                     |               |              |         |      |              | /dev/sda2       104G   21G   78G  22% /             +|       | 
      |                |                     |               |              |         |      |              | /dev/sdc1       917G  540G  331G  63% /opt/db_backup+|       | 
      |                |                     |               |              |         |      |              | /dev/sdb        939G  370G  522G  42% /home/pgsql    |       | 
7655 | 192.168.12.1  | 2016-01-13 11:00:01 |              0 | F2B/CEE173E8 |      26 |    0 |         5432 | Filesystem      Size  Used Avail Use% Mounted on    +|       | 
      |                |                     |               |              |         |      |              | /dev/sda3       103G  6.1G   92G   7% /             +|       | 
      |                |                     |               |              |         |      |              | /dev/sdb1       939G  285G  606G  32% /home/pgsql    |       | 
7653 | 192.168.12.8 | 2016-01-13 11:00:01 |               0 |              |      30 |      |         5432 | Filesystem      Size  Used Avail Use% Mounted on    +|       | 
      |                |                     |               |              |         |      |              | /dev/sda3        27G  1.9G   24G   8% /             +|       | 
      |                |                     |               |              |         |      |              | /dev/sda2        29G  4.1G   24G  15% /var          +|       | 
      |                |                     |               |              |         |      |              | /dev/sdb1       252G  118G  122G  50% /home          |       | 

2)pg统计库所有表的行数 
[root@pg-ro tmp]# cat tinadb.sh 
#!/bin/bash 
#2015-11-3 tina 
date=`date +"%Y-%m-%d %H:%M:%S"` 
echo "begin time is: $date" >>/tmp/tongji.log 

tables=$(psql -U postgres -d tinadb -c "select tablename from pg_tables where  schemaname='public' order by tablename;"|grep -v "tablename" |grep -v "rows"|grep -v "\-") 

#echo $tables >>/tmp/tongji.log 

for table in $tables 
    do 
       echo $table >>/tmp/tongji.log 
       psql -U postgres -d tinadb -c "select count(*) from $table;" |grep -v "count" |grep -v "row"|grep -v "\-">>/tmp/tongji.log
    done 
#echo "ok!" >>/tmp/tongji.log 

查看--并直接粘贴到execl表格中 
[root@pg-ro tmp]# cat /tmp/tongji.log  |awk 'NF==1{printf "%s ", $1;next}1' 
begin time is: 2015-11-03 14:12:12 
t1 11024 
t2 8267537 
t3 1684 
t4 2 

统计其他库,直接用vi替换功能替换db名即可: 
替换 :%s/tinadb/dbname/g 

3)pg 定期vacuum和reindex脚本 
[root@pg tina_shell]# cat pg_tinadb_vacuum.sh 
#!/bin/bash 
#2014-10-22 tina 
date=`date +"%Y-%m-%d %H:%M:%S"` 
echo "begin time is: $date" >>/tmp/pg_tinadb_vacuum.log 

tables=$(psql -U postgres -d tinadb -c "select tablename from pg_tables where schemaname='public';" |grep -v "tablename" |grep -v "rows"|grep -v "\-") 
echo $tables >>/tmp/pg_tinadb_vacuum.log 

indexes=$(psql -U postgres -d tinadb -c "select indexname from pg_indexes where schemaname='public' and indexname not like '%pkey';"|grep -v "indexname"|grep -v "\-" |grep -v "row") 

for table in $tables 
do 
psql -U postgres -d tinadb -c "vacuum full $table;">>/tmp/pg_tinadb_vacuum.log 
echo "table $table has finished vacuum.">>/tmp/pg_tinadb_vacuum.log 
done 

for index in $indexes 
do 
psql -U postgres -d tinadb -c "reindex index $index;">>/tmp/pg_tinadb_vacuum.log 
echo "index $index has finished reindex.">>/tmp/pg_tinadb_vacuum.log 
done 

查看后台日志: 
[root@pg tmp]# tail -f pg_tinadb_vacuum.log 
begin time is: 2016-01-13 11:38:26 
VACUUM 
table t1 has finished vacuum. 
VACUUM 
table t2 has finished vacuum. 
VACUUM 
table t3 has finished vacuum. 
VACUUM 
table t4 has finished vacuum. 
REINDEX 
index t1_rin_idx has finished reindex. 

建议:如果库中存在大表,就单独手动操作,不然可能会导致执行时长时间锁表,影响其他业务。 

4)pg日常备份脚本 
[root@mysqltest tina_shell]# cat backup.sh 
#!/bin/bash 
#本地备份保存目录 
bkdir=/home/bk_pg 
day=`date +"%Y%m%d"` 

#直接指定备份哪些,也可以通过pg_database查询所有非模板和系统db进行自动备份 
DB="tinadb testdb" 
cd $bkdir 
#result=0 

if [ -f $bkdir/pg.md5 ] 
then 
    rm -f $bkdir/pg.md5 
fi 

for db in $DB 
do 
    pg_dump --host localhost --port 5432 --username "postgres" --format custom --blobs --encoding UTF8 --verbose $db --file $bkdir/$db.$day.backup &> $bkdir/bk.log 
    pgret=$? 
    if [ "$pgret" -ne "0" ] 
    then 
        echo "$pgtime $db backup fail" >> $bkdir/pg.md5 
        exit 1 
    else 
        md5sum $bkdir/$db.$day.backup >> $bkdir/pg.md5 
    fi 
done 

#上传ftp,异地保存一份备份 
lftp backup.work <<END 
user username userpasswd 
lcd $bkdir 
cd 12.8_pg 
put tinadb.$day.backup 
put testdb.$day.backup 
put pg.md5 
exit 
END 

#删除两天前的备份 
find $bkdir/ -type f -mtime +2 -exec rm -f {} \; 


5)简易的pg主从同步检测脚本1 
[root@mysqltest tina_shell]# cat pg_check_sync.sh 
#!/bin/bash 
#check pg database whether is running 
pg_port=`netstat -nat|grep "LISTEN"|grep "5432"|sed -n 2p|awk -F : '{print $4}'|awk '{gsub(/ /,"")}1'` 
host_ip=`ifconfig |grep "inet addr:192.168"|awk '{print $2}'|awk -F : '{print $2}'` 
date=`date +"%Y-%m-%d %H:%M:%S"` 

echo $date >>/tmp/pg_check_state.log 
if [ "$pg_port" = "5432" ] 
   then 
       echo "$host_ip postgresql is running" >> /tmp/pg_check_state.log 
   else 
       echo "Warnning -$host_ip postgresql is not running!" >>/tmp/pg_check_state.log 
fi 

#check the role of the host 
pg_role1=`ps -ef |grep wal| awk '{print $10}'|grep "sender"` 
pg_role2=`ps -ef |grep wal| awk '{print $10}'|grep "receiver"` 
pg_slave_ip=`ps -ef|grep wal|grep sender|awk '{print $13}'|awk -F "(" '{print $1}'` 


if [ "$pg_role1" == "sender" -a "$pg_role2" == "" ] 
   then 
     echo "$host_ip is master host and $pg_slave_ip is slave host" >>/tmp/pg_check_state.log 
       else if  [ "$pg_role1" == "" -a "$pg_role2" == "receiver" ] 
       then echo "$host_ip is postgresql slave host.Please execute the shell in the master host!" >>/tmp/pg_check_state.log 
    else 
       echo "check whether the database has slave host" >>/tmp/pg_check_state.log 
   fi 
fi 

#check whether the slave is synchronous 
pg_sync_status=$(su - postgres -c "psql -c 'select state from pg_stat_replication;'|sed -n 3p") 

if [ "$pg_sync_status" = " streaming" ] 
   then echo "the slave is synchronous" >>/tmp/pg_check_state.log 
   else 
   echo "warnning - please check the sync status of slave database " >>/tmp/pg_check_state.log 
fi 

执行结果: 
1.单节点 
[root@mysqltest tina_shell]# cat /tmp/pg_check_state.log 
2016-01-13 15:04:53 
192.168.12.8 postgresql is running 
check whether the database has slave host            ----请检查该pg库是否有从库 

2.主节点 
[root@pg tina_shell]# cat /tmp/pg_check_state.log 
2016-01-13 15:03:31 
192.168.12.2 postgresql is running 
192.168.12.2 is master host and 192.168.12.1 is slave host 
the slave is synchronous                           ----主从同步 

3.从节点 
[root@pg tina_shell]# cat /tmp/pg_check_state.log 
2016-01-13 15:00:44 
192.168.12.1 postgresql is running 
192.168.12.1 is postgresql slave host.Please execute the shell in the master host!   ---此ip上pg是从库,请在主库上执行脚本 

6)简易的pg主从同步检测脚本2 
root@pg /usr/lib64/nagios/plugins]#cat check_pgsync.sh 
#!/bin/bash 
# nrpe command: check pg sql and sync state. 

# customer config 
pgport= 
pgdbname= 
pgdbuser= 

# default value. 
pgport=${pgport:-5432} 
pgdbname=${pgdbname:-postgres} 
pgdbuser=${pgdbuser:-postgres} 

if [ -z "$pgport" ]; then 
    echo "error: pgport no defined" 
    exit 4 
fi 

msg_ok="OK - pg is running and slave is synchronous." 
msg_warn="WARNING - pg is running but slave synchronous fail." 
msg_crit="CRITIAL - pg is not running on port: $pgport" 

# check pg running 
if netstat -ntple | grep -q "[:]$pgport"; then 
    # check slave db host. 
    if ps -ef | grep -q "[w]al receiver process"; then 
        echo "error: it seems you are running me in slave db host." 
    fi 
    # check slave synchronous 
    if psql -d "$pgdbname" -U "$pgdbuser" \ 
        -c 'select state from pg_stat_replication;' \ 
        | grep -q "[s]treaming" 
    then 
        echo "$msg_ok" 
        exit 0 
    else 
        echo "$msg_warn" 
        exit 1 
    fi 
else 
    echo "$msg_crit" 
    exit 2 
fi 


exit 5 


1.单节点 
[root@mysqltest tina_shell]# ./check_pgsync.sh 
WARNING - pg is running but slave synchronous fail. 

2.主节点 
[root@pg tina_shell]# ./check_pgsync.sh 
OK - pg is running and slave is synchronous. 

3.从节点 
[root@pg-ro tina_shell]# ./check_pgsync.sh 
error: it seems you are running me in slave db host. 
WARNING - pg is running but slave synchronous fail. 

7)pg主从切换shell脚本(闲来无事写的,不建议部署生产) 
主库:192.168.10.232 
从库:192.168.10.233 
环境:主从同步,主库突然挂掉 
脚本都部署好之后,只需要在主从执行第一个脚本,就会触发后面脚本的操作,一步到位。 
(部分参数需要提前设置好) 

1、检测主库是否正常启动,如果不是正常启动,就去执行从库的切换脚本 
[postgres@localhost tmp]$ cat pg_check_master.sh 
#!/bin/bash 
#check the master pg whether is running 
pg_port=`netstat -nat|grep "LISTEN"|grep "5432"|sed -n 2p|awk -F : '{print $4}'|awk '{gsub(/ /,"")}1'` 
host_ip=`ifconfig |grep "inet addr:192.168"|awk '{print $2}'|awk -F : '{print $2}'` 
date=`date +"%Y-%m-%d %H:%M:%S"` 
echo $date >>/tmp/pg_check_master.log 

if [ "$pg_port" = "5432" ] 
   then 
       echo "$host_ip postgresql is running" >> /tmp/pg_check_master.log 
    
   else 
       echo "Warnning -$host_ip postgresql is not running!" >>/tmp/pg_check_master.log 
       echo "the slave is switching to the master ...please waiting" >>/tmp/pg_check_master.log 
       ssh 192.168.10.233 "sh /tmp/pg_switch.sh" 
fi   

2、创建从库的触发文件,将从库启动成主库(触发文件,主库和从库的名字最好不要设置成一样的,以免不好区分) 
[postgres@localhost tmp]$ cat pg_switch.sh 
#!/bin/bash 
#swtch slave to master 
date=`date +"%Y-%m-%d %H:%M:%S"` 
echo $date >>/tmp/pg_switch.log 
cd /pg/data 
rm -fr recovery.done 
touch /tmp/pg.trigger.456 
sleep 20s 
if [ -f '/pg/data/recovery.done' ] 
      then echo "the slave has switched to the master successful!" >> /tmp/pg_switch.log 
      echo "the old master is going to switch to the new slave!">>/tmp/pg_switch.log 
      his_file=`ls -lt /pg/data/pg_xlog/0000000*.history |sed -n 1p|awk '{print $9}'` 
      scp $his_file root@192.168.10.232:/pg/data/pg_xlog 
      ssh 192.168.10.232  "sh /tmp/start_new_slave.sh"     
  else 
      echo "warnning:the slave has switched fail!">>/tmp/pg_switch.log 
fi 


3、注意recovery.conf会随着主从的变化而消失,因此我们可以先将内容写好的文件备份到上一级目录 
内容包含如下: 
vi /pg/recovery.conf.bak 
recovery_target_timeline = 'latest' 
standby_mode = 'on'  
primary_conninfo = 'host=192.168.10.233 port=5432 user=postgres password=tina' 
trigger_file = '/tmp/pg.trigger.456'  


4、有了时间线文件、有了recovery.conf,检查一下pg_hba.conf,就可以直接启动pg新从库了,并做一个主从同步的检查。 
[root@localhost tmp]# cat start_new_slave.sh 
#!/bin/bash 
date=`date +"%Y-%m-%d %H:%M:%S"` 
echo $date >>/tmp/start_new_slave.log 
chown postgres.postgres /pg/data/pg_xlog/*.history 

cp /pg/recovery.conf.bak /pg/data/recovery.conf 
chown postgres.postgres recovery.conf 
su - postgres -c "pg_ctl -D /pg/data start" >>/tmp/start_new_slave.log 2&>1 
pg_slave_status=`ps -ef |grep wal| awk '{print $10}'|grep "receiver"` 
if [ "$pg_slave_status" = "receiver" ] 
   then 
     echo "the slave sync is ok!" >>/tmp/start_new_slave.log 
   else 
       echo "error:please check the slave whether is running or not!" >>/tmp/start_new_slave.log 
fi 

8)pg删除归档日志 
[root@pg tina_shell]# cat pg_delete_archivedlog.sh 
#!/bin/bash 
find /home/pgsql/backup_new/archived_log/  -type f  -mtime +2 -exec rm {} \; 

9)常用拼接sql 
select 'select count(*) from '||tablename||';' from pg_tables where schemaname='public'; 
select 'alter table '||tablename||' add constraint u_'||tablename||' unique(sample_h);' from pg_tables where tablename like 't_wh20%'; 
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
5月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
525 9
|
5月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
510 2
|
8月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
316 60
|
11月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
5月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
7月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
875 0
|
9月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
286 17
|
Shell
一个用于添加/删除定时任务的shell脚本
一个用于添加/删除定时任务的shell脚本
348 1
|
10月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
975 25
|
9月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
265 4