SHELL训练营--day30_shell练习91-95

#部署mysql主从
#!/bin/bash
master_ip=192.168.100.12
slave_ip=192.168.100.13
mysqlc="mysql -uroot -psincethen"

check_ok()
{
    if [ $? -ne 0 ]
    then
        echo "$1 出错了"
        exit 1
    fi
}
f_exist()
{
    d=`date +%F%T`
    if [ -f $1 ]
    then
        mv $1 $1_$d
    fi
}

if ! grep '^server-id' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\server-id = 1001' /etc/my.cnf
fi

if !grep '^log-bin.*=.*' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\log-bin = aminglinux' /etc/my.cnf
fi
sed -i '/^log-bin.*/a\binlog-ignore-db = mysql' /etc/my.cnf

/etc/init.d/mysqld restart

check_ok "请重启mysql主服务器"

$mysqlc <<EOF
    grant replication slave on *.* to 'repl'@$slave_ip identified by "password";
    flush tables with read lock;
EOF

$mysqlc -e "show master status;" > /tmp/master.log
file=`tail -l /tmp/master.log |awk '{print $1}'`
pos=`tail -l /tmp/master.log |awk '{print $2}'`

f_exist /tmp/slave.sh
cat > /tmp/slave.sh <<EOF
#!/bin/bash
if ! grep '^server-id' /etc/my.cnf
then
    sed -i '/^\[mysqld\]$/a\server-id = 1002' /etc/my.cnf
fi

/etc/init.d/mysqld restart
check_ok "从服务器重启mysql"

$mysqlc <<EiF
    stop slave;
    change master to master_host="$master_ip", master_user="repl",\
    master_password="password",master_log_file="$file",master_log_pos=$pos;
    start slave;
EiF
EOF

f_exist /tmp/rs_slave.expect

cat > /tmp/rs_slave.expect <<EOF
set passwd "sincethen"
spawn rsync -a /tmp/slave.sh root@slave_ip:/tmp/slave.sh
expect {
    "yes/no" { send "yes\r" }
    "password:" { send "\$passwd\r" }
}
expect eof
EOF

chmod +x /tmp/rs_slave.expect
/tmp/rs_slave.expect
check_ok "传输slave.sh"

f_exist /tmp/exe.expect

cat > /tmp/exe.expect <<EOF
#!/usr/bin/expect
set passwd "sincethen"
spawn ssh root@$slave_ip
expect {
    "yes/no" { send "yes\r" }
    "password:" { send "\$passwd\r" }
}
expect "=]*"
send "/bin/bash /tmp/slave.sh\r"
expect "]*"
send "exit\r"
EOF

chmod +x /tmp/exe.expect
/tmp/exe.expect
check_ok "远程执行slave.sh"

$mysqlc -e "unlock tables;"

#管理docker
#!/bin/bash
while true
do
    read -p "请输入你要执行的操作:(stop|start|rm)" opt
    if [ -z "$opt" ]
    then
        echo "请输入操作"
        continue
    else
        break
    fi
done

docker ps -a |awk '{print $1}' > /tmp/id.txt
case $opt in 
    stop)
        for id in `cat /tmp/id.txt`
        do
            docker stop $id
        done
        ;;
    start)
        for id in `cat /tmp/id.txt`
        do
            docker start $id
        done
    ;;
    rm)
        for id in `cat /tmp/id.txt`
        do
            read -p "将要删除容器$id,是否继续?(y|n)" c
            case $c in
            y|Y)
                docker rm -f $id
                ;;
            n|N)
                echo "容器$id不删除。"
                ;;
            *)
                echo "只能输入'y'或'n'."
                ;;
            esac
        done
    ;;
    *)
        echo "请输入 start|stop|rm."
    ;;
case

#安装samba
#!/bin/bash
if [ "$#" -ne 1 ]
then
    echo "运行脚本的格式为:$0 /dir/"
    exit 1
else
    if ! echo $1 |grep -q '^/.*'
    then
        echo "请提供一个绝对路径。"
        exit 1
    fi
fi

if ! rpm -q samba >/dev/null
then
    echo "将要安装samba"
    sleep 1
    yum install -y samba
    if [ $? -ne 0 ]
    then
        echo "samba安装失败。"
        exit 1
    fi
fi

cnfdir="/etc/samba/smb.conf"
cat >> $cnfdir <<EOF
[share]
    comment = share all
    path = $1
    browseable = yes
    public = yes
    writable = no
EOF
if [ ! -d $1 ]
then
    mkdir -p $1
fi
chmod 777 $1
echo "test" > $1/test.txt

systemctl start smb

if [ $? -ne 0 ]
then
    echo "samba 服务启动失败,请检查脚本。"
else
    echo "samba 服务启动成功。"
fi

#批量查看多台机器负载
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
    echo $ip
    ssh $ip "uptime"
done

#自动挂载云盘
#!/bin/bash
if [ $# -ne 2 ]
then
    echo "Useage $0 盘符 挂载点,如: $0 /dev/xvdb /data"
    exit 1
fi

if [ ! -b $1 ]
then
    echo "你提供的盘符不对,请检查。"
    exit 1
fi

echo "格式化$1"    
mkfs -t ext4 $1

if [ ! -d $2 ]
then
    mkdir -p $2
fi
n=`awk '$NF == "$2"' /etc/fstab|wc -l`
if [ $n -eq 0 ]
then
    echo "$1        $2      ext4 default 0 0" >> /etc/fstab
    mount -a
else
    echo "配置文件/etc/fstab已存在挂载点$2,请检查。"
    exit 1
fi

猜你喜欢

转载自blog.51cto.com/sincethen/2347432
今日推荐