维护CentOS6.2下的一些常用脚本之二

服务器监控的一些代码,备份,学习,待优化用#脚本


1,监控数据库的Processlist,超过5条不为空的记录则打印输出,CTRL+C退出

#/bin/sh
# usage:
# bash filename 

export MYSQLBIN=/usr/local/mysql/bin
export MYSQLHOST='localhost'
export MYSQLPORT=3336
export MYSQLUSER=root
export MYSQLPSWD='********'
export TMPFILE=/tmp/mysqlstatus.dump.txt
export ty=20

echo ■■■■■■ ${MYSQLBIN}/mysql  》FOR Show Processlist ※※※※※※※※※※※※※※※※※※※※

while [ "1" -eq "1" ]
do

        export CURRENTTIME=`date +%T`

        echo START------------ $CURRENTTIME >$TMPFILE

        $MYSQLBIN/mysql -u$MYSQLUSER -p$MYSQLPSWD \
            --host $MYSQLHOST -P$MYSQLPORT \
            -e "show processlist" >> $TMPFILE

         sed -i -e '/NULL/d' $TMPFILE

        export LINES=`wc -l $TMPFILE | awk '{print $1}'`

        LINES=$[LINES -2]
        echo END-------------- $LINES >>$TMPFILE
        echo  >>$TMPFILE

        if [ "$LINES" -gt "5" ]
        then
                cat $TMPFILE
        else
                ty=$[ty+1]

                if [ "$ty" -gt "20" ]
                then
                        export ty=0
                        echo
                        echo -n "$CURRENTTIME : "
                fi

                echo -n "$LINES | "

                sleep 3
        fi


        sleep 3

done


#2,清除QMAIL的邮件队列,不清楚为啥加入任务计划就不执行,手动执行又没问题。其中grep -v fix_qmail是排除自身的文件名:

####################################################
#            Clear Qmail's queue
#
#                        --Ecen.thq 2011.11.02
####################################################
#!/bin/bash
clear
echo
echo clear mail queue
echo ===============================================
echo `date +%Y/%m/%d-%H:%M:%S`
echo
/etc/init.d/igenus-qmail stop

ps aux|grep qmail|grep -v fix_qmail|awk '{print $2}'|xargs kill -9

mv /var/qmail/queue /var/qmail/queue_bak`date "+%m%d"`

mkdir /var/qmail/queue

/var/qmail/bin/queue-fix /var/qmail/queue

/etc/init.d/igenus-qmail start

echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ps -aef|grep qmail
echo ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ " ps -aef|grep qmail "

echo  `date +%Y/%m/%d-%H:%M:%S` >> qmail_fix.log

echo
echo =================over=================

#3,mysql每日备份的脚本

#!/bin/sh
# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
#
# ----------------------------------------------------------------------

# your mysql login information
# db_user is mysql username
# db_passwd is mysql password
# db_host is mysql host
# -----------------------------
db_user="root"
db_passwd="********"
db_host="localhost"

# the directory for story your backup file.
backup_dir="/backup/mysql/"

# date format for backup file (dd-mm-yyyy)
time="$(date +"%M-%H-%d-%m-%Y")"

# mysql, mysqldump and some other bin's path
MYSQL="mysql"
MYSQLDUMP="mysqldump"
MKDIR="mkdir"
RM="rm"
MV="mv"
GZIP="gzip"

# check the directory for store backup is writeable
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0


# the directory for story the newest backup
test ! -d "$backup_dir/backup.0/" && $MKDIR "$backup_dir/backup.0/"


# get all databases
all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"


for db in $all_db
do
        $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/backup.0/$time.$db.gz"
done


# delete the oldest backup
test -d "$backup_dir/backup.5/" && $RM -rf "$backup_dir/backup.5"


# rotate backup directory
for int in 4 3 2 1 0
do
        if(test -d "$backup_dir"/backup."$int")
        then
                next_int=`expr $int + 1`
                $MV "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int"
        fi
done


exit 0;



你可能感兴趣的:(mysql,Date,centos,脚本,user,delete)