Introduction to Shell Script (bash)
As is known to all, Unix is famous for its gadgets, using a number of simple gadgets to accomplish a job that would otherwise require a lot of software development, making UNIX the ideal system platform for many people.
In many gadgets, Shell script is the most basic, powerful, and most widely used. It uses a wide range, not only from system start-up, program compiling, regular operation, Internet connection, even install the entire Linux system, can use it to complete.
Because shell script uses some of the instructions that you use on weekdays, you combine them into a "program." If you have frequent instructions in certain sequences, you can combine these instructions to become another new instruction. In this way, not only can simplify and speed up the operation speed, and even can simply automatic regular execution, greatly simplifying the system management work.
*************************
Bash (GNU bourne-again shell) is the default shell of many Linux platforms, in fact, there are many traditional Unix shell, like tcsh, CSH, Ash, BSH, Ksh, and so on, SHell script is roughly similar, When you learn a shell, the rest of the shell will soon start, and most of the time, a shell script can often be used on many kinds of shells.
Here I'll explain how you use bash. In fact, when you "man bash", you can see the instructions for bash, but for many people, this is as hard as "wordless". This document, the main source for "Man bash", I add some practical daily examples to illustrate. Hope that this will allow those who have not been able to enter the door, more or less can have a concept.
Teaching examples
"Hello World" Shell Script
In traditional programming, this section describes how shell script "Hello World" is written.
*************************
#!/bin/sh
# Filename:hello
echo "Hello world!"
*************************
You should notice the first line of "#!/bin/sh". Under UNIX, all executable script, regardless of the language, starts with "#!," such as perl as "#!/usr/bin/perl", tcl/tk "#!/usr/bin/wish", and see where the script program you want to execute is located. You can also use "#!/bin/bash", "#!/bin/tcsh", and so on to specify a specific shell.
Echo is a bash's built-in instruction.
*************************
Next, execute the script hello:
There are a number of ways to execute a script.
*************************
The first: Set the permissions of the hello file to executable.
[Foxman@foxman bash]# chmod 755 Hello
Perform
[Foxman@foxman bash]#./hello
Hello World
*************************
Second: Use Bash's built-in directive "source" or ".".
[Foxman@foxman bash]# Source Hello
Hello World
Or
[Foxman@foxman bash]#. Hello
Hello World
*************************
The third: direct use of the SH/BASH/TCSH directive to execute.
[Foxman@foxman bash]# sh Hello
Hello World
Or
[Foxman@foxman bash]# Bash Hello
Hello World
*************************
Bash Execution Options
*************************
-C String: Read string to be a command.
-I: Interactive interface.
-S: Read command by STDIN.
-: Cancels the read of the next option.
-NORC: Do not read ~/.BASHRC to perform.
-noprofile: Do not read/etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile, and so on.
-rcfile FileName: Execute filename, not ~/.BASHRC
-version: Displays the version.
-quiet: Do not be a mile when starting.
-login: Make sure bash is a login shell.
-nobraceexpansion: Do not use the curly brace expansion ({} symbol to expand).
-nolineediting: Do not use ReadLine to read command columns.
-posix: Adopt POSIX 1003.2 standard.
Shell Script for automatic backup
A shell Script for automatic backup
As we mentioned earlier, we can use Shell script to match Crond to work on a regular basis. To do regular work, on UNIX, is to use the collocation of Crond.
*************************
First we'll look at how to back up the system.
To back up the system is to use some compression tools. On many Unix systems, tar and gzip are de facto data exchange standards. We can often see some tar.gz or tgz files, which are called tarball. Of course, you can also use the bzip2, zip, and so on compression tools to compress, do not have to be limited to gzip. But tar with gzip is the most common, and also the most convenient way.
To compress the data we want, make a backup, which can be combined with tar and gzip. There are many different ways, the most commonly used instruction is the following:
Tar-c File/dir ... | gzip-9 > xxxx.tar.gz
You can also do it separately:
Tar-r File/dir ...-f Xxxx.tar
Gzip-9 Xxxx.tar
Or
Tar-r File/dir ...-f Xxxx.tar
Gzip-9 xxxx.tar.gz
*************************
After you've solved the basics of file backup under Linux, let's write a script that backs up your files.
#!/bin/sh
# Filename:backup
Dirs= "/etc/var/your_directories_or_files"
backup= "/tmp/backup.tgz"
Tar-c $DIRS | gzip-9 > $BACKUP
Where dirs is the file and directory you want to back up, backup is your back-up file. Do not put/tmp in the dirs, do that, you are backing up the backup, may be your hard disk explosion.
*************************
Next Test
[Foxman@foxman bash]# chmod 755 Backup
[Foxman@foxman bash]#./backup
After the execution is complete, there will be a backup.tgz in/TMP with your important information stored. You can
gzip-dc/tmp/backup.tgz | Tar-vt
Or
Tar vtfz/tmp/backup.tgz
Take a look inside the file list.
To undo this, you can use the following instructions to complete the recovery:
gzip-dc/tmp/backup.tgz | Tar-xv
Or
Tar xvfz/tmp/backup.tgz
Backup is usually the most important part of just backing up the system, and/etc is an integral part of that. Also, look at the important information in your system that needs to be backed up. In general, you do not need to back up the/bin,/sbin,/usr/bin,/usr/sbin,/usr/x11r6/bin, and so on. Just back up your important files and don't back up your entire hard drive, that's a pretty dumb move.
*************************
If you have many machines, you can use one of the lighter internal network hosts as the primary backup host. Automate the backup of all machines, and then use the network Archive system such as Nfs/coda/samba to put the backed-up data into the backup machine, which collects the backup data at regular intervals and then makes a backup of the machine.
Here is a diagram of the entire system backup scenario.
Before you do so, first of all, in the system, those are to be backed up, those are not needed.
*************************
The new backup
#!/bin/sh
Hostname= ' HOSTNAME '
Dirs= "/etc/var/your_important_directory"
backup= "/tmp/$HOSTNAME. tgz"
Nfs= "/mnt/nfs"
Tar-c $DIRS | gzip-9 > $BACKUP
Mv-f $BACKUP $NFS
*************************
Backup Script:collect_backup within the host
#!/bin/sh
Nfs= "/mnt/nfs"
backup= "/backup"
Mv-f $NFS/*.tgz $BACKUP
In this case, you are not able to put all backups directly in the/mnt/nfs, which is dangerous. In the event that any machine accidentally deletes all the contents of the/mnt/nfs, the backup disappears. Therefore, you need to move the/mnt/nfs to a directory that only the backup host can access.
*************************
When these individual script are tested, we then put them in the crontab. Find your crontab, its location may be in/var/spool/cron/crontabs/root,/etc/crontab,/var/cron/tabs/root.
In Crontab, select one of the following to join (see your regular time):
Slackware:/var/spool/cron/crontabs/root
* * * * * */full_backup_script_path/backup 1>/dev/null 2>/dev/null # per hour (too much)
* * * * */full_backup_script_path/backup 1>/dev/null 2>/dev/null # daily 16:30, back up before work
* * 0/full_backup_script_path/backup 1>/dev/null 2>/dev/null # every Monday 16:30
0 5 1 * */full_backup_script_path/backup 1>/dev/null 2>/dev/null # 5:0 per month
Redhat/debian:/etc/crontab
Redhat can be directly put backup into/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly. or by adding/etc/crontab as follows:
The use of crontab can be found in "Man 5 crontab", which is not detailed here.
Backup host settings are similar.
Note: All machines should not be backed up at the same time, or the internet will be jammed. The backup host will not receive backup data until the backup is set to the last time. You can adjust the time interval after the implementation.
*************************
Take a look at two little less than three lines of Shell Script, with cron this timer tool. It can be simplified to less than 10 minutes by making manual backup work that would otherwise take many hours. Use your imagination and add a little change, but you make your life easier and more enjoyable.
File System Check
System security has always been the concern of most computer users, in the UNIX system, the most important thing, that is, the system has no "Trojan Horse" (Trojan Horse). No matter how Trojan horse put in, there will always be the same, that is, the file of the Trojan, its file date will be changed, and even other changes in the state. In addition, in many situations, the system will have more than some unknown files. So, on weekdays, check that the status of the entire file system has been altered, that all states have changed files, and that there are currently