shell編程實戰20

1. 部署mysql主從mysql

#!/bin/bash
master_ip=192.168.100.12
slave_ip=192.168.100.13
mysqlc="mysql -uroot -paminglinux"linux

check_ok()
{
if [ $? -ne 0 ]
then
echo "$1 出錯了。"
exit 1
fi
}sql

f_exist()
{
d=`date +%F%T`
if [ -f $1 ]
then
mv $1 $1_$d
fi
}docker

## 設置主mysql配置
if ! grep '^server-id' /etc/my.cnf
then
sed -i '/^\[mysqld\]$/a\server-id = 1001' /etc/my.cnf
fibash

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

sed -i '/^log-bin.*/a\binlog-ignore-db = mysql ' /etc/my.cnfide

/etc/init.d/mysqld restart
check_ok "主上重啓mysql"spa

## 登陸mysql,受權用戶、鎖表以及show master status。
$mysqlc <<EOF
grant replication slave on *.* to 'repl'@$slave_ip identified by 'yourpassword';
flush tables with read lock;
EOF
$mysqlc -e "show master status" > /tmp/master.log
file=`tail -1 /tmp/master.log|awk '{print $1}'`
pos=`tail -1 /tmp/master.log|awk '{print $2}'`rest

## 建立在從上配置和操做的腳本
f_exist /tmp/slave.shserver

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 <<EOF
stop slave;
change master to master_host="$master_ip", master_user="repl", master_password="yourpassword", master_log_file="$file", master_log_pos=$pos;
start slave;
EOF
EOF

## 建立傳輸slave.sh的expect腳本
f_exist /tmp/rs_slave.expect

cat > /tmp/rs_slave.expect <<EOF
#!/usr/bin/expect
set passwd "aminglinux"
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

## 執行expect腳本
chmod +x /tmp/rs_slave.expect
/tmp/rs_slave.expect
check_ok "傳輸slave.sh"

## 建立遠程執行命令的expect腳本
f_exist /tmp/exe.expect

cat > /tmp/exe.expect <<EOF
#!/usr/bin/expect
set passwd "aminglinux"
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

## 執行expect腳本
chmod +x /tmp/exe.expect
/tmp/exe.expect
check_ok "遠程執行slave.sh"

## 主上解鎖表
$mysqlc -e "unlock tables"

2. 管理docker

#!/bin/bash

while true
do
read -p "請輸入你要執行的操做:(stop/start/rm) " opt
if [ -z "$opt" ]
then
echo "請輸入要執行的操做。"
continue
else
break
fi
done

## 容器的id存放到一個臨時文件裏,方便後續遍歷
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。"
;;
esac

3. 安裝配置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

#假設系統爲CentOS7
systemctl start smb
if [ $? -ne 0 ]
then
echo "samba服務啓動失敗,請檢查配置文件是否正確。"
else
echo "samba配置完畢,請驗證。"
fi

4. 批量查看多臺機器負載

 

5. 自動掛雲盤

 

相關文章
相關標籤/搜索