目录:

  1. shell基础变量逻辑运算符
  2. scp基础用法脚本
  3. while+for+case基础用法脚本
  4. 内核优化脚本
  5. 自动修改本机ip脚本
  6. for+case 查询日志脚本
  7. 一键yum安装lamp脚本
  8. 源码安装lamp
  9. tcpping.py(python脚本)
  10. ssh免密钥脚本
  11. nginx日志切割脚本
  12. mysql主从配置安装
  13. mysqldump_backmysql脚本
  14. innobackupex_mysqlbackup脚本
  15. innobackupex_mysqlbackup_zeng_quan脚本(周7全量,其余天增量)
  16. innobackupex_数据库恢复脚本
  17. 备份系统文件目录(增全)
  18. docker一键安装容器启动脚本
  19. docker容器重启自动修改ip脚本
  20. 批量新增用户脚本
  21. 监控日志是否刷新脚本
  22. 磁盘监控脚本
  23. system_监控脚本
  24. 同步备份日志脚本
  25. ftp同步脚本
  26. rsync同步目录脚本
  27. rsync同步目录脚本1
  28. tcp连接数
  29. tcp_dump脚本
  30. telnet端口脚本
  31. nc端口脚本
  32. gzip日志打压脚本
  33. 9*9乘法运算
  34. 监控最近一小时错误日志与前一日同时段错误日志量比较
  • shell基础变量逻辑运算符

系统变量:

$0 当前脚本的名称

$n 当前脚本的第n个参数,n=1,2,…9

$* 当前脚本的所有参数(不包括程序本身)

$# 当前脚本的参数个数(不包括程序本身)

$? 命令或程序执行完后的状态,一般返回0表示执行成功。

$UID 当前用户的ID

$PWD 当前所在的目录

逻辑运算符解析:

-f 判断文件是否存在 eg: if [ -f filename ]

-d 判断目录是否存在 eg: if [ -d dir     ]

-eq 等于 应用于:整型比较 equal

-ne 不等于 应用于:整型比较 not equal

-lt 小于 应用于:整型比较 letter

-gt 大于 应用于:整型比较 greate

-le 小于或等于 应用于:整型比较

-ge 大于或等于 应用于:整型比较

-a 双方都成立(and) 逻辑表达式 –a 逻辑表达式

-o 单方成立(or) 逻辑表达式 –o 逻辑表达式

-z 空字符串

-n 字符串非空

-x file 当file是一个可执行文件时,返回True

  • scp基础用法脚本
#!/bin/bash

#auto scp files for client

#by authors wugk
if [ -z "$1" -o -z "$2" ];then
echo "please usage:$0 {/data/text.txt /data/ }"
exit
fi
for i in `cat list.txt| grep -v "^#"` do
echo "the follow result $i"
scp -r $ root@$i:$
done
  • while+for+case基础用法
#!/bin/bash
cat >xxxx.txt <<EOF
qq.com
192.168.1.115
EOF
echo -e "\033[32m--------------------------------------------------------------------------------\033[1m"
cat <<EOF
+++++++++++++++这只是几种方法都可以ping到 还有很多方法这里没写'+++++++++++++++++++++
++++++++++++++++++++++++说明脚本书写的方式很多 看大家喜好+++++++++++++++++++++++++++++++
EOF
echo -e "\033[32m--------------------------------------------------------------------------------\033[0m" ############################################
for i in `cat xxxx.txt`
do
case $i in
qq.com)
ping $i -c2
;;
192.168.1.115)
ping $i -c2
;;
*)
;;
esac
done
  • 内核优化脚本
#!/bin/bash
#by cxm
#优化系统参数
optimize(){ isOptimized=`cat /etc/security/limits.conf |grep |wc -l`
if [ ! $isOptimized = "" ]; then
echo "已经优化过了"
return ;
fi cat >> /etc/security/limits.conf << EFF
* soft nofile
* hard nofile
* soft nproc
* hard nproc
* soft memlock -
* hard memlock -
EFF sed -i "s/soft.*nproc.*/soft nproc 409600/g" /etc/security/limits.d/-nproc.conf; #7版本一下的都是20-nproc.conf echo "session required /lib64/security/pam_limits.so" >>/etc/pam.d/login; modprobe bridge;
lsmod|grep bridge;
cat >> /etc/sysctl.conf << EFF
net.ipv4.ip_forward =
net.ipv4.conf.default.rp_filter =
net.ipv4.conf.default.accept_source_route =
kernel.sysrq =
kernel.core_uses_pid =
net.ipv4.tcp_syncookies =
kernel.msgmnb =
kernel.msgmax =
kernel.shmmax =
kernel.shmall =
net.ipv4.tcp_max_tw_buckets =
net.ipv4.tcp_sack =
net.ipv4.tcp_window_scaling =
net.ipv4.tcp_rmem =
net.ipv4.tcp_wmem =
net.core.wmem_default =
net.core.rmem_default =
net.core.rmem_max =
net.core.wmem_max =
net.core.netdev_max_backlog =
net.core.somaxconn = 65535
net.ipv4.tcp_max_orphans =
net.ipv4.tcp_max_syn_backlog =
net.ipv4.tcp_timestamps =
net.ipv4.tcp_synack_retries =
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_tw_recycle =
net.ipv4.tcp_tw_reuse =
net.ipv4.tcp_mem =
net.ipv4.tcp_fin_timeout =
net.ipv4.tcp_keepalive_time =
net.ipv4.ip_local_port_range = EFF
sysctl -p; cat >> /etc/profile << EFF
ulimit -SHl unlimited
ulimit -SHu
ulimit -SHn
EFF cat >> ~/.bashrc << EFF
ulimit -SHl unlimited
ulimit -SHu
ulimit -SHn
EFF source /etc/profile
source ~/.bashrc } optimize
  • 自动修改本机ip脚本
#!/bin/bash
#auto change IP address
if [ -z $ ];then
echo -e "\033[32m--------------------------\033[1m"
echo "Usage:{Exec $0 192.168.1.11}"
echo -e "\033[32m\033[0m"
exit
fi echo -e "\033[32m--------------------------\033[1m"
NUM_IP=`echo $|awk -F. '{print $1,$2,$3,$4}'|sed 's/ /\n/g'|grep -v "^$"|wc -l`
while [ $NUM_IP -ne ]
do
read -p "Please Retry Avaliable IP Address:" IPADDR
echo $IPADDR|grep -E --color "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
if [ $? -eq ];then
IP_1=`echo $IPADDR|awk -F. '{print $1}'`
IP_2=`echo $IPADDR|awk -F. '{print $2}'`
IP_3=`echo $IPADDR|awk -F. '{print $3}'`
IP_4=`echo $IPADDR|awk -F. '{print $4}'`
NUM=""
if [ $IP_1 -le $NUM -a $IP_2 -le $NUM -a $IP_3 -le $NUM -a $IP_4 -le $NUM ];then
NUM_IP=`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'|sed 's/ /\n/g'|grep -v "^$"|wc -l`
fi
fi done NETWORK_CONF="/etc/sysconfig/network-scripts/ifcfg-eth0"
DNS_CONF="/etc/resolv.conf"
HOSTNAME_CONF="/etc/sysconfig/network"
BACKUP_DIR="/data/backup/`date +%Y%m%d`"
DATE=`date +%H%M`
IPADDR="$1"
HOSTNAME=`echo $IPADDR|awk -F. '{print "BeiJing-IDC-WEB-"$(NF-1)"-"$NF}'`
NETMASK="255.255.255.0"
GATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3".""1"}'`
DNS="8.8.8.8" if [ ! -d $BACKUP_DIR ];then
mkdir -p $BACKUP_DIR
fi
##backup and change server ip address
cp $NETWORK_CONF $BACKUP_DIR/
cp $DNS_CONF $BACKUP_DIR/
grep "static" $NETWORK_CONF >>/dev/null
if [ $? -eq ];then
sed -i "s/^IPADDR.*/IPADDR=$IPADDR/g" $NETWORK_CONF
sed -i "s/^NETMASK.*/NETMAKS=$NETMASK/g" $NETWORK_CONF
sed -i "s/^GATEWAY.*/GATEWAY=$GATEWAY/g" $NETWORK_CONF
sed -i "s/^ONBOOT.*/ONBOOT=yes/g" $NETWORK_CONF
echo "nameserver $DNS" >$DNS_CONF
hostname $HOSTNAME
sed -i "s/^HOSTNAME.*/HOSTNAME=$HOSTNAME/g" $HOSTNAME_CONF
echo "$IPADDR $HOSTNAME" >>/etc/hosts
else
sed -i "s/^BOOTPROTO.*/BOOTPROTO=static/g" $NETWORK_CONF
cat >>$NETWORK_CONF<<EOF
IPADDR=$IPADDR
NETMASK=$NETMASK
GATEWAY=$GATEWAY
EOF
sed -i "s/^ONBOOT.*/ONBOOT=yes/g" $NETWORK_CONF
hostname $HOSTNAME
sed -i "s/^HOSTNAME.*/HOSTNAME=$HOSTNAME/g" $HOSTNAME_CONF
echo "$IPADDR $HOSTNAME" >>/etc/hosts
echo "nameserver $DNS" >$DNS_CONF
fi
echo -e "\033[32mNetWork Config Change successfully,Follow:\033[0m"
echo -e "\033[32m------------------------\033[0m"
cat $NETWORK_CONF
/etc/init.d/network restart >>/dev/null >&
echo -e "\033[32m------------------------\033[0m"
  • for+case 查询日志脚本
#/bin/sh

Dir=/fiodata/DataFiles/outside/NGLogHour
riqi=`date -d "-1 day" +%Y-%m-%d`
echo "+++++++++++++++++++++++++++++++++++++++"
echo " FLGs were created on $riqi"
echo "+++++++++++++++++++++++++++++++++++++++" for dir in `ls $Dir/$riqi`
do
case $dir in nginxjf)
cd $Dir/$riqi/nginxjf
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxjf is $tj"
;;
nginxwap)
cd $Dir/$riqi/nginxwap
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxwap is $tj"
;;
nginxwww)
cd $Dir/$riqi/nginxwww
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxwww is $tj"
;;
nginxxf)
cd $Dir/$riqi/nginxxf
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxxf is $tj"
;;
nginxxf_wap)
cd $Dir/$riqi/nginxxf_wap
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxxf_wap is $tj"
;;
nginxnlzf)
cd $Dir/$riqi/nginxnlzf
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxnlzf is $tj"
;;
nginxzqsw3w)
cd $Dir/$riqi/nginxzqsw3w
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxzqsw3w is $tj"
;;
nginxzqswwap)
cd $Dir/$riqi/nginxzqswwap
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxzqswwap is $tj"
;;
nginxhtml5)
cd $Dir/$riqi/nginxhtml5
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxhtml5 is $tj"
;;
nginxzqsw)
cd $Dir/$riqi/nginxzqsw
tj=`ls | grep FLG | wc -l`
echo "The FLGs of nginxzqsw is $tj"
;;
*) ;;
esac
done
  • 一键yum安装lamp脚本
#!/bin/bash
lamp="httpd httpd-devel php php-devel mysql mysq-server php-mysql mysql-devel"
yum -y install $lamp
if [ $? -eq ];then
echo "yum安装lamp安装成功"
fi #start httpd mysqld server
pkill httpd
sleep
/etc/init.d/httpd restart
#初始化数据库
/usr/bin/mysql_install_db --user=mysql
/etc/init.d/mysqld restart
#创建mysql数据库
mysql -e "create database discuz charset=utf8;"
mysql -e "grant all on *.* to discuz@'localhost' identified by '123456';"
mysql -e "flush privileges;"
mysql -e "show databases;"|grep "discuz" >/dev/null
if [ $? -eq ];then
echo " 数据库创建成功"
fi
cat >/var/www/html/index.php <<EOF
<?php
phoinfo();
?>
EOF # access to http web
ip=`ifconfig eth0 |grep "Bcast" |awk '{print $2}'| cut -d: -f2`
echo -e "you can to access http://$ip/index.php"
  • 源码安装lamp
#/bin/bash
#auto install LAMP
#by csy -- #Httpd define path variable
A_FILES=apr-1.5..tar.gz
A_FILES_DIR=apr-1.5.
A_URL=http://mirrors.cnnic.cn/apache//apr/
A_PREFIX=/usr/local/apr
AU_FILES=apr-util-1.5..tar.gz
AU_FILES_DIR=apr-util-1.5.
AU_URL=http://mirrors.cnnic.cn/apache//apr/
AU_PREFIX=/usr/local/apr-util
PC_FILES=pcre-8.37.tar.gz
PC_FILES_DIR=pcre-8.37
PC_URL=http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/
PC_PREFIX=/usr/local/pcre
H_FILES=httpd-2.4..tar.gz
H_FILES_DIR=httpd-2.4.
H_URL=http://mirror.bit.edu.cn/apache//httpd/
H_PREFIX=/usr/local/apache2/ #Mysql define path variable
M_FILES=mysql-5.6..tar.gz
M_FILES_DIR=mysql-5.6.
M_URL=http://mirrors.linuxeye.com/lnmp/src/
M_PREFIX=/usr/local/mysql/ #PHP define path variable
P_FILES=php-5.6..tar.gz
P_FILES_DIR=php-5.6.
P_URL=http://cn2.php.net/distributions/
P_PREFIX=/usr/local/php/ echo -e "\033[34m------------------------------------------\033[1m"
echo -e "\033[35mReady to set up LAMP source environment\nWait for 5 seconds, please select menu\033[0m"
sleep
if [ -z "$1" ];then
echo -e "\033[36mPlease Select install menu follow:\033[0m"
echo -e "\033[32m1)编译安装Apache服务器\033[1m"
echo "2)编译安装Mysql服务器"
echo "3)编译安装PHP服务器"
echo "4)配置index.php并启动LAMP服务"
echo "----------------------------------------------"
echo "please input you select usage:{$0 1|2|3|4|help..}"
exit
fi if [[ "$1" -eq "help" ]];then
echo -e "\033[36mPlease Select Install Menu follow:\033[0m"
echo -e "\033[32m1)编译安装Apache服务器\033[1m"
echo "2)编译安装MySQL服务器"
echo "3)编译安装PHP服务器"
echo "4)配置index.php并启动LAMP服务"
echo "please input you select usage:{$0 1|2|3|4|help..}"
exit
fi ##################################
#Install httpd web server
if [[ "$1" -eq "" ]];then
#/etc/init.d/iptables stop
#sed -i 's/SELINUX=enforcing/SELINUX=disabled' /etc/sysconfig/selinux
yum -y install gcc* wget openssl openssl-devel
wget -c $A_URL/$A_FILES && tar -zxvf $A_FILES && cd $A_FILES_DIR && ./configure --prefix=$A_PREFIX
if [ $? -eq ];then
make && make install
echo -e "\033[32m The $A_FILES_DIR install OK!\033[0m"
else
echo -e "\033[32m the $A_FILES_DIR install error,please check...\033[0m"
exit
fi wget -c $AU_URL/$AU_FILES && tar -zxvf $AU_FILES && cd $AU_FILES_DIR && ./configure --prefix=$AU_PREFIX --with-apr=$A_PREFIX
if [ $? -eq ];then
make && make install
echo -e "\033[32m The $AU_FILES_DIR install OK!\033[0m"
else
echo -e "\033[32m the $AU_FILES_DIR install error,please check...\033[0m"
exit
fi wget -c $PC_URL/$PC_FILES && tar -zxvf $PC_FILES && cd $PC_FILES_DIR && ./configure --prefix=$PC_PREFIX
if [ $? -eq ];then
make && make install
echo -e "\033[32m The $PC_FILES_DIR install OK!\033[0m"
else
echo -e "\033[32m the $PC_FILES_DIR install error,please check...\033[0m"
exit
fi
wget -c $H_URL/$H_FILES && tar -zxvf $H_FILES && cd $H_FILES_DIR && ./configure --prefix=$H_PREFIX --with-apr=$A_PREFIX --with-apr-util=$AU_PREFIX --with-pcre=$PC_PREFIX --enable-so --enable-rewrite
if [ $? -eq ];then
make && make install
echo "ServerName localhost:80" >> /usr/local/apache2/conf/httpd.conf
cp $H_PREFIX/bin/apachectl /etc/init.d/httpd
chkconfig --add httpd && chkconfig --level httpd on
/etc/init.d/httpd restart
ps -rf |grep httpd
echo -e "\033[32m----------------------------------\033[0m"
echo -e "\033[32m The $H_FILES_DIR Server install Success ! \033[0m"
else
echo -e "\033[32m The $H_FILES_DIR Make or Make install ERROP,please check....\033[0m"
exit
fi
fi #Install mysql DB server
if [[ "$1" -eq "" ]];then
yum -y install make cmake gcc-c++ bison ncurses ncurses-devel
wget -c $M_URL/$M_FILES && tar -zxvf $M_FILES && cd $M_FILES_DIR && cmake ./ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE= \
-DWITH_INNOBASE_STORAGE_ENGINE= \
-DWITH_MEMORY_STORAGE_ENGINE= \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_TCP_PORT= \
-DENABLED_LOCAL_INFILE= \
-DWITH_PARTITION_STORAGE_ENGINE= \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
if [ $? -eq ];then
make -j8 && make install
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
cd $M_PREFIX
chown -R mysql.mysql .
scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --user=mysql
chown -R mysql.mysql ./data
cp support-files/mysql.server /etc/init.d/mysqld
bin/mysqld_safe --user=mysql &
ln -s $M_PREFIX/bin/mysql /usr/bin/mysql
chmod +x /etc/init.d/mysqld
rm -rf /etc/my.cnf
cat > /etc/my.cnf <<EOF

  [client]
  port = 3306
  default-character-set=utf8

[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysqld.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=
[mysqld_safe]
log-error=/usr/local/mysql/mysqld.log
pid-file=//usr/local/mysql/mysqld.pid
EOF
/etc/init.d/mysqld restart
chkconfig --add mysqld
chkconfig mysqld on
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32mThe $M_FILES_DIR Server Install Success !\033[0m"
else
echo -e "\033[32mThe $M_FILES_DIR Make or Make install ERROR,Please Check......"
exit
fi
fi #Install PHP server
if [[ "$1" -eq "" ]];then
yum -y install zlib zlib-devel gd curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel
wget -c $P_URL/$P_FILES && tar -zxvf $P_FILES && cd $P_FILES_DIR &&./configure --prefix=$P_PREFIX --enable-fpm --enable-mbstring --enable-sockets --enable-debug --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-curl --with-config-file-path=/usr/local/php/etc --with-apxs2=$H_PREFIX/bin/apxs --with-mysql=$M_PREFIX/ --with-mysqli=$M_PREFIX/bin/mysql_config
if [ $? -eq ];then
make && make test && make install
cp php.ini-development /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =/date.timezone ='UTC'/g' /usr/local/php/etc/php.ini
export PATH=$PATH:/usr/local/php/bin
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32mThe $P_FILES_DIR Server Install Success !\033[0m"
else
echo -e "\033[32mThe $P_FILES_DIR Make or Make install ERROR,Please Check......"
exit
fi
fi
####################################
if [[ "$1" -eq "" ]];then sed -i 's/DirectoryIndex index.html/ DirectoryIndex index.php index.html/g' $H_PREFIX/conf/httpd.conf
echo "AddType application/x-httpd-php .php" >>$H_PREFIX/conf/httpd.conf
$H_PREFIX/bin/apachectl restart
IP=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f2`
echo "You can access http://$IP/index.php" cat >$H_PREFIX/htdocs/index.php <<EOF
<?php
phpinfo();
?>
EOF
echo -e "\033[34mphp整合成功 请输入ip浏览器访问 如果访问不了请观点防火墙和selinux\033[0m"
fi
  • tcpping.py(python脚本)
#!/usr/bin/env python
#coding:utf-8
#filename:tcpping.py
import socket
import time
import sys
import os NORMAL = 0
ERROR = 1
TIMEOUT = 1 def ping(ip, port, timeout=TIMEOUT):
try:
cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = (str(ip), int(port))
cs.settimeout(timeout)
status = cs.connect_ex((address))
if status == 0:
return NORMAL
else:
return ERROR
except:
return ERROR if len(sys.argv) != 3:
print ur'input example: ./tcpping.py www.baidu.com 80'
sys.exit(1) ip = sys.argv[1]
port = sys.argv[2]
while True:
try:
time1 = time.time()
if ping(ip, port) == 0:
time2 = time.time()
time3 = (time2 - time1) * 1000
print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
print "%.3fms" % (time3)
time.sleep(1)
else:
print "time out"
os.system("tcpdump -i eth0 -X -s0 -vnn host %s -w /tmp/tcpping.logs" % (ip))
time.sleep(1)
except:
break
sys.exit()
  • ssh免密钥脚本
#!/usr/bin/expect
set timeout
set username [lindex $argv ]
set password [lindex $argv ]
set hostname [lindex $argv ]
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $username@$hostname
expect {
#first connect, no public key in ~/.ssh/known_hosts
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "password:"
send "$password\r"
}
#already has public key in ~/.ssh/known_hosts
"password:" {
send "$password\r"
}
"Now try logging into the machine" {
#it has authorized, do nothing!
}
}
expect eof
#chmod auto_ssh.sh
#然后执行下述命令即可。
#./auto_ssh.sh root 192.168.10.162
  • nginx日志切割脚本
#!/bin/bash
# auto mv nginx log shell
# by zhangan
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
S_LOG="/usr/local/nginx/logs/access.log"
date="`date +%Y-%m-%d`"
F_files="`date -d "- hours" +%H_access.log`"
D_LOG="/usr/local/nginx/logs/$date"
echo -e "\033[32m please wait start cut shell scripts....\033[1m"
sleep
if [ ! -d $D_LOG ];then
mkdir -p $D_LOG
fi
cd $D_LOG
mv $S_LOG $date-$F_files
kill -USR1 `cat /usr/local/nginx/nginx.pid`
tar -czf $date-$F_files.tar.gz $date-$F_files;rm -rf $date-$F_files
echo "------------------------------------"
echo "then nginx log cutting successfully"
echo -e "\033[32myou can access $D_LOG/access.log files.\033[0m"
find . -name '*.gz' -type f -mtime + -exec rm -f {} \; #添加到任务计划里面 一个小时执行一次
  • mysql主从配置安装
#!/bin/bash
#auto install LAMP
#by csy --
/etc/init.d/iptables stop
setenforce
M_FILES=mysql-5.6..tar.gz
M_FILES_DIR=mysql-5.6.
M_URL=http://mirrors.linuxeye.com/lnmp/src/
M_PREFIX=/usr/local/mysql/
/etc/init.d/iptables stop
setenforce
yum -y install gcc* wget openssl openssl-devel
yum -y install make gcc-c++ bison ncurses ncurses-devel
wget -c $M_URL/$M_FILES && tar -zxvf $M_FILES && cd $M_FILES_DIR && cmake ./ \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE= \
-DWITH_INNOBASE_STORAGE_ENGINE= \
-DWITH_MEMORY_STORAGE_ENGINE= \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_TCP_PORT= \
-DENABLED_LOCAL_INFILE= \
-DWITH_PARTITION_STORAGE_ENGINE= \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
if [ $? -eq ];then
make -j8 && make install
groupadd mysql
useradd -g mysql -s /sbin/noghion mysql
cd $M_PREFIX
chown -R mysql.mysql .
scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --user=mysql
chown -R mysql.mysql ./data
cp support-files/mysql.server /etc/init.d/mysqld
bin/mysqld_safe --user=mysql &
ln -s $M_PREFIX/bin/mysql /usr/bin/mysql
chmod +x /etc/init.d/mysqld
rm -rf /etc/my.cnf
cat > /etc/my.cnf <<EOF
[client]
port =
default-character-set=utf8
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
user=mysql
character-set-server=utf8
symbolic-links=
open_files_limit =
max_connections=
skip-name-resolve
#skip-grant-tables
slow_query_log = 'on'
long_query_time=
slow_query_log_file=/usr/local/mysql/mysql-slow.log
tmp_table_size=256M
key_buffer_size=512M
read_buffer_size=32M
sort_buffer_size=32M
query_cache_limit=1M
query_cache_size=16M
server-id =
log-bin=mysql-bin
log-slave-updates
binlog_cache_size = 4M
binlog_format = mixed
max_binlog_cache_size = 1G
max_binlog_size = 1G
auto_increment_offset=
auto_increment_increment=
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
replicate-do-db =all
EOF
/etc/init.d/mysqld restart
chkconfig --add mysqld
chkconfig mysqld on
echo -e "\n\033[32m-----------------------------------------------\033[0m"
echo -e "\033[32mThe $M_FILES_DIR Server Install Success !\033[0m"
else
echo -e "\033[32mThe $M_FILES_DIR Make or Make install ERROR,Please Check......"
exit
fi function MYSQL_CONFIG(){ #Master Config Mysql
mysql -e "grant replication slave on *.* to 'tongbu'@'%' identified by '123456';"
MASTER_FILE=`mysql -e "show master status;"|tail -|awk '{print $1}'`
MASTER_POS=`mysql -e "show master status;"|tail -|awk '{print $2}'`
MASTER_IPADDR=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f2`
read -p "Please Enter INput Slave IP Address": SLAVE_IPADDR #Slave Config Mysql
ssh -l root $SLAVE_IPADDR "sed -i 's#server-id = 1#server-id = 2#g' /etc/my.cnf"
ssh -l root $SLAVE_IPADDR "/etc/init.d/mysqld restart"
ssh -l root $SLAVE_IPADDR "mysql -e \"change master to master_host='$MASTER_IPADDR',master_user='tongbu',master_password='123456',master_log_file='$MASTER_FILE',master_log_pos=$MASTER_POS;\""
ssh -l root $SLAVE_IPADDR "mysql -e \"slave start;\""
ssh -l root $SLAVE_IPADDR "mysql -e \"show slave status\G;\""
} read -p "Please ensure your Server is Master?yes or no": INPUT
if [ $INPUT == "y" -o $INPUT == "yes" ];then
MYSQL_CONFIG
else
exit
fi
  • mysqldump_backmysql脚本
#!/bin/bash
#auto_backmysql databases
#by zhangan
sql_dump="/usr/local/mysql/bin/mysqldump"
sql_host="localhost"
sql_user="root"
sql_password=""
sql_db="zhangan"
sql_dir="/data/backup/mysql/"
sql_file=`date -d "-1day" +%Y-%m-%d.sql`
echo -e "\033[33m========================================\033[1m"
if [ $UID -eq ];then
echo "user root is ok please wait"
else
echo "not use root is error "
exit
fi
echo "========================================"
sleep
if [ ! -d $sql_dir ];then
mkdir -p $sql_dir
fi
##########################################
$sql_dump -h$sql_host -u$sql_user -p$sql_password $sql_db > $sql_dir/$sql_file
if [ $? -eq ];then
echo " mysql backup ok "
echo "backup url $sql_dir"
else
echo -e "\033[35m mysql backup 失败 \033[0m"
fi
echo -e "\033[33m -------------------------------------- \033[1m"
cd $sql_dir;tar -czf $sql_file.tar.gz $sql_file;rm -rf $sql_file;find . -mtime + -exec rm -rf {} \;
echo -e "\033[33m done \033[0m "
#auto add crontab
grep "auto_backmysql.sh" /var/spool/cron/root >>/dev/null
if [ $? -ne ];then
echo "0 0 * * * /bin/bash /data/sh/auto_backmysql.sh >/tmp/mysql.log 2>&1" >>/var/spool/cron/root
/etc/init.d/crond restart
chkconfig crond on
fi
  • innobackupex_mysqlbackup脚本
#!bin/bash
#by zhanga
databases='zhangan'
user='root'
password=''
backmysql_dir="/data/backup/mysql"
log=full_`date -d "yesterday" +%Y%m%d`.log
gz=zhangan_`date -d "yesterday" +%Y%m%d`.tar.gz
if [ ! -d $backmysql_dir ];then
mkdir -p $backmysql_dir
fi
innobackupex --user=$user --password=$password --defaults-file=/etc/my.cnf --databases=zhangan --stream=tar $backmysql_dir >$backmysql_dir/$log| gzip >$backmysql_dir/$gz if [ $? -eq ];then
echo "ok"
else
echo "no"
fi
cd $backmysql_dir;find . -mtime + -exec rm -rf {} \; #放在任务计划里面每天23:30执行
  • innobackupex_mysqlbackup_zeng_quan脚本(周7全量,其余天增量)
#!bin/bash
#by zhanga
databases='zhangan'
user='root'
password=''
backmysql_dir="/data/backup/mysql"
log=full_`date +%Y%m%d`.log
gz=zhangan_`date +%Y%m%d`.tar.gz
gz2=zhangan_`date -d "yesterday" +%Y%m%d`.tar.gz
WEEK=`date +%u`
if [ ! -d $backmysql_dir ];then
mkdir -p $backmysql_dir
fi
#EXEC Full_Backup Function Command
Full_Backup()
{
if [ $WEEK -eq ];then
innobackupex --user=root --password= --defaults-file=/etc/my.cnf --databases=zhangan --stream=tar $backmysql_dir >$backmysql_dir/$log | gzip >$backmysql_dir/$gz
if [ $? -eq ];then
echo "ok"
else
echo "no" fi
fi
}
#Add_Backup Files System
Add_Backup()
{
if [ $WEEK -ne "" ];then
tar -zxf $backmysql_dir/$tar.tar.gz;innobackupex --user=root --password= --defaults-file=/etc/my.cnf --databases=zhangan --incremental --stream=tar $backmysql_dir --incremental-basedir=/data/backup/mysql/$gz2 | gzip >$backmysql_dir/$gz
if [ $? -eq ];then
echo "ok"
else
echo "no" fi
fi
}
Full_Backup;Add_Backup cd $backmysql_dir;find . -mtime + -exec rm -rf {} \; #脚本不完善 必须要先执行一个完整的前一天备份 才能增量使用这个脚本 测试脚本
  • innobackupex_数据库恢复脚本
#!/bin/bash
service mysqld stop
mv /var/lib/mysql /var/lib/mysqlbackup
mkdir -p /var/lib/mysql
cd /home/backup
tar -izxvf full--.tar.gz -C /home/full
cd /home/full
innobackupex --user=root --password= --apply-log /home/full
innobackupex --defaults-file=/etc/my.cnf --user=root --password= --copy-back /home/full
chown -R mysql.mysql /var/lib/mysql
service mysqld start
  • 备份系统文件目录(增全)
#!/bin/sh
#Automatic Backup Linux System Files
#Define Variable
DB_DIR=/usr/local/mysql/data/
BACK_DIR=/data/backup/mysql/
WEEK=`date +%u`
FILES=`date +%Y%m%d`_mysql_backup.tgz
CODE=$?
#Determine Whether the Target Directory Exists
if
[ ! -d $BACK_DIR ];then
mkdir -p $BACK_DIR/
echo "This $BACK_DIR Created Successfully !"
fi
#EXEC Full_Backup Function Command
Full_Backup()
{
if
[ "$WEEK" -eq "" ];then
rm -rf $BACK_DIR/snapshot
cd $BACK_DIR ;tar -g $BACK_DIR/snapshot -czvf $FILES $DB_DIR
[ "$CODE" == "" ]&&echo -e "--------------------------------------------\nThese Full_Backup System Files Backup Successfully !"
fi
}
#Perform incremental BACKUP Function Command
Add_Backup()
{
if
[ $WEEK -ne "" ];then
cd $BACK_DIR ;tar -g $BACK_DIR/snapshot -czvf $FILES $DB_DIR
[ "$CODE" == "" ]&&echo -e "-------------------------------------------\nThese Add_Backup System Files Backup Successfully !"
fi
}
Full_Backup;Add_Backup
cd $BACK_DIR
find . -mtime + -exec rm -rf {} \;
  • docker一键安装容器启动脚本,使用pipework脚本来配置容器IP,能够实现容器的批量管理,此脚本适用于CentOS6.x系统
#!/bin/bash
#auto install docker and Create VM
#by wugk --
#Define PATH Varablies
IPADDR=`ifconfig |grep "Bcast"|awk '{print $2}'|cut -d: -f2|grep "192.168"|head -`
GATEWAY=`route -n|grep "UG"|awk '{print $2}'|grep "192.168"|head -`
DOCKER_IPADDR=$
IPADDR_NET=`ifconfig |grep "Bcast"|awk '{print $2}'|cut -d: -f2|grep "192.168"|head -|awk -F. '{print $1"."$2"."$3".""xxx"}'` NETWORK=(
HWADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $1}'`
IPADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $2}'`
NETMASK=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $3}'`
GATEWAY=`route -n|grep "UG"|awk '{print $2}'`
) if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" ];then echo -e "\033[32m---------------------------------\033[0m"
echo -e "\033[32mPlease exec $0 IPADDR CPU(C) MEM(G) DISK(G),example $0 $IPADDR_NET 16 32 50\033[0m"
exit
fi CPU=`expr $ - `
if [ ! -e /usr/bin/bc ];then
yum install bc -y >>/dev/null >&
fi
MEM_F=`echo $ \* |bc`
MEM=`printf "%.0f\n" $MEM_F`
DISK=$
USER=$
REMARK=$ ping $DOCKER_IPADDR -c >>/dev/null >& if [ $? -eq ];then echo -e "\033[32m---------------------------------\033[0m"
echo -e "\033[32mThe IP address to be used,Please change other IP,exit.\033[0m"
exit
fi if [ ! -e /etc/init.d/docker ];then
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install lxc libcgroup device-mapper-event-libs -y
yum install docker-io -y
yum install device-mapper* -y
/etc/init.d/docker start
if [ $? -ne ];then
echo "Docker install error ,please check."
exit
fi
fi cd /etc/sysconfig/network-scripts/
mkdir -p /data/backup/`date +%Y%m%d-%H%M`
yes|cp ifcfg-eth* /data/backup/`date +%Y%m%d-%H%M`/
if
[ -e /etc/sysconfig/network-scripts/ifcfg-br0 ];then
echo
else
cat >ifcfg-eth0 <<EOF
DEVICE=eth0
BOOTPROTO=none
${NETWORK[]}
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
BRIDGE="br0"
${NETWORK[]}
${NETWORK[]}
${NETWORK[]}
USERCTL=no
EOF
cat >ifcfg-br0 <<EOF
DEVICE="br0"
BOOTPROTO=none
${NETWORK[]}
IPV6INIT=no
NM_CONTROLLED=no
ONBOOT=yes
TYPE="Bridge"
${NETWORK[]}
${NETWORK[]}
${NETWORK[]}
USERCTL=no
nameserver 202.101.172.46
EOF /etc/init.d/network restart fi echo 'Your can restart Ethernet Service: /etc/init.d/network restart !'
echo '---------------------------------------------------------' cd -
#######create docker container
service docker status >>/dev/null
if [ $? -ne ];then
/etc/init.d/docker restart
fi NAME="Docker$$_`echo $DOCKER_IPADDR|awk -F"." '{print $(NF-1)"_"$NF}'`"
IMAGES=`docker images|grep -v "REPOSITORY"|grep -v "none"|head -|awk '{print $1}'` (这里匹配的是docker容器里面的第一个镜像)
CID=$(docker run -itd --cpuset-cpus=-$CPU -m ${MEM}m --net=none --name=$NAME $IMAGES /bin/bash) if [ -z $IMAGES ];then
echo "Plesae Download Docker Centos Images,you can to be use docker search centos,and docker pull centos6.5-ssh,exit"
exit
fi if [ ! -f /usr/local/bin/pipework ];then
yum install wget unzip zip -y
wget https://github.com/jpetazzo/pipework/archive/master.zip
unzip master
cp pipework-master/pipework /usr/local/bin/
chmod +x /usr/local/bin/pipework
rm -rf master
fi ip netns >>/dev/null
if [ $? -ne ];then
rpm -e iproute --nodeps
rpm -ivh https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm
fi
pipework br0 $NAME $DOCKER_IPADDR/@$IPADDR docker ps -a |grep "$NAME" DEV=$(basename $(echo /dev/mapper/docker-*-$CID))
dmsetup table $DEV | sed "s/0 [0-9]* thin/0 $((${DISK}*1024*1024*1024/512)) thin/" | dmsetup load $DEV
dmsetup resume $DEV
resize2fs /dev/mapper/$DEV
docker start $CID
docker logs $CID LIST="docker_vmlist.csv"
if [ ! -e $LIST ];then
echo "编号,容器ID,容器名称,CPU,内存,硬盘,容器IP,宿主机IP,使用人,备注" >$LIST
fi
###################
NUM=`cat docker_vmlist.csv |grep -v CPU|tail -|awk -F, '{print $1}'`
if [[ $NUM -eq "" ]];then
NUM=""
else
NUM=`expr $NUM + `
fi
##################
echo -e "\033[32mCreate virtual client Successfully.\n$NUM `echo $CID|cut -b 1-12` $NAME $2C ${MEM}M ${DISK}G $DOCKER_IPADDR $IPADDR $USER $REMARK\033[0m"
if [ -z $USER ];then
USER="NULL"
REMARK="NULL"
fi
echo $NUM,`echo $CID|cut -b -`,$NAME,${}C,${MEM}M,${DISK}G,$DOCKER_IPADDR,$IPADDR,$USER,$REMARK >>$LIST
rm -rf docker_vmlist_*
iconv -c -f utf- -t gb2312 docker_vmlist.csv -o docker_vmlist_`date +%H%M`.csv
  • 使用pipework脚本来配置容器IP,能够实现容器的批量管理,此脚本适用于CentOS7.x系统
#!/bin/bash
#auto install docker and Create VM
#by
#Define PATH Varablies
IPADDR=`ifconfig|grep -E "\<inet\>"|awk '{print $2}'|grep "192.168"|head -`
GATEWAY=`route -n|grep "UG"|awk '{print $2}'|grep "192.168"|head -`
IPADDR_NET=`ifconfig|grep -E "\<inet\>"|awk '{print $2}'|grep "192.168"|head -|awk -F. '{print $1"."$2"."$3"."}'`
LIST="/root/docker_vmlist.csv"
if [ ! -f /usr/sbin/ifconfig ];then
yum install net-tools* -y
fi
for i in `seq `;do ping -c ${IPADDR_NET}${i} ;[ $? -ne ]&& DOCKER_IPADDR="${IPADDR_NET}${i}" &&break;done >>/dev/null >&
echo "##################"
echo -e "Dynamic get docker IP,The Docker IP address\n\n$DOCKER_IPADDR"
NETWORK=(
HWADDR=`ifconfig eth0|grep ether|awk '{print $2}'`
IPADDR=`ifconfig eth0|grep -E "\<inet\>"|awk '{print $2}'`
NETMASK=`ifconfig eth0|grep -E "\<inet\>"|awk '{print $4}'`
GATEWAY=`route -n|grep "UG"|awk '{print $2}'`
)
if [ -z "$1" -o -z "$2" ];then
echo -e "\033[32m---------------------------------\033[0m"
echo -e "\033[32mPlease exec $0 CPU(C) MEM(G),example $0 4 8\033[0m"
exit
fi
#CPU=`expr $ - `
if [ ! -e /usr/bin/bc ];then
yum install bc -y >>/dev/null >&
fi
CPU_ALL=`cat /proc/cpuinfo |grep processor|wc -l`
if [ ! -f $LIST ];then
CPU_COUNT=$
CPU_1=""
CPU1=`expr $CPU_1 + `
CPU2=`expr $CPU1 + $CPU_COUNT - `
if [ $CPU2 -gt $CPU_ALL ];then
echo -e "\033[32mThe System CPU count is $CPU_ALL,not more than it.\033[0m"
exit
fi
else
CPU_COUNT=$
CPU_1=`cat $LIST|tail -|awk -F"," '{print $4}'|awk -F"-" '{print $2}'`
CPU1=`expr $CPU_1 + `
CPU2=`expr $CPU1 + $CPU_COUNT - `
if [ $CPU2 -gt $CPU_ALL ];then
echo -e "\033[32mThe System CPU count is $CPU_ALL,not more than it.\033[0m"
exit
fi
fi
MEM_F=`echo $ \* |bc`
MEM=`printf "%.0f\n" $MEM_F`
DISK=
USER=$
REMARK=$
ping $DOCKER_IPADDR -c >>/dev/null >&
if [ $? -eq ];then
echo -e "\033[32m---------------------------------\033[0m"
echo -e "\033[32mThe IP address to be used,Please change other IP,exit.\033[0m"
exit
fi
if [ ! -e /usr/bin/docker ];then
yum install docker* device-mapper* lxc -y
mkdir -p /export/docker/
cd /var/lib/ ;rm -rf docker ;ln -s /export/docker/ .
mkdir -p /var/lib/docker/devicemapper/devicemapper
dd if=/dev/zero of=/var/lib/docker/devicemapper/devicemapper/data bs=1G count= seek=
service docker start
if [ $? -ne ];then
echo "Docker install error ,please check."
exit
fi
fi cd /etc/sysconfig/network-scripts/
mkdir -p /data/backup/`date +%Y%m%d-%H%M`
yes|cp ifcfg-eth* /data/backup/`date +%Y%m%d-%H%M`/
if
[ -e /etc/sysconfig/network-scripts/ifcfg-br0 ];then
echo
else
cat >ifcfg-eth0<<EOF
DEVICE=eth0
BOOTPROTO=none
${NETWORK[]}
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Ethernet
BRIDGE="br0"
${NETWORK[]}
${NETWORK[]}
${NETWORK[]}
USERCTL=no
EOF
cat >ifcfg-br0 <<EOF
DEVICE="br0"
BOOTPROTO=none
${NETWORK[]}
IPV6INIT=no
NM_CONTROLLED=no
ONBOOT=yes
TYPE="Bridge"
${NETWORK[]}
${NETWORK[]}
${NETWORK[]}
USERCTL=no
EOF
/etc/init.d/network restart fi
echo 'Your can restart Ethernet Service: /etc/init.d/network restart !'
echo '---------------------------------------------------------' cd -
#######create docker container
service docker status >>/dev/null
if [ $? -ne ];then
service docker restart
fi NAME="Docker_`echo $DOCKER_IPADDR|awk -F"." '{print $(NF-1)"_"$NF}'`"
IMAGES=`docker images|grep -v "REPOSITORY"|grep -v "none"|grep "centos"|head -|awk '{print $1}'`
if [ -z $IMAGES ];then
echo "Plesae Download Docker Centos Images,you can to be use docker search centos,and docker pull centos6.5-ssh,exit 0"
if [ ! -f jfedu_centos68.tar ];then
echo "Please upload jfedu_centos68.tar for docker server."
exit
fi
cat jfedu_centos68.tar|docker import - jfedu_centos6.
fi
IMAGES=`docker images|grep -v "REPOSITORY"|grep -v "none"|grep "centos"|head -|awk '{print $1}'`
CID=$(docker run -itd --privileged --cpuset-cpus=${CPU1}-${CPU2} -m ${MEM}m --net=none --name=$NAME $IMAGES /bin/bash)
echo $CID
docker ps -a |grep "$NAME"
pipework br0 $NAME $DOCKER_IPADDR/@$GATEWAY
docker exec $NAME /etc/init.d/sshd start
if [ ! -e $LIST ];then
echo "编号,容器ID,容器名称,CPU,内存,硬盘,容器IP,宿主机IP,使用人,备注" >$LIST
fi
###################
NUM=`cat $LIST |grep -v CPU|tail -|awk -F, '{print $1}'`
if [[ $NUM -eq "" ]];then
NUM=""
else
NUM=`expr $NUM + `
fi
##################
echo -e "\033[32mCreate virtual client Successfully.\n$NUM `echo $CID|cut -b 1-12`,$NAME,$CPU1-$CPU2,${MEM}M,${DISK}G,$DOCKER_IPADDR,$IPADDR,$USER,$REMARK\033[0m"
if [ -z $USER ];then
USER="NULL"
REMARK="NULL"
fi
echo $NUM,`echo $CID|cut -b -`,$NAME,$CPU1-$CPU2,${MEM}M,${DISK}G,$DOCKER_IPADDR,$IPADDR,$USER,$REMARK >>$LIST
rm -rf /root/docker_vmlist_*
iconv -c -f utf- -t gb2312 $LIST -o /root/docker_vmlist_`date +%H%M`.csv
  • docker容器重启自动修改ip脚本
#!/bin/bash
DOCKER_ID="$1"
DOCKER_NAME=`docker ps -a |grep -i "$DOCKER_ID" |awk '{print $1}'`
DOCKER_IPADDR=`docker ps -a |grep -i "$DOCKER_ID" |awk '{print $NF}'|awk -F"_" '{print "192.168."$(NF-1)"."$NF}'`
IPADDR=`ifconfig |grep "Bcast"|awk '{print $2}'|cut -d: -f2|grep "192.168"|head -` if [ -z $ ];then
echo -e "\033[32m---------------------------\033[0m"
echo -e "\033[32mPlease Input You want Start $0 DOCKER_ID ,example $0 651cf1d11192\033[0m"
exit
fi docker start $DOCKER_ID
pipework br0 $DOCKER_NAME $DOCKER_IPADDR/@$IPADDR
echo -e "\033[32mDoker Name $DOCKER_NAME Started,IPaddr:$DOCKER_IPADDR\033[0m"
#sh auto_docker_mgr.sh +容器的id (这里是容器的id 不是名字 记住这个脚本是对另一个脚本生成的容器才有用)
  • 批量新增用户脚本
#!/bin/bash
#请写好批量用户的列表user_list,一行一个用户
user_list="$1"
init_passwd="Ericss0n"
ssh_state=
if [ -z $]
then
echo "请写好批量用户的列表作为第一个参数 "
fi
function add_user {
if [ -n "$user_list" ]
then
echo "AllowUsers" >>/etc/ssh/sshd_config
for user in `cat "$user_list"`
do
id $user > /dev/null >&
if [ $? -ne ]
then
useradd "$user"
echo "$user:$init_passwd" | chpasswd
echo "[INFO] Added user $user success."
sed -i "/AllowUsers/ s/$/ $user/" /etc/ssh/sshd_config
echo "[INFO] Add user \"$user\" to ssh allow list."
ssh_state=
else
echo "[WARN] Added user \"$user\" failed, already exists."
fi
done
if [$ssh_state -eq ]
then
service sshd restart
fi
fi
}
add_user
  • 监控日志是否刷新脚本
#!/usr/bin/sh
#第一种监控日志刷新时间
cd /opt/ericsson/csp/logs
logupdate=`grep 'The HTTP headers' tsc-tem.log|tail -|awk '{print $2}'|cut -d. -f1` #取出日志里面最后一行得时间
currtime=`date +'%Y-%m-%d %H:%M:%S'` #当前系统时间
systime=`date -d "-5 minute" +'%H:%M:%S'` #当前系统5分钟前得时间
chkflag=`echo "$logupdate $systime"|awk '{if($1 < $2)print "1";else print "0"}'` #这里判断日志里面得时间与当前时间得大小
if [ $chkflag -eq ]
then
echo "log $logupdate sys $systime"
pronum=`ps -ef|grep apache-tomcat-tem|grep java|grep -v grep|awk '{print $2}'|wc -l` #取出进程得进程数量
if [ $pronum -ge ]
then
proid=`ps -ef|grep apache-tomcat-tem|grep java|grep -v grep|awk '{print $2}'` #取出进程得id
jstack -l $proid > ${proid}jstack.txt #导堆栈
kill - $proid
echo "log $logupdate sys $systime $proid"
sleep
fi
echo "$currtime log $logupdate sys $systime $proid" >> /home/tscuser/apache-tomcat-tem.txt
/opt/ericsson/csp/app/tsc/apache-tomcat-tem/bin/startup.sh #重启
fi #,,,,, * * * * /home/tscuser/chktomcattemtostart.sh >/dev/null >& --------------------------------------------------------------------------------- #!/bin/bash
#第二种监控日志文件大小
cd /opt/ericsson/csp/logs
file='tsc-tem.log'
filesize=`ls -trl $file|awk '{print $5}'`
echo $filesize
sleep
filesize1=`ls -trl $file|awk '{print $5}'`
echo $filesize1
if [ $filesize1 -le $filesize ];then
pronum=`ps -ef|grep apache-tomcat-tem|grep java|grep -v grep|awk '{print $2}'|wc -l` #取出进程得进程数量
if [ $pronum -ge ]
then
proid=`ps -ef|grep apache-tomcat-tem|grep java|grep -v grep|awk '{print $2}'` #取出进程得id
jstack -l $proid > ${proid}jstack.txt #导堆栈
kill - $proid
echo "log $logupdate sys $systime $proid"
sleep
fi
echo "$currtime log $logupdate sys $systime $proid" >> /home/tscuser/apache-tomcat-tem.txt
/opt/ericsson/csp/app/tsc/apache-tomcat-tem/bin/startup.sh #重启
fi
  • 磁盘监控脚本
#!/bin/bash
ip="192.168.1.13"
df1=""
df2=`df -h |grep "/app$"|awk '{print $4}'|sed 's/%/ /'`
if [[ $df2 -ge $df1 ]];then
echo "turn"
else
echo "false"
fi
  • system_监控脚本
#!/bin/bash
while :;do
day=`date +%Y%m%d-%w`
file="/app/sinova/logs/system/sysinfo$day"
if [ -f $file ]; then
echo "ok">/dev/null
else
echo " date load cpu mem swap file_system file_app java_num gen_usage app_usage |tcp_est">>$file
echo " 1 5 15 user system idle free free ">>$file
echo "">>$file
fi
time=`date +%m%d\|%H:%M:%S`
load=`uptime |grep load|sed s/\,//g| awk '{printf "%10s| %6s |%6s", $(NF-2),$(NF-1),$(NF)}'`
cpu=`vmstat | sed -n '$p' |awk '{printf "%8s |%6s |%9s", $13,$14,$15}'`
mem=`free -g | grep 'buffers/cache' | awk '{print $4}'`
tcp_est=$(netstat -an|awk '/^tcp/ && /ESTABLISHED/{t++}END{print t}')
swap=`free -g | grep 'Swap' | awk '{print $4}'`
file_sys=`/usr/sbin/lsof | wc -l`
file_app=`/usr/sbin/lsof | grep -v grep | grep nginx | wc -l`
app_thread=`ps -ef H | grep nginx | grep -v grep | wc -l`
gen_usage=`df -h | grep /$ | awk '{print $4}'`
app_usage=`df -h |awk '$NF~/^\/app$/{print $4}'`
echo "$time|$load|$cpu|$mem|$swap|$file_sys|$file_app|$app_thread|$gen_usage|$app_usage|$tcp_est" >>$file
sleep
done
  • 同步备份日志脚本
#!/bin/bash
DATE=$(date +%Y%m%d)
SDIR="/app/sinova/logs/cache"
SIP="$(/sbin/ifconfig |awk 'NR==2{split($2,a,":");print a[2]}')"
DIP="192.168.1.11"
#DDIR="/data/sms2.0/${DIP}/app/sinova/logs/cache"
DDIR="${SIP}_cachelog"
LOGDIR="/app/sinova/logs/sync"
LOG="${LOGDIR}/backup_cachelog_${DATE}.log"
INFOLOGS="$(find ${SDIR} -type f -name 'info.log*gz')"
ERRLOGS="$(find ${SDIR} -type f -name 'error.log*gz')"
for log in ${INFOLOGS} ${ERRLOGS};do
rsync -avz ${log} ${DIP}:${DDIR} >> ${LOG}
done
find ${SDIR} -type f -name '*log*gz' -mtime + -exec rm -f {} \;
find ${LOGDIR} -type f -name '*log' -mtime + -exec rm -f {} \;
  • ftp同步脚本
#!/bin/bash
FILE_PATH="/app/sinova/logs/www/marketInterface3.1/"
S_DIR="/disk/ftp/YH_FTP/sign/10.20.34.54"
R_IP="192.168.1.7"
USRNAME="sign"
PASSWD="&SF#SF78SS"
DATE=`date -d '1 day ago' +%Y%m%d`
rftp()
{
ftp -n<<EOF
open $R_IP
user $USRNAME $PASSWD
bin
prom off
cd $S_DIR
lcd $FILE_PATH
mput signin*$DATE*.log
close
bye
EOF
}
rftp
  • rsync同步目录脚本
#!/bin/bash
DATE=$(date +%Y%m%d)
DIP1=192.168.1.2
DIP2=192.168.1.3
SDIR="/app/qymh/tomcatNavMag/navfile/ST"
DDIR11="/app/sinova/applications/mobileService/templates/sitemap"
DDIR12="/app/sinova/applications/trunk/mobileService_trunk/templates/sitemap"
DDIR2="/app/client/webapps/clientT/templates/sitemap"
LOGDIR="/app/sinova/logs/sync"
[ ! -d ${LOGDIR} ] && mkdir -p ${LOGDIR}
ssh sinova@${DIP1} "cd ${DDIR11} && rm -fr android4 iphone4"
ssh sinova@${DIP1} "cd ${DDIR12} && rm -fr android4 iphone4"
ssh client@${DIP2} "cd ${DDIR2} && rm -fr android4 iphone4"
rsync -avz ${SDIR}/{android4,iphone4} sinova@${DIP1}:${DDIR11} > ${LOGDIR}/${DIP1}_nav_${DATE}.log
rsync -avz ${SDIR}/{android4,iphone4} sinova@${DIP1}:${DDIR12} >> ${LOGDIR}/${DIP1}_nav_${DATE}.log
rsync -avz ${SDIR}/{android4,iphone4} client@${DIP2}:${DDIR2} > ${LOGDIR}/${DIP2}_nav_${DATE}.log
ssh sinova@${DIP1} "cd ${DDIR12} && find -type f -name "*.json" -exec sed -i "s/mobileService-test/mobileService-test1/g" {} \;"
ssh client@${DIP2} "cd ${DDIR2} && find -type f -name "*.json" -exec sed -i "s/mobileService-test/clientT/g" {} \;"
find ${LOGDIR} -type f -name '*.log' -mtime + -exec rm -f {} \;
  • rsync同步目录脚本1
#!/bin/bash
DATE=$(date +%Y%m%d)
SDIR="/app/weblogic/bea/applications/navfile4/WO/client"
DIP="$(echo 192.168.4.{11..14})"
DDIR="/app/sinova/applications/clientNavigaiton"
LOGDIR="/app/weblogic/logs/wo"
TMPDIR="/tmp/initNavigation"
[ ! -d ${LOGDIR} ]&&mkdir -p ${LOGDIR}
[ ! -d ${TMPDIR} ]&&mkdir -p ${TMPDIR}
for ip in ${DIP};do
echo -e "\033[32mRemove old data on ${ip}...\033[0m"
ssh sinova@${ip} "cd ${DDIR} && rm -fr *"
echo -e "\033[32mTransfer new data to ${ip}...\033[0m"
rsync -avz ${SDIR}/ sinova@${ip}:${DDIR} > ${LOGDIR}/${ip}_wo_client_${DATE}.log
done
  • tcp连接数
#!/bin/bash
DATE=$(date +%Y%m%d)
TIME=$(date +%Y'-'%m'-'%d'_'%H':'%M':'%S)
LOGDIR="/app/sinova/logs/tcp_connection/"
LOGFILE="${LOGDIR}tcp_connect_${DATE}.log"
[ ! -d ${LOGDIR} ] && mkdir ${LOGDIR}
TCP_EST=$(sudo netstat -anlp|grep java|awk '/^tcp/ && /ESTABLISHED/{t++}END{print t}')
echo "Time : $TIME" >> ${LOGFILE}
echo "TCP establish connect number : $TCP_EST" >> ${LOGFILE}
echo "TCP connect details :" >> ${LOGFILE}
sudo netstat -anlp|grep java|awk -F: '/^tcp/ && /ESTABLISHED/{print $8}'|sort|uniq -c|sort -n -r -k >> ${LOGFILE}
echo "" >> ${LOGFILE}
find $LOGDIR -maxdepth -mtime -type f -name "tcp_connect_*.log" -exec rm -f {} \;
  • tcp_dump脚本
#!/bin/bash
TIME=$(date "+%Y%m%d-%H:%M:%S")
DDIR="/app/sinova/tcpdumpdata"
IP=$(/sbin/ifconfig|awk 'NR==2{split($2,a,":");print a[2]}')
NIC=bond0
NUM=
FILE="${DDIR}/${NIC}_${TIME}.cap"
[ ! -d ${DDIR} ] && mkdir -p ${DDIR}
#sudo tcpdump -i ${NIC} -s -n -vv -c ${NUM} src net 10.142.194.0/ and dst net 10.142.194.0/ -w ${FILE}
sudo tcpdump -i ${NIC} -s -n -vv -c ${NUM} src host ${IP} and dst net 10.142.194.0/ or dst host 10.20.48.11 -w ${FILE}
find ${DDIR} -type f -name '*.cap' -mtime + -exec rm -f {} \;
  • telnet端口脚本
#!/bin/bash
date=`date +%Y%m%d:%H:%M`
date1=`date +%Y%m%d`
DATE=`date +%H:%M`
IP="192.168.1.2"
PORT="22"
dir="/app/sinova/bin/nc1/$date1"
if [ ! -d $dir ];then
mkdir -p $dir
fi
for ip in $IP
do
for i in {..}
do
(sleep ;) | telnet $ip $PORT >>/app/sinova/bin/nc1/$date1/nc1-$DATE.txt
done
done
IPS=`cat /app/sinova/bin/nc1/$date1/nc1-$DATE.txt | grep -B \] | grep [-] | awk '{print $3}' | cut -d '.' -f ,,,|uniq -c`
echo "$date" >>/app/sinova/bin/nc.log
echo "$IPS--$PORT " >>/app/sinova/bin/nc.log
  • nc端口脚本
#!/bin/bash
#IP='192.168.1.2'
IP="192.168.1.12 192.168.1.11"
#PORT=''
PORT=''
DATE=`date +%F_%T`
date=`date +%Y-%d-%m`
D_LOG="/app/sinova/logs/$date"
if [ ! -d $D_LOG ];then
mkdir -p $D_LOG
fi for ip in ${IP[@]}
do
for port in ${PORT[@]}
do
a=
for i in {..}
do
nc -z -w $ip $port &>/dev/null
if [ $? -eq ]
then
let a+=1 #成功a就+
fi
done
echo "$DATE" >> $D_LOG/nc.log
echo "$ip--$port $a" >> $D_LOG/nc.log
done
done
  • gzip日志打压脚本
#!/bin/bash
DIR=/disk3/YH_DATA/DATA/service_new
IP=(`echo 192.168.1.{..}` `echo 192.168..{..}`)
for ip in ${IP[@]}
do
find $DIR/interface/${ip} -type f -name "*20160405*" |xargs gzip -
find $DIR/service/${ip} -type f -name "*20160405*" |xargs gzip -
done
  • 9*9乘法运算
#!/bin/bash
#*
#--
for ((i=;i<=;i++))
do
for ((j=;j<=i;j++))
do
echo -n " $j*$i=$[$i*$j] "
if [[ $j == $i ]];then
echo -e '\n'
fi
done
done
  • 错误日志进行监控,监控最近一小时错误日志与前一日同时段错误日志量比较,如超出100%则提示告警
#!/bin/bash

todayhour=`date -d "-1 hour" "+%Y-%m-%d %H"`
yesterdaydour=`date -d "-25 hour" "+%Y-%m-%d %H"`
yesterday=`date -d "-1 day" "+%Y-%m-%d"`
#echo $todayhour $yesterdaydour $yesterday
todayfile='/opt/ericsson/csp/logs/TC.log'
yesterdayfile='/opt/ericsson/csp/history-logs/TC-'${yesterday}'.log'
#echo $todayfile $yesterdayfile
todaynum=`grep '\[ERROR\]' $todayfile |grep "$todayhour"|wc -l`
yesterdaynum=`grep '\[ERROR\]' $yesterdayfile|grep "$yesterdaydour"|wc -l`
#如超出100%则提示告警
echo "$todaynum $yesterdaynum"|awk '{printf("%d",$1*100/$2)}'

shell学习及脚步编写的更多相关文章

  1. shell 学习之脚本编写1

    脚本要求: 执行脚本后 接受输入1,显示当前时间,并提示是否继续选择. 接受输入2,显示CPU负载,并提示是否继续选择.  接受输入3,显示剩余内存,并提示是否继续选择. 接受输入0,退出脚本. ·· ...

  2. Shell学习之Shell特性(一)

    Shell学习之Shell特性 目录 命令和文件自动补齐功能 命令历史记忆功能 history.上下键.!number.!string.!$.!! 别名功能 alias.unalias cp.~use ...

  3. [转帖][Bash Shell] Shell学习笔记

    [Bash Shell] Shell学习笔记 http://www.cnblogs.com/maybe2030/p/5022595.html  阅读目录 编译型语言 解释型语言 5.1 作为可执行程序 ...

  4. Shell 学习—AWK介绍

    Shell 学习—AWK = = = 安装awk root@kiki-desktop:~/shell# apt-get install gawk gawk-doc = = = awk 是一种程序语言. ...

  5. Shell 学习(三)

    目录 Shell 学习(三) 流程控制 1 if判断 2 case 语句 3 for循环 4 while 循环 2 read 读取控制台输入 2.1 基本语法 2.2 应用实例 3 函数 3.1 系统 ...

  6. Shell 学习(二)

    目录 Shell 学习(二) 1 设置环境变量 1.1 基本语法 1.2 实践 2 位置参数变量 2.1 介绍 2.2 基本语法 2.3 位置参数变量应用实例 3 预定义变量 3.1 基本介绍 3.2 ...

  7. Hadoop学习笔记(5) ——编写HelloWorld(2)

    Hadoop学习笔记(5) ——编写HelloWorld(2) 前面我们写了一个Hadoop程序,并让它跑起来了.但想想不对啊,Hadoop不是有两块功能么,DFS和MapReduce.没错,上一节我 ...

  8. shell学习总结之自定义函数

    shell学习总结之自定义函数 Myfun (){ echo -n "now i is $i " ! [ "$i" ] && exit ; ec ...

  9. SHELL学习笔记----IF条件判断,判断条件

    SHELL学习笔记----IF条件判断,判断条件 前言: 无论什么编程语言都离不开条件判断.SHELL也不例外.  if list then           do something here   ...

随机推荐

  1. Angular动态组件

    一.主页面: app.component.html: <button (click)="load();">动态</button> 2<div #dom ...

  2. 数据结构实验之查找六:顺序查找(SDUT 3378)

    (不知道为啥开个数组就 TLE .QAQ) #include <stdio.h> #include <stdlib.h> #include <string.h> / ...

  3. Dns的作用

    DNS(Domain Name System,域名系统),万维网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串 DNS系统:通过 ...

  4. Latex 数字加粗后变宽 Latex bold without increasing the length of the text

    Add the following code at the beginning of the article \newsavebox\CBox \def\textBF#1{\sbox\CBox{#1} ...

  5. Shell编程—企业生产案例

    Linux系统Shell编程—企业生产案例(一) 企业数据库可以说是重点保护对象啊,没有之一,数据在当今企业里就是生命线,因此今天就来说一说,如何通过shell脚本来检查或监控MYSQL数据库服务是否 ...

  6. python提取计算结果的最大最小值及其坐标

    我们在fluent当中后处理的时候,可以通过fluent本身得到某些物理量的最大值和最小值,但是我们却无法确定这些最大值和最小值的具体位置.其实我们可以将求解数据导出以后,借助python求得最大值和 ...

  7. GO获取随机数

    使用的"math/rand"包. 基本随机数 a := rand.Int() b := rand.Intn(100) //生成0-99之间的随机数 fmt.Println(a) f ...

  8. posh-git

    https://github.com/dahlbyk/posh-git#step-2-import-posh-git-from-your-powershell-profile $profile.All ...

  9. Spark(五十二):Spark Scheduler模块之DAGScheduler流程

    导入 从一个Job运行过程中来看DAGScheduler是运行在Driver端的,其工作流程如下图: 图中涉及到的词汇概念: 1. RDD——Resillient Distributed Datase ...

  10. mysql的备份与还原,安装(window)

    之前一直使用的navicat的自动的备份功能.但是在使用navicat的还原功能时,贼慢. 今天要做的是window服务器上将mysql单个数据库备份.然后还原到我的mac笔记本上. 本来以为很快的, ...