Administration
對 Linux 系統管理員有用的重要腳本
我是一名 Linux 系統管理員,經常發現自己一次又一次地做同樣的家務。我需要一些腳本來讓生活更輕鬆。Linux 系統管理員需要哪些最重要的腳本?
雖然下面提供的所有腳本可能都是準確的,但可能還有更高級的功能可用。但是,這些腳本只是為了概述系統管理員應該如何完成工作。我沒有製作以下任何腳本,並且所有腳本的引用都與它們的描述一起內聯給出。
磁碟使用情況
此腳本可用於分析磁碟使用情況,如果報告的磁碟空間超過 90 %,則會向管理員發送一封電子郵件。腳本取自這裡。
#!/bin/sh # set -x # Shell script to monitor or watch the disk space # It will send an email to $ADMIN, if the (free available) percentage of space is >= 90%. # ------------------------------------------------------------------------- # Set admin email so that you can get email. ADMIN="root" # set alert level 90% is default ALERT=90 # Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions. # An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5" EXCLUDE_LIST="/auto/ripper" # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: # function main_prog() { while read output; do #echo $output usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1) partition=$(echo $output | awk '{print $2}') if [ $usep -ge $ALERT ] ; then echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)" | \ mail -s "Alert: Almost out of disk space $usep%" $ADMIN fi done } if [ "$EXCLUDE_LIST" != "" ] ; then df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog else df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' | main_prog fi
增量備份腳本
此腳本將增量備份到外部安裝的硬碟驅動器中。它是對 /home 目錄進行備份。但是,可以對其進行修改以適應要求。腳本取自這裡。
#!/bin/bash # ---------------------------------------------------------------------- # mikes handy rotating-filesystem-snapshot utility # ---------------------------------------------------------------------- # this needs to be a lot more general, but the basic idea is it makes # rotating backup-snapshots of /home whenever called # ---------------------------------------------------------------------- unset PATH # suggestion from H. Milz: avoid accidental use of $PATH # ------------- system commands used by this script -------------------- ID=/usr/bin/id; ECHO=/bin/echo; MOUNT=/bin/mount; RM=/bin/rm; MV=/bin/mv; CP=/bin/cp; TOUCH=/bin/touch; RSYNC=/usr/bin/rsync; # ------------- file locations ----------------------------------------- MOUNT_DEVICE=/dev/hdb1; SNAPSHOT_RW=/root/snapshot; EXCLUDES=/usr/local/etc/backup_exclude; # ------------- the script itself -------------------------------------- # make sure we're running as root if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root. Exiting..."; exit; } fi # attempt to remount the RW mount point as RW; else abort $MOUNT -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ; if (( $? )); then { $ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite"; exit; } fi; # rotating snapshots of /home (fixme: this should be more general) # step 1: delete the oldest snapshot, if it exists: if [ -d $SNAPSHOT_RW/home/hourly.3 ] ; then \ $RM -rf $SNAPSHOT_RW/home/hourly.3 ; \ fi ; # step 2: shift the middle snapshots(s) back by one, if they exist if [ -d $SNAPSHOT_RW/home/hourly.2 ] ; then \ $MV $SNAPSHOT_RW/home/hourly.2 $SNAPSHOT_RW/home/hourly.3 ; \ fi; if [ -d $SNAPSHOT_RW/home/hourly.1 ] ; then \ $MV $SNAPSHOT_RW/home/hourly.1 $SNAPSHOT_RW/home/hourly.2 ; \ fi; # step 3: make a hard-link-only (except for dirs) copy of the latest snapshot, # if that exists if [ -d $SNAPSHOT_RW/home/hourly.0 ] ; then \ $CP -al $SNAPSHOT_RW/home/hourly.0 $SNAPSHOT_RW/home/hourly.1 ; \ fi; # step 4: rsync from the system into the latest snapshot (notice that # rsync behaves like cp --remove-destination by default, so the destination # is unlinked first. If it were not so, this would copy over the other # snapshot(s) too! $RSYNC \ -va --delete --delete-excluded \ --exclude-from="$EXCLUDES" \ /home/ $SNAPSHOT_RW/home/hourly.0 ; # step 5: update the mtime of hourly.0 to reflect the snapshot time $TOUCH $SNAPSHOT_RW/home/hourly.0 ; # and thats it for home. # now remount the RW snapshot mountpoint as readonly $MOUNT -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ; if (( $? )); then { $ECHO "snapshot: could not remount $SNAPSHOT_RW readonly"; exit; } fi;
高 CPU 使用率腳本
有時,我們需要監控系統中的高 CPU 使用率。我們可以使用下面的腳本來監控高 CPU 使用率。腳本取自這裡。
#!/bin/bash while [ true ] ;do used=`free -m |awk 'NR==3 {print $4}'` if [ $used -lt 1000 ] && [ $used -gt 800 ]; then echo "Free memory is below 1000MB. Possible memory leak!!!" | /bin/mail -s "HIGH MEMORY ALERT!!!" user@mydomain.com fi sleep 5 done
向 Linux 系統添加新使用者
此腳本允許 root 使用者或管理員通過鍵入使用者名和密碼(密碼以加密方式輸入)以更簡單的方式將新使用者添加到系統中。下面的腳本取自這裡。
#!/bin/bash # Script to add a user to Linux system if [ $(id -u) -eq 0 ]; then read -p "Enter username : " username read -s -p "Enter password : " password egrep "^$username" /etc/passwd >/dev/null if [ $? -eq 0 ]; then echo "$username exists!" exit 1 else pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) useradd -m -p $pass $username [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!" fi else echo "Only root may add a user to the system" exit 2 fi
數據庫備份
該腳本是一個非常基本的腳本,可用於備份數據庫。腳本取自這裡。
#!/bin/sh now="$(date +'%d_%m_%Y_%H_%M_%S')" filename="db_backup_$now".gz backupfolder="/var/www/vhosts/example.com/httpdocs/backups" fullpathbackupfile="$backupfolder/$filename" logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile" mysqldump --user=mydbuser--password=mypass --default-character-set=utf8 mydatabase | gzip > "$fullpathbackupfile" echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile" chown myuser "$fullpathbackupfile" chown myuser "$logfile" echo "file permission changed" >> "$logfile" find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \; echo "old files deleted" >> "$logfile" echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile" echo "*****************" >> "$logfile" exit 0