Share Shell scripts on Linux production servers without regret

Source: Internet
Author: User
Tags mysql backup

As a Linux/unix system administrator, I often ask this question: What can shell do? PHP is so powerful, why not use PHP to complete the work? In fact, shell is mainly used for database backup (SVN backup), scheduled task (crontab), service status monitoring, and ftp remote backup compared with PHP development languages. For these tasks, shell is widely recognized as powerful, which is also one of the basics of every Linux/unix system administrator. Windows 2008 also shows PowerShell, which is very good.

Below I will use the shell script of my online server as an example to let everyone know about shell and its usage.

[Directory]
MySQL hot backup script
Synchronize servers with FTP
Keepalived monitoring switch script
Batch backup of SVN version Libraries
Automatic Monitoring of ADSL retries
Generate multiple accounts in Linux (updated on January 1, October 11)
Test whether the host in the LAN is alive (updated on January 1, October 11)

Click the subpage title below to go to the corresponding shell script page for reading.
I. MySQL hot backup script

This is one of the MySQL backup methods. The script is as follows:
#! /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 Database
DBLIST = 'LS-p/var/lib/mysql grep/tr-d /'
# I stole my colleague's script writing method and found that he directly typed DBLIST one by one.
# It is acceptable that there are few databases. If there are more than databases, huh?

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

Many system administrators prefer to use mysqldump-opt to back up databases. I will not repeat it here because it is relatively simple.
2. Use FTP to synchronize servers

Many system administrators prefer to use rsync to synchronize data between two servers, but we prefer to use FTP to achieve the following advantages:
FTP consumes more bandwidth than rsync, so it is more suitable for synchronizing database (especially several hundred GB of data)
Configuration is quite convenient, especially with pureftpd
The advantage of rsync is that it can quickly synchronize large amounts of small files, such as batch images under Level 2 directories or level 3 directories.

Basically, FTP and rsync have their own advantages. It depends on the system administrator's choice. The following is the shell script method for FTP synchronization:
FTPOLDDATE = 'date + % Y-% m-% d-60 days'
# The date variable for the 60-day period defined in FTP is to save the 60-day data on the FTP end and is deleted within the time limit.

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
!
Iii. Monitoring switch script for Keepalived

The following functions are implemented: For Nginx + Keepalived, compile the nginx monitoring script nginx_pid.sh and keep monitoring the nginx process in the background. If the process disappears, try to restart nginx, if it fails, stop the keepalived service of the local machine immediately and let another Server Load balancer take over. The script details are as follows:
#! /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 SVN version Libraries

It is quite simple to implement its functions. It mainly uses svnadmin hotcopy, which is very powerful. I often use it for the transfer of svn version libraries (by the way, I accidentally deleted the database by mistake last time, and it was easy to use svnadmin hotcopy to restore it ).
For PROJECT in test project svntest
Do
Cd $ {SVNDIR}
$ {SVNADMIN} hotcopy $ {PROJECT }$ {BACKDIR}/$ {DATE}/$ {PROJECT} -- clean-logs

Cd $ {BACKDIR}/$ {DATE}
Tar zcvf release variable project1__svn_1_1_date0000.tar.gz $ {PROJECT}>/dev/null
Rm-rf $ {PROJECT}

Echo "Repository: $ {PROJECT} backup done into $ {BACKDIR}/$ {DATE}/Successful! "
>>$ {LogFile}
/Bin/sleep 2
Done
5. automatically monitor shell scripts for ADSL repeat

The adsl in the company's office is loose, and the gateway will be gone once the gateway is lost. Therefore, the following script is compiled:
#! /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

Script Execution method: nuhup sh route. sh &

Note that you must use nohup to prevent the script from taking effect when the root user logs out.
6. Generate account scripts in batches in Linux

This script is used to generate accounts in the production environment. It can also generate hundreds 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

Replace the fields such as tom jerry joe jane in the sample code with the account name list you need. The password is redhat and can be changed by the user later.
7. Test the alive script on the host in the LAN

This script is used to check whether the host from 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 are 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

※Note: $? A space is required between and =.

Summary

I feel that other scripts such as automatic MySQL monitoring are relatively simple and suitable for beginners to learn. If you are interested, you can go to the Baidu blog of fuqin liquor cooking to visit and learn. If you are interested, we will sort it out in batches to share with you in the future. I hope you can read such shell scripts and learn some useful knowledge from them.

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.