Linux production server shell script sharing

Source: Internet
Author: User
Tags rsync

Linux production server shell script sharing

2012-6-6 86 Market Network Linux

As a Linux/unix system administrator, I often meet people asking this question: what can the shell do? PHP is so powerful, why not use PHP to get the job done? In fact, compared to the PHP development language, the shell is mainly used for database backup (SVN backup), Scheduled Tasks (crontab), Service status monitoring, FTP remote backup and so on. For these tasks, the strength of the shell is universally acknowledged, and this is one of the basic linux/unix of every system administrator. Now there's a PowerShell in Windows 2008, which is pretty good.

Below I will use my online server shell script example to let you know the shell and its purpose.

Directory
MySQL's Hot backup script
Synchronizing Servers with FTP
keepalived Monitoring Switch Script
Batch backup of SVN repository
Automatic monitoring of ADSL and dial-out number
Linux Batch Build account (updated October 11)
Test if the host in the LAN is Alive (updated October 11)

Click on the Sub-page title below to go to the corresponding Shell script page to read.
One, MySQL's hot backup script

This is one of the ways MySQL is backed up, with the following script:
#!/bin/bash
Path=/usr/local/sbin:/usr/bin:/bin

# The Directory of Backup
Backdir=/usr/mysql_backup
# The Password of MySQL
Rootpass=password

# remake The Directory of Backup
RM-RF $BACKDIR
Mkdir-p $BACKDIR

# Get The Name of the Database
Dblist= ' ls-p/var/lib/mysql grep/tr-d/'
# Stole a glance at my colleague's way of writing the script, and found that he directly knocked the dblist up by hand.
# database is a small situation is possible, if the database is more than hundred, hehe

# Backup with Database
For dbname in $DBLIST
Do
Mysqlhotcopy $dbname-u root-p $ROOTPASS $BACKDIR logger-t mysqlhotcopy
Done

Many system administrators like to use Mysqldump--opt to back up the database, because it is relatively simple, I do not repeat here.
Second, synchronize the server with FTP

Many system administrators like to use Rsync to synchronize data between two servers, but we prefer to use FTP to achieve, the benefits are:
FTP runs out of bandwidth more than rsync, so it's better suited to synchronizing databases (especially hundreds of g of data)
Very convenient to configure, especially with PUREFTPD.
The advantage of rsync is the ability to quickly and easily synchronize small files, such as a two-level directory or a three-level catalog of batch images.

Basically FTP and rsync synchronization each have advantages, look at the system administrator's choice. Here is how the shell script for FTP synchronization is described in this section:
Ftpolddate= ' Date +%y-%m-%d-d ' -60 days '
#在FTP定义60天间的日期变量, is to save 60 days of data on the FTP side, deleted late.

host=192.168.4.199
Ftp_username=db
Ftp_password=password

CD ${backdir}/${date}

Ftp-i-n-v <<!
Open ${host}
User ${ftp_username} ${ftp_password}
Bin
CD ${ftpolddate}
Mdelete *
Cd..
RmDir ${ftpolddate}
mkdir ${date}
CD ${date}
Mput *
Bye
!
Third, the keepalived monitoring switch script

The implementation function is as follows: For nginx+keepalived, write nginx monitoring script nginx_ Pid.sh, placed in the background has been monitoring the nginx process, such as the process disappears, try to restart Nginx, if it fails, immediately stop the local keepalived service, let another load balancer take over. This script details the following:
#!/bin/bash
While:
Do
Nginxpid= ' ps-c nginx--no-header wc-l '
If [$nginxpid-eq 0];then
/usr/local/nginx/sbin/nginx
Sleep 5
If [$nginxpid-eq 0];then
/etc/init.d/keepalived stop
Fi
Fi
Sleep 5
Done
Iv. batch Backup of the SVN repository

The realization of its function is very simple, mainly with the Svnadmin hotcopy, which is very powerful, I often used in the SVN version of the library transfer (by the way, the last careless operation will delete the library, it is easy to use the Svnadmin hotcopy restore it over).
For project in test project Svntest
Do
CD ${svndir}
${svnadmin} hotcopy ${project} ${backdir}/${date}/${project}--clean-logs

CD ${backdir}/${date}
Tar zcvf ${project}_svn_${date}.tar.gz ${project} >/dev/null
RM-RF ${project}

echo "Repository: ${project} backup done to ${backdir}/${date}/successful!"
>> ${logfile}
/bin/sleep 2
Done
Five, automatically monitor the ADSL and dial the shell script

The company's office ADSL love off the line, a drop of the gateway gateways will be gone. The following script is therefore written:
#!/bin/bash
While:
Do
If Route tail-l grep "0.0.0.0"
Then
&>/dev/null
Else
Adsl-stop
Adsl-start
Fi
Sleep 10
Done

Execute script method: Nuhup sh route.sh &

Note that the previous use of the nohup, so as to avoid the root user logout when the script also exits the issue of effect.
Six, Linux batch generation account script

This script is applied to the build account in the production environment and can generate hundreds of thousands of accounts with the same password. The script code is as follows:
#!/bin/bash
For name in Tom Jerry Joe Jane
Do
Useradd $name
echo Redhat passwd--stdin $name
Done

Use your own account name list to replace the fields in this code example with Tom Jerry Joe Jane. Passwords are Redhat, allowing users to change them later.
Seven, test the host in the LAN is Alive small script

This script is used to check if the host between 192.168.1.100 to 192.168.1.200 is alive. The script is as follows:
#!/bin/bash
#Checks to see if hosts 192.168.1.100-192.168.1.200 is alive
For n in {100..200}; Do
host=192.168.1. $n
PING-C2 $host &>/dev/null
If [$ = 0]; Then
echo "$host is up"
Else
echo "$host is down"
Fi
Done

※ Warm tip: Note that there must be a space between $? and =.

Summary

Some other scripts that automatically monitor MySQL status I feel relatively simple, suitable for novice learning, interested in the fuqin to cook wine Baidu Blog to visit the study. If you are interested, will be sorted out in batches to share with you. I hope you see the shell scripts and so on, can learn from the knowledge of their own helpful.

Linux production server shell script sharing

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.