Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Administrator shell script

Shulou Source: shulou.com Published: 2022-06-03 04:18:58 10月06日 Update

Technorati tag: shell,df,sed,awk

-Monitoring system statistics

Monitor disk free space

Disk resource utilization

Monitor CPU and memory usage

-perform backup

Archived data file

Store backup files offline

Monitor system statistics

Monitor disk free space

This shell script monitors the free disk space on a specific volume, such as /, and sends an email message when the free disk space falls below the set threshold.

#! / bin/bash# monitor available disk space# Note format variable = `value `value `SPACE= `df | sed-n'/\ / $/ p' | gawk'{print $5}'| sed's thenecho'if [$SPACE-ge 20] thenecho "Disk space on root at $SPACE% used" | mail-s "Disk warning" elseecho "Disk space on root is OK" fi

Add a task schedule

How often you run this script depends on the activity of the file server. Small space, once a day.

# Mini, 12 30300 every morning * / root/diskmon# large, 4 times a day, 12 30 30 12:30 in the afternoon and 4pm in the afternoon 30300 zero eight 12print 16 * / root/diskmon user disk usage #! / bin/bash# calculate disk usage and report per userTEMP= `mktemp-t tmp.XXXXXX`du-s / home/* | grep-v lost | sed's /\ / home\ /'| sort-g-r > $TEMPTOTAL= `du-s / home | gawk'{print $1} '`cat $TEMP | gawk-v n = "$TOTAL"' BEGIN {print "Total Disk Useage by User" print "User\ tSpace\ tPercent"} {printf "% s\ t% d\ t% 6.2f%\ n", $2 $1, ($1Association) * 100} END {print "-" Printf "Total\ t% d\ n", n}'rm-f $TEMP results as follows [root@docker1 ~] #. / diskhogs.shTotal Disk Useage by UserUser Space Percentbenny 3704 99.68%test 12 0.32%--Total 3716 monitors CPU and memory usage

Capture script

#! / bin/bash# script to capture system statisticsOUTFILE=/root/capstats.csvDATE= `date +% m/%d/% Y`TIME = `date +% print% M:% S`TIMEOUT = `uptime`VMOUT = `vmstat 12`USERS = `echo $TIMEOUT | awk'{print $4} '`LOAD= `echo $TIMEOUT | awk' {print $9}'| sed's / / / '`FREE=' echo "$VMOUT" | sed-n'/ [0-9] / p' | sed-n '2p' | awk' {print $4} '`IDLE= `echo "$VMOUT" | sed-n' / [0-9] / p' | sed-n '2p' | awk' {print $15} '`echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" > $OUTFILE results are as follows: [root@docker1 ~] # cat capstats.csv 12and112016, 0RAP09R02En2 0.04LoadFree Memory%CPU Idle 183360100 output html format #! / bin/bash# parse capstats data into daily reportFILE=/root/capstats.csvTEMP=/root/capstats.htmlMAIL=which muttDATE= `date + "% AMagi% BMague% dMague% Y" `echo "Report for $DATE" > $TEMPecho "> > $TEMPecho" DateTimeUsers "> > $TEMPecho" LoadFree Memory%CPU Idle "> > $TEMPcat $FILE | awk-F,'{printf"% s%s%s ", $1meme 2meme 3 Printf "% s%s%s\ n\ n", $4 Stat report for 5 DATE 6;}'> > $TEMPecho "> > $TEMP$MAIL-a $TEMP-s" Stat report for $DATE "root

< /dev/null#rm -f $TEMP结果如下 执行备份 使用shell脚本备份系统上数据的两种不同的方法。 · 1)归档数据文件 必需函数 tar命令 tar命令用于将整个目录归档为单个文件。 tar -cf archive.tar /root/test 2>

/ dev/null

Create daily archives

#! / bin/bash# archive a working diretoryDATE= `date +% y% m% d`File = archive$DATESOURCE=/root/testDESTINATION=/root/archive/$FILEtar-cf $DESTINATION $SOURCE 2 > / dev/nullgzip $DESTINATION

Create an hourly archive script

#! / bin/bash# archive a working diretory hourlyDAY= `date +% d`Mont = `date +% m`TIME = `date +% k% M`SOURCE = / root/testBASEDEST=/root/archivemkdir-p $BASEDEST/$MONTH/$DAYDESTINATION=$BASEDEST/$MONTH/$DAY/archive$TIMEtar-cf $DESTINATION $SOURCE 2 > / dev/nullgzip $DESTINATION

Can be combined with cron

2) offline storage of backup files

Working directory archives can be sent in the form of email attachments

#! / bin/bash# archive a working directory and e-mail it outMAIL= `which mutt`DATE = `date +% y% m% d`File = archive$DATESOURCE=/root/testDESTINATION=/root/archive/$FILEZIPFILE=$DESTINATION.ziptar-cf $DESTINATION$ SOURCE 2 > / dev/nullzip $ZIPFILE $DESTINATIONrm-f $DESTINATION$MAIL-a $ZIPFILE-s "Archive for $DATE" root@docker1.com < / dev/null

PS: df sed awk

Tags: Files disks monitoring scripts backups spaces conditions information data systems results memory commands formats directories idle mail storage statistics offline Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux OPPO Reno vpn NVidia