留备
#!/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