SHLL script summary

xiaoxiao2021-03-06  43

1, the mysql database automatic backup. SH

#! / bin / bash # this is a shellscript for auto db backup # Powered by aspbiz@163.com # 2004-09#SettingDBName=Mysqldbuser=RootdbPasswd=/root/db.logdbpath=/VAR / lib / mysql / # BackupMethod = mysqldump # BackupMethod = mysqlhotcopy # BackupMethod = tar # Setting EndNewFile = "$ BackupPath" db $ (date % y% m% d) .tgzDumpFile = "$ BackupPath" db $ (date % Y% m% d) Oldfile = "$ backuppath" DB $ (DATE % Y% M% D --Date = '5 days ago'). TGzecho "-------------- ----------------------------- ">> $ logfileecho $ (DATE "% Y-% M-% D% H: % M:% s ") >> $ logfileecho" -------------------------- ">> $ logfile # delete old fileif [-f $ Oldfile】 THEN RM -F $ Oldfile >> $ logfile 2> & 1 echo "[$ oldfile] delete old file surcess!" >> $ logfileelse echo "[$ oldfile] no old backup file!" >> $ logfilefiif [ F $ newfile] Then echo "[$ newfile] The Backup File is exists, can't backup!" >> $ logfileelse case $ backupmethod in mysqldump) IF [-z $ dbpasswd] Then mysqldump -u $ dbuser --Opt $ DBNAME> $ dumpfile else mysqldump -u $ dbuser -p $ dbpasswd - ipt $ dbname> $ Dumpfile Fi Tar CZVF $ NEWFILE $ DUMPFILE >> $ logfile 2> & 1 echo "[$ newfile] backup success!" >> $ logfile rm -rf $ dumpfile ;; mysqlhotcopy) RM-RF $ dumpfile mkdir $ dumpfile if [-z $ dBPasswd] then mysqlhotcopy -u $ DBUser $ DBName $ DumpFile >> $ LogFile 2> & 1 else mysqlhotcopy -u $ DBUser -p $ dBPasswd $ DBName $ DumpFile >> $ LogFile 2> & 1 fi tar czvf $ NewFile $ DumpFile >> $ Logfile 2> & 1 echo "[$ newfile] backup success!" >> $ logfile rm -rf $ dumpfile ;;

*) /Etc/init.d/mysqld stop> / dev / null 2> & 1 TAR CZVF $ newfile $ dbpath $ dbname >> $ logfile 2> & 1 /etc/init.d/mysqld start> / dev / null 2> & 1 echo "[$ newfile] backup success!" >> $ logfile ;; esacfiecho ----------------------------------------------------------------------------------------------------------------------------------------- ------------ ">> $ logfile2, log automatic clearance script

Timed as follows: * * * * /shell/autdeletelog.SH u/Dev/null 2> & 1 scripting content: #! / Bin / sh / usr / bin / find / var / log / -name "messag *" -mtime 10 -Print | / usr / bin / xargs / bin / rm -f / usr / bin / find / var / log / -name "secur *" -mtime 10 -print | / usr / bin / xargs / bin / RM -F / USR / BIN / FIND / VAR / LOG / -NAME "Mail *" -mtime 10 -Print | / usr / bin / xargs / bin / rm -f / usr / bin / find / var / log / -Name "dhcp *" -mtime 10 -print | / usr / bin / xargs / bin / rm -f / usr / bin / find / var / log / -name "cro *" -mtime 10 -print | / USR / BIN / Xargs / Bin / RM -FEXIT

3. Monitor Linux system logs in real time, you can know the startup time of the system, etc.! The following scripts are mainly to monitor the logs generated by the system, as long as there are also logging contents in the log, and generate the desired log files in real time, to keep check! Note: If the log function of the system has been destroyed, this method is invalid. Vi Run_log.sh #! / bin / bash

While [true]; do date = `DATE % Y% M% D` Tail -f / var / log / message | egrep" (syslogd 1.4.1: restart | login on) >> / home / log / run_syslog . $ Date Sleep 2 Done

CHMOD u x run_log.sh

-------------------------------------------------- ---------------------- Tail -f / var / log / message | eGrep "(syslogd 1.4.1: restart | login on) >> / Home / log / run_syslog. $ Date In the above script, (syslogd 1.4.1: restart | login on) is the content existing in the monitor system log file, different contents in the middle of the monitor system log file, -------------------------------------------------- -----------------------

Also Begm Be sure to join this script file into the system boot file, that is, let the system start this script to start the monitoring effect, join the method as follows: vi /etc/rc.d/rc.local# !/bin/sh # # This script will be executed * after * all the other init scripts. # You can put your it atitization stuff in Here if you don't # Want to do the full sys v style init stuff. Touch / var / lock / subsysys / local sh/usr/run_log.sh Save Exit, let the system restart, use ps -ef | grep run_log.sh, check if this script has been started, or view / home / log / under run_syslog.2004- 08-07 This file. After this success, I will know when the server is restarted later, and some people have logged in the server system. ^ _ ^. . This is not to do more things to do more, don't know when the system has been logged in by Hacker, I don't know. As long as you often go to view the generated log files. 4. Implement automatic backup and automatic clear log files in UNIX system -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------- Perform backup and empty the current log files, clear_countlog.shzcatlinux, zcatlinux@yahoo.com.cn, 2004- 06-28 ----------------------------------------------- ---------------- #! / bin / bashlogfile = / var / count.logdate = `date % y% m% d`if [-f $ logfile] TEN CP $ Logfile $ logfile- $ date find / var / -ctime 3 -name count / * -exec rm {} /; echo "> $ logfilefi ------------------- ------------------------------ Implementing the above scripts every day in crontab! -------------------------------------------------- 10 3 × × × Root /Home/clear_countlog.sh

5, as long as the log of the monitor can also realize the startup of the program

#! / bin / bashwhile [1]; do jsprog = `ps -ef | grep jsrun | sed -e '/ grep / d'ration [" $ pROG "]; THEN JSLOG =` tail -n 12 / var / JMIS / LOG / NOHUP.out | GREP "over stop" | wc -l`ness [$ jslog -gt 0]; THEN CD / VAR / JMIS / BIN / NOHUP /VAR/JMIS/bin/jsrun.sh & else Sleep 60 Fi Else CD / VAR / JMIS / BIN / NOHUP /VAR/JMIS/bin/jsrun.sh & Sleep 120 Fidone6, allow the system to automatically find ways to delete files!

#! / bin / bash find / home / log / -ctime 3-name bbslog / * -exec rm {} /; ## Find the BBSLOG file before the third day and delete Find / TMP / LOG / -CTIME 4 - Name mail / * -exec rm {} /; ## Find all Mail log files 4 days ago, and remove all

7. Configure crontabs in Linux and Solaris, respectively.

## set crontab timeif ["$ OS" = "linux"]; then echo "10 3 * * * root /usr/glog.sh" >> / etc / crontab service crd restartelse echo "10 3 * * * / usr / VAR / SPOOL / CRON / CRONTABS / ROOT for PID IN `PS - EF | GREP CRON | SED -E '/ GREP / D' | awk '{print $ 2}'` do kill -9 $ PID DONE RM -RF /ETC/cron.d/fifo / usr / sbin / cronfi

转载请注明原文地址:https://www.9cbs.com/read-58694.html

New Post(0)