Daily operations Work Shell script case

Source: Internet
Author: User
Tags openssl md5



1, list_sys_status.sh
Displays the following information used by the system:
Host name, IP address, subnet mask, gateway, DNS server IP address information


#!/bin/bash
ip= ' Ifconfig eth0 | head-2 | Tail-1 | awk ' {print $} ' | Awk-f ":" ' {print $} '
zw= ' Ifconfig eth0 | head-2 | Tail-1 | awk ' {print $} ' | Awk-f ":" ' {print $} '
gw= ' Route-n | Tail-1 | awk ' {print $} '
Hn= ' hostname '
dns= ' Head-1/etc/resolv.conf | awk ' {print $} '
Echo ' This machine IP address is ' $IP
Echo ' This subnet mask is ' $ZW
Echo ' This machine gateway is ' $GW
Echo ' This machine hostname is ' $HN
Echo ' This machine DNS is ' $DNS

2. Mysqlbak.sh Backup Database Directory script


#!/bin/bash
day= ' Date +%y%m%d '
Size= ' Du-sh/var/lib/mysql '
echo "Date: $DAY" >>/tmp/dbinfo.txt
echo "Data Size: $SIZE" >>/tmp/dbinfo.txt
Cd/opt/dbbak &>/dev/null | | Mkdir/opt/dbbak
Tar zcf/opt/dbbak/mysqlbak-${day}.tar.gz/var/lib/mysql/tmp/dbinfo.txt &>/dev/null
Rm-f/tmp/dbinfo.txt

Crontab-e
*/3 * */opt/dbbak/dbbak.sh

3, every Sunday 23:30 P.M., do a full backup of the WEBDB library on the database server
Save each backup file to the system's/mysqlbak directory
Use the system date to make the backup file name Webdb-yyyy-mm-dd.sql
Generate a new Binlog log after each full backup
Back up all current Binlog logs to the/mysqlbinlog directory


#mkdir/mysqlbak
#mkdir/mysqlbinlog
#service mysqld Start
Cd/shell
#vi webdb.sh
#!/bin/bash
day= ' Date +%f '
mysqldump-hlocalhost-uroot-p123 webdb >/mysqlbak/webdb-${day}.sql
Mysql-hlocalhost-uroot-p-E "flush Logs"
Tar zcf/mysqlbinlog.tar.gz/var/lib/mysql/mysqld-bin.0*
#chmod +x webdb.sh
#crontab-E
* * 7/shell/webdb.sh


4, very.ser.sh (check the running status of any one service)

Only check the status of service vsftpd httpd sshd Crond, any of the services in MySQL

If it is not a service in these 5, prompt the user to check the service name and exit the script
If the service is running, output "service name is running"
Start the service if the service is not running


Method 1: Use read to write the script
#!/bin/bash
Read-p "Please enter your service name:"
if [$service! = ' Crond ' a $service! = ' httpd ' a $service! = ' sshd '-a $service! = ' mysqld '-a $service! = ' vsftpd '];th En
echo "Can only check ' vsftpd,httpd,crond,mysqld,sshd"
Exit 5
Fi
Service $service Status &>/dev/null

If [$?-eq 0];thhen
echo "Service Online"
Else
Service $service Start
Fi

Or

Method 2: Use positional variables to write scripts
If [-Z $];then
echo "You mast specify a servername!"
echo "Usage: ' basename$0 ' servername"
Exit 2
Fi
if [= = = "Crond"] | | [$ = = "MySQL"] | | [= = = "Sshd"] | | [$ = = "HTTPd"] | | [$ = = "VSFTPD"];then
Service Status &>/dev/null
If [$?-eq 0];then
echo "is running"
Else
Service start
Fi
Else
echo "Usage: ' basename" server name "
echo "But only check for vsftpd httpd sshd crond mysqld" && exit2
Fi


5, pc_noline.sh
IP address of the online host in the output 192.168.1.0/24 network segment
Count the number of non-online hosts,
and save the IP address and the time that is not in the line host to the/tmp/ip.txt file.


#!/bin/bash
ip=192.168.1.
J=0
For i in ' seq 10 12 '
Do
Ping-c 3 $ip $i &>/dev/null
If [$?-eq 0];then
Echo Online hosts are: $ip $i
Else
Let J + +
echo $ip $i >>/tmp/ip.txt
Date >>/tmp/ip.txt
Fi
Done
Echo does not have a line host number of $j


6, a simple website forum test script

Use interactive input method to implement automatic login forum database, modify user password


[Email protected] scripts]# vim input.sh

#!/bin/bash

End=ucenter_members
Mysql=/home/lnmp/mysql/bin/mysql

Read-p "Enter A website directory:" WebDir
webpath=/home/webser/$webdir/config
Echo $WebPath

Read-p "Enter dbuser name:" Dbuser
Echo $dbuser

READ-SP "Enter dbuser password:" Dbpass

Read-p "Enter db name:" dbname
Echo $dbname

Read-p "Enter db tablepre:" Dbtablepre
Echo $dbtablepre

globalphp= ' grep ' tablepre* "$WebPath/config_global.php |cut-d" ' "-f8 '
ucenterphp= ' grep ' uc_dbtablepre* ' $WebPath/config_ucenter.php |cut-d '. '-f2 | Awk-f "'" ' {print '} '

if [$dbtablepre = = $Globalphp] && [$dbtablepre = = $Ucenterphp];then

Start= $dbtablepre
Pre= ' echo $Start $end '

Read-p "Enter you name:" Userset
Echo $userset

result= ' $MYsql-u$dbuser-p$dbpass $dbname-E "Select username from $Pre where username= ' $userset ' \g" |cut-d '-f2|tail -1 '
Echo $Result
if [$userset = = $Result];then
Read-p "Enter Your password:" Userpass
passnew= ' echo-n $userpass |openssl md5|cut-d '-f2 '

$MYsql-u$dbuser-p$dbpass $dbname-E "update $Pre set password= ' $passnew ' where username= ' $userset ';"
$MYsql-u$dbuser-p$dbpass $dbname-E "flush privileges;"
Else
echo "$userset isn't right user!"
Exit 1
Fi
Else
Exit 2
Fi


7, slave_status.sh (check MySQL slave structure from the database server state)
1) Whether the local database service is running
2) Ability to communicate properly with the primary database server
3) Can I use an authorized user to connect to the database server
4) Whether the SLAVE_IO process in this machine is in the Yes State
Whether the slave_sql process in this machine is in the Yes State


[Email protected] scripts]# vim test.sh

#!/bin/bash
NETSTAT-TULNP | grep:3306 >/dev/null
If [$?-eq 0];then
echo "Service is running"
Else
Service mysqld Start
Fi
Ping-c 3 192.168.1.100 &>/dev/null
If [$?-eq 0];then
echo "Network connection OK"
Else
echo "Network connection Failed"
Fi
mysql-h192.168.1.100-uroot-p123456 &>/dev/null
If [$?-eq 0];then
echo "Database connection succeeded"
Else
echo "Database connection Failed"
Fi
io= mysql-uroot-p123-e "show Slave Status\g" | grep slave_io_running | awk ' {print $ ' >/dev/null
Sql= mysql-uroot-p123-e "show Slave Status\g" | grep slave_sql_running | awk ' {print $} '/dev/null
If [Io==yes] && [Sql==yes];then
echo "IO and SQL Connection succeeded"
Else
echo "IO thread and SQL thread connection failed"
Fi



Daily operations Work Shell script case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.