1. 批量探测10.0.0.0/24网段的所有主机是否存活,存活的主机的远程连接端口是否开放。
1. 1-254 进行循环探测254台主机
2. 使用什么命令探测主机是否存活
ping -c1 -W1 IP地址 返回值为0则表示主机存活
3. 再进行判断存活的主机的远程端口是否开放
[root@shell /scripts/shell-day31]# cat for-1.sh
#!/bin/bash
#1.引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.定义变量
Ip_log=/tmp/ip.log
Port_log=/tmp/port.log
echo "探测日期为:$( date +%F)" >$Ip_log
echo "探测日期为:$( date +%F)" >$Port_log
#3.进行批量探测
for i in {1..254}
do
#批量执行循环体
{
Ip=10.0.0.$i
#4.进行判断主机是否存活
ping -c1 -W1 $Ip &>/dev/null
if [ $? -eq 0 ];then
action "$Ip 主机是存活的.............." /bin/true
echo "$Ip" >>$Ip_log
fi
} &
sleep 0.1
done
wait
echo "IP地址扫描探测完毕!开始探测主机远程端口!"
for i in $(sed '1d' $Ip_log)
do
State=$(nmap -p22 $i | awk '/^22/{print $2}')
if [ $State == "open" ];then
action "IP地址${i}的远程端口是开放的..." /bin/true
echo "IP地址${i}的远程端口是开放的..." >>$Port_log
else
action "IP地址${i}的远程端口是关闭的..." /bin/false
fi
done
[root@shell /scripts/shell-day31]# sh for-1.sh
10.0.0.60 主机是存活的.............. [ OK ]
10.0.0.254 主机是存活的.............. [ OK ]
IP地址扫描探测完毕!开始探测主机远程端口!
IP地址10.0.0.60的远程端口是开放的... [ OK ]
IP地址10.0.0.254的远程端口是关闭的... [FAILED]
[root@shell /scripts/shell-day31]# cat /tmp/ip.log
探测日期为:2020-03-07
10.0.0.60
10.0.0.254
[root@shell /scripts/shell-day31]# cat /tmp/port.log
探测日期为:2020-03-07
IP地址10.0.0.60的远程端口是开放的...
2. 写一个上课随机点名脚本
1. 要有名单
2. 随机打印这些人员名单进行循环 随机数不能大于人员的总数并且随机数不能等于0
[root@shell ~]# echo $(( $RANDOM % 30 + 1 ))
3. 将最后一次循环到的人员打印出来
[root@shell /scripts/shell-day31]# cat for-2.sh
#!/bin/bash
#1.统计文件的总行数
Number=$(wc -l student.txt |awk '{print $1}')
#2.指定循环的次数
read -p "请输入你要循环的次数:" Num
if [[ ! $Num =~ ^[0-9]+$ ]];then
echo "你输入的不是一个整数!"
exit
fi
#3.进行循环
for i in $(seq $Num)
do
#4.定义一个随机数,随机数不能大于文件的总数且不能等于0
Ran=$(( $RANDOM % $Number + 1 ))
#5.开始循环打印
sed -n "${Ran}p" student.txt
sleep 0.2
done
Stu_Name=$(sed -n "${Ran}p" student.txt)
echo "天玄子:$Stu_Name"
3. 针对数据库进行分库分表备份
mysqldump -uroot -p123 -B 库名 >database.sql
mysqldump -uroot -p123 库名 表名 >database_table.sql
备份所有的库和所有的表
[root@shell /scripts/shell-day31]# mysql -uroot -p123 -e "show databases;" |sed 1d |grep -v '.*schema'
mysql
test
wordpress
world
[root@shell /scripts/shell-day31]# mysql -uroot -p123 -e "use world;show tables;" | sed 1d
city
country
countrylanguage
[root@shell /scripts/shell-day31]# cat for-3.sh
#!/bin/bash
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#1.定义变量
DB_User=root
DB_Pass=123
DB_name=$(mysql -u$DB_User -p$DB_Pass -e "show databases;" |sed 1d |grep -v '.*schema')
Date=$(date +%F)
#2.备份数据库
for Database in $DB_name
do
DB_path=/backup/$Database
if [ ! -d $DB_path ];then
mkdir -p $DB_path &>/dev/null
fi
#备份数据库
mysqldump -u$DB_User -p$DB_Pass --single-transaction -B $Database >$DB_path/${Database}_${Date}.sql
if [ $? -eq 0 ];then
action "数据库${Database}备份成功..............." /bin/true
else
action "数据库${Database}备份失败..............." /bin/false
fi
#定义表的变量
TB_name=$(mysql -u$DB_User -p$DB_Pass -e "use $Database;show tables;"|sed 1d)
#备份表
for Table in $TB_name
do
mysqldump -u$DB_User -p$DB_Pass --single-transaction $Database $Table >$DB_path/${Database}_${Table}_${Date}.sql
if [ $? -eq 0 ];then
action "数据库${Database}中的${Table}表备份成功............" /bin/true
else
action "数据库${Database}中的${Table}表备份失败............" /bin/false
fi
done
done
[root@shell /scripts/shell-day31]# sh for-3.sh
数据库mysql备份成功............... [ OK ]
数据库mysql中的columns_priv表备份成功............ [ OK ]
数据库mysql中的db表备份成功............ [ OK ]
数据库mysql中的event表备份成功............ [ OK ]
数据库mysql中的func表备份成功............ [ OK ]
数据库mysql中的general_log表备份成功............ [ OK ]
数据库mysql中的help_category表备份成功............ [ OK ]
数据库mysql中的help_keyword表备份成功............ [ OK ]
数据库mysql中的help_relation表备份成功............ [ OK ]
数据库mysql中的help_topic表备份成功............ [ OK ]
数据库mysql中的host表备份成功............ [ OK ]
数据库mysql中的ndb_binlog_index表备份成功............ [ OK ]
数据库mysql中的plugin表备份成功............ [ OK ]
数据库mysql中的proc表备份成功............ [ OK ]
数据库mysql中的procs_priv表备份成功............ [ OK ]
数据库mysql中的proxies_priv表备份成功............ [ OK ]
数据库mysql中的servers表备份成功............ [ OK ]
数据库mysql中的slow_log表备份成功............ [ OK ]
数据库mysql中的tables_priv表备份成功............ [ OK ]
数据库mysql中的time_zone表备份成功............ [ OK ]
数据库mysql中的time_zone_leap_second表备份成功............ [ OK ]
数据库mysql中的time_zone_name表备份成功............ [ OK ]
数据库mysql中的time_zone_transition表备份成功............ [ OK ]
数据库mysql中的time_zone_transition_type表备份成功.........[ OK ]
数据库mysql中的user表备份成功............ [ OK ]
数据库test备份成功............... [ OK ]
数据库wordpress备份成功............... [ OK ]
数据库wordpress中的wp_commentmeta表备份成功............ [ OK ]
数据库wordpress中的wp_comments表备份成功............ [ OK ]
数据库wordpress中的wp_links表备份成功............ [ OK ]
数据库wordpress中的wp_options表备份成功............ [ OK ]
数据库wordpress中的wp_postmeta表备份成功............ [ OK ]
数据库wordpress中的wp_posts表备份成功............ [ OK ]
数据库wordpress中的wp_term_relationships表备份成功.........[ OK ]
数据库wordpress中的wp_term_taxonomy表备份成功............ [ OK ]
数据库wordpress中的wp_termmeta表备份成功............ [ OK ]
数据库wordpress中的wp_terms表备份成功............ [ OK ]
数据库wordpress中的wp_usermeta表备份成功............ [ OK ]
数据库wordpress中的wp_users表备份成功............ [ OK ]
数据库world备份成功............... [ OK ]
数据库world中的city表备份成功............ [ OK ]
数据库world中的country表备份成功............ [ OK ]
数据库world中的countrylanguage表备份成功............ [ OK ]
[root@shell /scripts/shell-day31]# ll /backup/
total 8
drwxr-xr-x 2 root root 4096 2020-03-07 11:13 mysql
drwxr-xr-x 2 root root 33 2020-03-07 11:13 test
drwxr-xr-x 2 root root 4096 2020-03-07 11:13 wordpress
drwxr-xr-x 2 root root 147 2020-03-07 11:13 world
[root@shell /scripts/shell-day31]# ll /backup/mysql/
total 1076
-rw-r--r-- 1 root root 514805 2020-03-07 11:17 mysql_2020-03-07.sql
-rw-r--r-- 1 root root 2374 2020-03-07 11:17 mysql_columns_priv_2020-03-07.sql
-rw-r--r-- 1 root root 3588 2020-03-07 11:17 mysql_db_2020-03-07.sql
-rw-r--r-- 1 root root 3661 2020-03-07 11:17 mysql_event_2020-03-07.sql
-rw-r--r-- 1 root root 2023 2020-03-07 11:17 mysql_func_2020-03-07.sql
-rw-r--r-- 1 root root 2049 2020-03-07 11:17 mysql_general_log_2020-03-07.sql
-rw-r--r-- 1 root root 3249 2020-03-07 11:17 mysql_help_category_2020-03-07.sql
-rw-r--r-- 1 root root 9600 2020-03-07 11:17 mysql_help_keyword_2020-03-07.sql
-rw-r--r-- 1 root root 11831 2020-03-07 11:17 mysql_help_relation_2020-03-07.sql
-rw-r--r-- 1 root root 467162 2020-03-07 11:17 mysql_help_topic_2020-03-07.sql
-rw-r--r-- 1 root root 3263 2020-03-07 11:17 mysql_host_2020-03-07.sql
-rw-r--r-- 1 root root 2126 2020-03-07 11:17 mysql_ndb_binlog_index_2020-03-07.sql
-rw-r--r-- 1 root root 1875 2020-03-07 11:17 mysql_plugin_2020-03-07.sql
-rw-r--r-- 1 root root 3549 2020-03-07 11:17 mysql_proc_2020-03-07.sql
-rw-r--r-- 1 root root 2460 2020-03-07 11:17 mysql_procs_priv_2020-03-07.sql
-rw-r--r-- 1 root root 2499 2020-03-07 11:17 mysql_proxies_priv_2020-03-07.sql
-rw-r--r-- 1 root root 2189 2020-03-07 11:17 mysql_servers_2020-03-07.sql
-rw-r--r-- 1 root root 2189 2020-03-07 11:17 mysql_slow_log_2020-03-07.sql
-rw-r--r-- 1 root root 2562 2020-03-07 11:17 mysql_tables_priv_2020-03-07.sql
-rw-r--r-- 1 root root 1934 2020-03-07 11:17 mysql_time_zone_2020-03-07.sql
-rw-r--r-- 1 root root 2008 2020-03-07 11:17 mysql_time_zone_leap_second_2020-03-07.sql
-rw-r--r-- 1 root root 1922 2020-03-07 11:17 mysql_time_zone_name_2020-03-07.sql
-rw-r--r-- 1 root root 2059 2020-03-07 11:17 mysql_time_zone_transition_2020-03-07.sql
-rw-r--r-- 1 root root 2201 2020-03-07 11:17 mysql_time_zone_transition_type_2020-03-07.sql
-rw-r--r-- 1 root root 5702 2020-03-07 11:17 mysql_user_2020-03-07.sql
4. 判断数据库主从复制是否异常脚本
1. 判断IO和SQL线程是否都为Yes
2. 如果不是Yes就是主从复制异常,发送邮件给管理员
3. 再去判断是IO线程问题还是SQL线程问题
4. 自动化的解决一些简单的问题
5. 从库写入 ,这个问题怎么解决 1007 可以不可以执行一次跳过的命令。
准备主从复制环境
[root@db02 ~]# cat mysql_zhucong.sh
#!/bin/bash
#1.定义变量
IO_Status=$(mysql -uroot -p123 -e "show slave status\G" |awk '/Slave_IO_Running/{print $NF}')
SQL_Status=$(mysql -uroot -p123 -e "show slave status\G" |awk '/Slave_SQL_Running/{print $NF}')
#2.判断主从是否异常
if [ $IO_Status == "Yes" -a $SQL_Status == "Yes" ];then
echo "主从复制正常!"
else
#3.判断IO线程是否正常
if [ $IO_Status == "Yes" ];then
echo "主从复制IO线程正常!"
else
echo "主从复制IO线程异常!"
mysql -uroot -p123 -e "show slave status\G" |grep "Last_IO" >/tmp/io_err.log
mail -s "test" xxxxx@qq.com < /tmp/io_err.log
if [ $? -eq 0 ];then
echo "邮件已发送给管理员!"
else
echo "邮件发送失败!"
fi
fi
#4.判断SQL线程是否正常
if [ $SQL_Status == "Yes" ];then
echo "主从复制SQL线程正常!"
else
echo "主从复制SQL线程异常!"
SQL_Err=$(mysql -uroot -p123 -e "show slave status\G" |awk '/Last_SQL_Errno/{print $NF}')
case $SQL_Err in
1007)
echo "脚本判断,SQL线程出错为从库写入!脚本尝试进行一次跳过错误来解决问题!"
mysql -uroot -p123 -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
SQL_Status=$(mysql -uroot -p123 -e "show slave status\G" |awk '/Slave_SQL_Running/{print $NF}')
if [ $SQL_Status == "Yes" ];then
echo "脚本已经成功的执行一次跳过!主从复制正常!"
echo "脚本已经成功的执行一次跳过!主从复制正常!" | mail -s "主从复制SQL线程报错!尝试跳过一次成功!" xxxxx@qq.com
else
echo "脚本尝试跳过一次错误!但是主从复制还是异常!"
mysql -uroot -p123 -e "show slave status\G" |grep "Last_SQL" >/tmp/sql_err.log
mail -s "test" xxxxx@qq.com < /tmp/sql_err.log
if [ $? -eq 0 ];then
echo "邮件已发送给管理员!"
else
echo "邮件发送失败!"
fi
fi
;;
*)
echo "脚本判断,SQL线程出错!脚本无法解决故障!"
mysql -uroot -p123 -e "show slave status\G" |grep "Last_SQL" >/tmp/sql_err.log
mail -s "test" xxxxx@qq.com < /tmp/sql_err.log
if [ $? -eq 0 ];then
echo "邮件已发送给管理员!"
else
echo "邮件发送失败!"
fi
esac
fi
fi