postgreSQL进程检测与数据库备份脚本

脚本功能:

系统环境:SUSE Linux Server 11.3 X64

  1. 数据库进程检测重启。

  2. 数据库备份压缩脚本。



#### Check DB Process Script ####


#!/bin/sh

YEAR=`date +%Y`

MON=`date +%m`

DAY=`date +%d`

HOUR=`date +%H`

MIN=`date +%M`

echo "$YEAR-$MON-$DAY $HOUR:$MIN" > /opt/pgdb/cron/dbstatus.log

servName="postmaster"

DBSTAT=`ps -ef|grep $servName|grep -v 'grep'`

if [ "$DBSTAT" == "" ];

then

    echo "testdb DB is down" >> /opt/pgdb/cron/dbstatus.log

    echo "testdb database is down"|mailx -s "testdb DB down" [email protected]

    DBstus=`ps -ef|grep $servName|grep -v 'grep'`

    while [ "$DBstus" == "" ];

     do

      if ["$DBstus" == ""];

      then

        echo "DB starting ..." >> /opt/pgdb/cron/dbstatus.log

        /etc/init.d/postgresql start

        DBstus=`ps -ef|grep $servName|grep -v 'grep'`

      else

        echo "testdb DB already running."

      fi

    done

else

    echo "testdb DB is running." >> /opt/pgdb/cron/dbstatus.log

fi





#### Backup DB Script ####


#!/bin/bash

export PATH=$PATH:/usr/local/pgsql/bin

DATE=`date +%Y_%m_%d_%H`

YESDAY=`date -d "yesterday" +%Y_%m_%d_%H`

TWODAGO=`date -d "3 day ago" +%Y_%m_%d_%H`


del_file_3ago() {

     #### delete 3 day ago backup file ####

    PGHOME="/home/postgres"

    cd $PGHOME

    echo "Dir list before delete 3 day ago backup file: "

    ls -lrt

    TDAFILE="testdb_"$TWODAGO".bak.tar.gz"

    echo "Detele 3 Day ago file: "$TDAFILE

    rm "$TDAFILE"

    echo "Dir list after delete 3 day ago backup file: "

    ls -lrt

}


process_yesterday_bak() {

       #### Compress and remove yesterday bak file####

       PGHOME="/home/postgres"

       cd $PGHOME

       COMFILE="testdb_"$YESDAY".bak.tar.gz"

       YDFILE="testdb_"$YESDAY".bak"

       echo "tar zcvf $COMFILE $YDFILE"|sh

       echo "General yesterday tar file: "$COMFILE

       rm "$YDFILE"

       echo "delete yesterday file: "$YDFILE

       echo "Dir list After tar yesterday backup file: "

       ls -lrt

}


#### Backup testdb database ####


echo $DATE

echo "Starting backup database: testdb ...."

echo "">/home/postgres/pgbk_fail.txt

su - postgres -c "pg_dump testdb > /home/postgres/testdb_$DATE.bak 2>/home/postgres/pgbk_fail.txt"

grep failed /home/postgres/pgbk_fail.txt

if [ $? -eq 0 ]

then

    echo "Database testdb backup Failed!"

else

    echo "Database testdb backup Successful!"

    del_file_3ago

    process_yesterday_bak

fi

echo "Dir list After backup database testdb: "

ls -lrt





你可能感兴趣的:(shell,PostgreSQL)