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 version ")} {print$1" "$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 Size ")} /disk/ {print $1" "$4}') #获取IP信息 IP_Addr=$( ETH=$(awk -F ":" 'NR>2{print $1}' /proc/net/dev)&& for eth in $ETH;do echo -e "$eth $(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" "$8}' | egrep -o '[0-9]{1,5}'$' ''[a-z]*' | sort | uniq) #获取内存信息 Mem_Info=$(free -h | egrep "(Mem|Swap)" | awk '{print $1" "$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 SERVER $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 "