常用基础脚本

添加用户脚本


#!/bin/bash
#添加自定义用户,密码为随机密码,并保存到指定文件中,同时做备份
DATE=`date +%F-%T`
USER_FILE=/tmp/user.txt

echo_color() {
        if [ $1 == "green" ]
        then
                echo -e "\033[32;40m$2\033[0m"
        else
                echo -e "\033[31;40m$2\033[0m"
        fi

}

if [ -s $USER_FILE ]
then
        mv $USER_FILE ${USER_FILE}-${DATE}.bak
        echo_color green "$USER_FILE exist,rename ${USER_FILE}-${DATE}.bak"
fi

for USER in $1
do
        if ! id $USER &>/dev/null
        then

                useradd -m -s /bin/bash $USER
                PASS=`echo $RANDOM | md5sum | cut -c 1-8`
                echo $PASS | passwd --stdin $USER &> /dev/null
                if [ $? -ne 0 ]
                then

                        echo -e "$USER User create fail"
                else
                        echo -e "$USER User create successful."
                        echo -e "User   Password" >> $USER_FILE
                        echo "----------------" >> $USER_FILE
                        echo "$USER $PASS" >>$USER_FILE

                fi
          else
                echo "$USER is exit"
            fi
        done
if [[ ! -n "$1" ]]
then
    echo -e "Execution of the script error, please follow the example to run the script: \nExample: 'sh $0 nginx'"
fi

扫描主机存活脚本



#!/bin/bash
#扫描主机存活,并将扫描结果生成文本
. /etc/init.d/functions
for i in {1..50}
do
        ip=172.21.134.${i}
        ping -c2 -w2 $ip >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
                action "$ip" /bin/false
                echo "$ip" >>/tmp/ipdown.txt
        else
                action "$ip" /bin/true
                echo "$ip" >> /tmp/ipup.txt
        fi
done

实现MySQL数据库库备份

#!/bin/bash
#实现分库备份mysql
user=root
read -p "Please enter your password:" pass
MYCMD="mysql -u$user -p$pass"
MYDUMP="mydump -u$user -p$pass -x -B -F -R"
DBLIST=`$MYCMD -e "show databases;" | sed "1,2d" | egrep -v "_schema | mysql "`

for database in $DBLIST
do
	$MYDUMP $database | gzip > /tmp/${database}_$(date +%F).sql.gz &>/dev/null
done

实现MySQL数据库表备份

#!/bin/bash
user=root
read -p "please enter your password:" pass
MYCMD="mysql -u$user -p$pass"
MYDUMP="mysqldump -u$user -p$pass -x -F -R"
DBLIST=`$MYCMD -e "show databases;" | sed '1,2d' | egrep -v "_shema | mysql"`
for db in $DBLIST
do
  tablist=`$MYCMD -e "show tables from $db;" | sed 1d`
     for table in $tablist
     do
       mkdir -p /tmp/${db}
       $MYDUMP $db $table | gzip > /tmp/${db}/${table}_${date +%F}.sql.gzip
     done
done

判断程序是否正常运行

#!/bin/bash
#通过端口
. /etc/init.d/functions

port=`lsof -i:80 | grep nginx | wc -l`
if [ $port -ge 2 ]
then
	action "nginx is running" /bin/true
else
	action "nginx is not running" /bin/false
	/usr/local/nginx/sbin/nginx
	action "starting nginx" /bin/true
fi

#!/bin/bash
#通过进程
. /etc/init.d/functions

port=`ps -ef| grep nginx | grep -v grep | wc -l`

if [ $port -ge 2 ]
then
	action "nginx is running" /bin/true
else
	action "nginx is no running" /bin/false
	/usr/local/nginx/sbin/nginx
	action "starting nginx" /bin/true
fi

#!/bin/bash
#wget返回内容
. /etc/init.d/functions

port=`wget -T 5 --spider http://172.18.12.2 &>/dev/null`

if [ $port -ne 0 ]
then
	/usr/local/nginx/sbin/nginx
	action "starting nginx" /bin/true
else
	echo "nginx is no running" /bin/false
fi

#!/bin/bash
#返回状态码200判断
. /etc/init.d/functions

port=`curl -s -I -w "%{http_code}\n"` 172.18.12.2 -o /dev/null
if [ "$port" == "200" ];then
	action "nginx is running" /bin/true
else
	/usr/local/nginx/sbin/nginx
	action "starting nginx" /bin/true
fi





posted @ 2021-05-20 11:06  月黑风高人不善  阅读(84)  评论(0编辑  收藏  举报