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.
#直接指定备份哪些,也可以通过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
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是从库,请在主库上执行脚本
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.
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
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%';