自编shell脚本合集(完善中)
1.数据库备份
#!/bin/bash user="root" psword="root" bakdir="/data/mysqlbak" Date=$(date +%F) dbnames=$(mysql -u root -proot -e "show databases" | sed 1d | egrep -v "(schema$|mysql)") #删除3天之前的备份 find $bakdir -ctime +3 -name \*.sql | xargs rm -f #备份数据库 for dbname in $dbnames do mysqldump -u$user -p$psword --databases $dbname > $bakdir/${dbname}-${Date}.sql done
2.自动安装apache php mysql (利用本地网络源)
#!/bin/bash #Date :2018-04-09 #Author :高鹏 #Function :本脚本用于centos6/7使用自带源安装httpd、mysql、php #Version :0.2 #Update :2018-04-09 grep -i "centos" /etc/redhat-release 2> /dev/null if [[ $? -ne 0 ]];then echo "NOT CentOS" && exit fi OS=$(awk -F . '{print $1}' /etc/redhat-release | grep -o [0-9] ) function package_version(){ yum list | egrep "(^httpd.x86_64|^php.x86_64|^mysql.x86_64|^mariadb.x86_64)" | awk -F "-" '{print $1}'| awk 'BEGIN{printf("pachage\t\tversion\n")} {print$1"\t"$2}' } function package_info(){ Apache=$(httpd -v 2> /dev/null | egrep -o '[0-9]\.[0-9]\.[0-9]') && echo "Apache Version : $Apache" || echo "Apache Version : uninstalled" Mysql=$(mysql -V 2> /dev/null| egrep -o '[0-9]\.[0-9]\.[0-9]') && echo "MySQL/MariaDB : $Mysql" || echo "MySQL/MariaDB :uninstalled" PHP=$(php -v 2> /dev/null | sed -n 1p | egrep -o '[0-9]\.[0-9]\.[0-9]') && echo "PHP Version : $PHP" || echo "PHP Version :uninstalled" } function install(){ [[ $OS -eq 7 ]] && yum install -y httpd php mariadb-server [[ $OS -eq 6 ]] && yum install -y httpd php mysql-server mysql mysql-devel } function start_server(){ [[ $OS -eq 7 ]] && systemctl start mariadb httpd > /dev/null [[ $OS -eq 6 ]] && service mysqld start && service httpd start } function main(){ echo "################################" echo "########## installed ###########" package_info echo "################################" package_version read -p "If install?(Y/N) :" yn case $yn in Y|y|yes|YES ) read -p "pelease input mysql psword(default:root) :" mysql_psword mysql_ps=${mysql_psword:=root} install;; N|n|no|NO ) exit;; *) echo "invalid input" && exit;; esac start_server /usr/bin/mysqladmin -u root -h localhost.localdomain password "$mysql_ps" } main && echo “install complete”
3.获取本机或远程主机信息
#!/bin/bash
#localinfo.sh #本地信息 clear #判断是否为root用户 if [[ $(id -u) -ne 0 ]];then echo "Please execute as root." exit 2 fi #获取版本号 if [[ -f /etc/redhat-release ]]; then Version=$(cat /etc/redhat-release) elif [[ -f /etc/issue ]]; then Version=$(awk '{print $1,$2}' /etc/issue) fi #获取磁盘信息 Disk_Info=$(lsblk -l | awk 'BEGIN{printf("Disk\t\tSize\n")} /disk/ {print $1"\t\t"$4}') #获取IP信息 IP_Addr=$( ETH=$(awk -F ":" 'NR>2{print $1}' /proc/net/dev)&& for eth in $ETH;do echo -e "$eth\t\t$(ifconfig $eth | awk '/inet/ {print $2}' | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")" done ) #获取端口及服务信息 Port=$( netstat -lntup | awk -F / '/LISTEN/ {print $1,$2}' | awk '{print $4"\t\t"$8}' | egrep -o '[0-9]{1,5}'$'\t\t''[a-z]*' | sort | uniq) #获取内存信息 Mem_Info=$(free -h | egrep "(Mem|Swap)" | awk '{print $1"\t\t"$2}') #获取 CPU信息 Cpu=$(grep "physical id" /proc/cpuinfo | uniq | wc -l) Core=$(grep "core id" /proc/cpuinfo | wc -l) function main(){ echo -e " ########################################### DATE : $(date "+%F %r") ############## SYSTEM INFO ################ HOSTNAME : $(hostname) VERSION : $Version ARCH : $(uname -m) ############## IP INFO ################ $IP_Addr ############## DISK INFO ################ $Disk_Info ############## CPU INFO ################ NUM OF CPU : $Cpu NUM OF CORE : $Core ############## MEM INFO ################ $Mem_Info ############## PORT SERVER ################ PORT\t\tSERVER $Port ############## END ################" } main
远程信息(调用localinfo.sh)
#!/bin/bash if [[ -z $1 ]];then echo "USE: $0 IP_ADDRESS [PORT]"&&exit else PORT=$2 PORT=${PORT:=22} ssh root@$1 -p $PORT 'bash -s' < LocalInfo.sh fi
4带颜色输出字符
#!/bin/bash #closr #Black red green yellow blue violet and bluish white function Echo_Black(){ echo -e "\033[30m${1}\033[0m" } function Echo_Red(){ echo -e "\033[31m${1}\033[0m" } function Echo_Green(){ echo -e "\033[32m${1}\033[0m" } function Echo_Yellow(){ echo -e "\033[33m${1}\033[0m" } function Echo_Blue(){ echo -e "\033[34m${1}\033[0m" } function Echo_Violet(){ echo -e "\033[35m${1}\033[0m" } function Echo_Bluish(){ echo -e "\033[36m${1}\033[0m" } function Echo_White(){ echo -e "\033[37m${1}\033[0m"