linux和aix获取crontab、crontab对应的脚本(sh)文件

 留备

#!/bin/bash
#获取各个用户下的crontab以及对应的sh脚本文件(如果有)
if [ "`whoami`" != "root" ];then
	echo "Error:must run as root!"
	exit 1
fi

#由于有多个服务器的crontab需要导出,为方便辨别是哪个服务器导出的文件,tar包命名时加上IP后缀
sys=`uname`
if [ "$sys" = "Linux" ];then
	file_path=/var/spool/cron
	ipaddr=`ifconfig -a|grep inet|grep -v inet6|grep -v 127.0.0.1|awk -F. '{print $4}'|awk '{print $1}'`
elif [ "$sys" = "AIX" ];then
	file_path=/var/spool/cron/crontabs
	ipaddr=`ifconfig -a|grep inet|grep -v inet6|grep -v 127.0.0.1|awk -F. '{print $2}'|awk '{print $4}'`
fi

#判断是否存在crontab文件,不存在则退出
if_exist=`ls -l $file_path |grep -v total|wc -l`
if [ $if_exist -eq 0 ];then
	echo "no crontab exist,exit!"
	exit 0
fi

mkdir -p /tmp/"$ipaddr"_cron
dest_dir=/tmp/"$ipaddr"_cron


cd $dest_dir
cp $file_path/* $dest_dir
for word in `ls $file_path`
do
	mkdir ${word}_cron
	while read line
		do 
			#首字母为井号的直接跳过
			szm=`echo "$line"|cut -c 1`
				if [ "$szm"x = "#"x ];then
					continue
				fi
				shell_type=`echo "$line"|awk '{print $6}'`
				shell_command=`echo "$line"|awk '{print $7}'`
				if [ "$shell_type"x = "sh"x -a -f "$shell_command" ];then
					cp $shell_command $dest_dir/${word}_cron
				fi
		done < $file_path/$word
done
cd /tmp/
tar -Pcvf $dest_dir.tar $dest_dir
	

	

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值