vacuum(free)

vim pg-vacuum-freeze.sh

#!/bin/bash

#设置自动定期对 事务ID执行 回环
#zhaowz 2019-04-02

 

date=`date +"%Y%m%d%H%M"`
v_hour=0
v_param=$#

if [ $v_param -ge 3 ];then
        echo "Invalid argument!! E.g sh pg-vacuum-freeze.sh vlnx191001.xxx.cn OR sh pg-vacuum-freeze.sh vlnx191001.xxx.cn  \" db1,db2,db3,...\""
        exit 1
fi

pghosts=$1

if [ -n "$pghosts" ];then

        hostname=${pghosts%%.*}
        filename=$hostname'-vacuum-freeze-'$date

        for pghostname in $pghosts
        do
                if [ $v_param -eq 1 ];then
                        dblist=$(psql  -h $pghostname -U zhaowz -d postgres -c "select datname from pg_database where datname not in ('template0','template1')" -A -t)
                elif [ $v_param -eq 2 ];then
                        dblist=$(psql  -h $pghostname -U zhaowz -d postgres -c "select datname from pg_database where datname=any('{$2}')" -A -t)
                fi

                if [ -z "$dblist" ];then
                        echo "Invalid dbname OR Invalid hostname !!!" >> /home/zhaowz/logs/$filename.log
                        exit 1
                fi


                        for db in $dblist
                        do
                                v_hour=$(date "+%k")
                                if [[ ${v_hour} -ge 1 && ${v_hour} -lt 10 ]]; then

                                        echo "$db : `date +"%Y%m%d%H%M"` Begin">>/home/zhaowz/logs/$filename.log
                                        tables=$(psql  -h $pghostname -U zhaowz -d $db -c "select tablename from pg_tables where schemaname='public'" -A -t)

                                        for table in $tables
                                        do
                                                echo "$table :`date +"%Y%m%d%H%M"` Begin vacuum-freeze ">>/home/zhaowz/logs/$filename.log
                                                psql  -h $pghostname -U zhaowz -d $db -c "Vacuum freeze $table"
                                                echo "$table :`date +"%Y%m%d%H%M"` $table vacuum-freeze has finished   ">>/home/zhaowz/logs/$filename.log
                                        done
                                fi
                        done
        done
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值