1.每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式为“etcbak-yyyy-mm-ddHH.tar.xz”,其中日期是前一天的时间。
[root@localhost /data/scripts]#cat etc_backup.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: etc_backup.sh
#********************************************************************
[ -d /backup ] || mkdir /backup
rpm -q xz >/dev/null
[ $? -eq 0 ] || yum -y install xz >/dev/null
DATEFORMAT=`date -d yesterday "+%Y-%m-%d-%H"`
tar -Jcf /backup/etcbak-$DATEFORMAT.tar.xz /etc &> /dev/null
[root@localhost /data/scripts]#vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
30 1 * * 0 root sh /data/scripts/etc_backup.sh &>/dev/null
2.每两小时取出当前系统/proc/meminfo文件中以S或M开头的信息追加至/tmp/meminfo.txt文件中。
crontab -e
1 */2 * * * /bin/grep "^S|^M" /proc/meminfo >> /temp/meminfo.txt
3.工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就执行wall警报。
[root@localhost /data/scripts]#bash check_disk.sh
100
Warning!!!
/dev/sr0's avaliable space is less than 20%|mail -s Warning root
[root@localhost /data/scripts]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 1.7G 99G 2% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 72M 919M 8% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 50G 169M 50G 1% /data
/dev/sda1 1014M 127M 888M 13% /boot
/dev/sr0 4.3G 4.3G 0 100% /mnt/cdrom
tmpfs 199M 0 199M 0% /run/user/0
[root@localhost /data/scripts]#cat check_disk.sh
#!/bin/bash
#********************************************************************
#Author: Kevin.Wen
#Revision: 1.0
#QQ: 2510905014
#Date: 2020-10-16
#FileName: check_disk.sh
#********************************************************************
Usage_Rate=`df |awk -F '[ %]+' 'BEGIN{max = 0}{if ($5+0 > max) max=$5} END {print max}'`
Partition_Name=`df |grep "$Usage_Rate%" |cut -d" " -f1`
echo $Usage_Rate
if [ $Usage_Rate -gt 80 ];then
echo -e "Warning!!! \n $Partition_Name's avaliable space is less than 20%|wall 'disk will be full'"
fi
*/10 * * * 1-5 root sh /data/scripts/check_disk.sh &>/dev/null