#!/bin/bash
# *************************************************************
# File: getcrontab.sh
# Date: 2008-07-16 10:55
# Author: Marko Schulz - <info@tuxnet24.de>
# Description: Save all crontabs of all unixusers where the UID is greater than 1000.
# *************************************************************
# List of unixuser that will be ignored (space seperated list).
prune="nobody"
# Directory where the crontabs will be stored.
directory="/data/backup/$( hostname )/crontabs"
# *************************************************************
# This function save the crontab of the current unixuser in
# the backup directory and set the file access to 640. Empty
# crontab files will be deleted.
function rc_save_crontab () {
local user=$1
local directory=$2
# Backup the crontab of the current user.
crontab -u ${user} -l >${directory}/${user} 2>/dev/null
# Delete all empty crontabs.
[ ! -s "${directory}/${user}" ] && rm -f ${directory}/${user}
# Remove read access for others.
[ -f "${directory}/${user}" ] && chmod o-r ${directory}/${user} 2>/dev/null
}
# *************************************************************
# Program action...
# Replace all spaces into pipe character.
prune=$( echo $prune | sed 's/[[:space:]]\{1,\}/|/g' )
# Create backup directory if not exists.
[ ! -d "$directory" ] && mkdir -p $directory 2>/dev/null
# First, we clean the directory.
rm -f ${directory}/*
# Match all vailed unixuser...
for user in $( awk -F: '$1 !~ /('$prune')/ && $3 >= 1000 { print $1 }' /etc/passwd ); do
rc_save_crontab "${user}" "${directory}"
done
# At last we save the crontab entry from root.
rc_save_crontab "root" "${directory}"
# *************************************************************
# EOF