Tuesday, February 23, 2010

Backup of CRON list and jobs

#!/bin/bash
cd /opt/psycho/
mkdir `hostname`_crons
OUT=`hostname`_crons
> crons.txt
> $OUT/cronlist
for i in `ls /var/spool/cron/`
do
grep $i /etc/passwd
if [ $? = 0 ]
then
{
crontab -u $i -l >> $OUT/$i.cron
crontab -u $i -l | grep -v "^#" | sort | uniq | awk {'print $6'} >> crons.txt
crontab -u $i -l | grep -v "^#" | sort | uniq | awk {'print $7'} >> crons.txt
crontab -u $i -l | grep -v "^#" | sort | uniq | awk {'print $8'} >> crons.txt
}
fi
done

for i in `cat crons.txt`
do
cp $i $OUT 2> /dev/null
done
tar -czf $OUT.tar.gz2 $OUT

It works in Linux. Other NIXes may require some rendering

No comments: