1.写一个脚本,完成如下功能 传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;
(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;
(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;
(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;
(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts
[root@localhost scripts]# vim 1.sh
read -p "请输入序号指定打包压缩的类型:" NAME
case $NAME in
gzip)
tar -czvPf /backups/etc-20160613.tar.gz /etc/* >/dev/null
;;
bzip2)
tar -cjvPf /backups/etc-20160613.tar.bz2 /etc/* >/dev/ull
;;
xz)
tar -cjvPf /backpus/etc-20160613.tar.xz /etc/* >/dev/null
;;
*)
echo "错误的压缩工具"
exit 1
;;
esac
[root@localhost scripts]# bash 1.sh
请输入序号指定打包压缩的类型:a
错误的压缩工具
[root@localhost scripts]# bash 1.sh
请输入序号指定打包压缩的类型:xz
[root@localhost scripts]# cd ..
[root@localhost /]# cd /backpus
[root@localhost backpus]# ll
总用量 17052
-rw-r--r--. 1 root root 17460489 1月 22 13:48 etc-20160613.tar.xz
2.报警脚本,要求如下:
根分区剩余空间小于20%
发送告警邮件给自己
配合crond每5分钟检查一次脚本
[root@localhost /]# set from=111111111@163.com
[root@localhost /]# set smtp=smtp.163.com
[root@localhost /]# set smtp-auth-user=111111111@163.com
[root@localhost /]# set smtp-auth-password=AXAXA
[root@localhost /]# set smtp-auth=login
[root@localhost /]# echo "testmail" | mail -s "testmail" 11111111@163.com
创建脚本文件进行脚本配置:
root_space=$(df -h / | awk 'NR==2 {print $5}' | cut -d'%' -f1)
if [ $root_space -lt 20 ];
then
echo "根分区剩余空间不足20%,请及时处理!" | mail -s "根分区剩余空间不足警告" 11111111@163.com
fi
df -h /
awk 'NR==2 {print $5}'
cut -d'%' -f1
root_space=$(...)
上述代码解释运行代码:
[root@localhost scripts]# echo $(df -h /)
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/openeuler-root 21G 1.7G 19G 9% /
[root@localhost scripts]# echo $(df -h / | awk 'NR==2 {print $5}')
9%
[root@localhost scripts]# echo $(df -h / | awk 'NR==2 {print $5}' | cut -d'%' -f1)
9
进行循环配置:
[root@localhost /]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: >
Active: active (running) since Sun 2024-01-21 11:50:09 CST; 8h ago
Main PID: 960 (crond)
Tasks: 1 (limit: 8914)
Memory: 22.6M
CGroup: /system.slice/crond.service
└─ 960 /usr/sbin/crond -n
crontab -e
*/5 * * * * bash /scripts/2.sh