3 # utility for gathering your crontab file on miscell hosts
5 # relies on a config file named
10 # COMMENTS: line containing a sharp (#)
12 # any other line should be a HOST declaration like this
13 # [method:][username@]hostname
16 # (*) method defaults to ssh:
17 # (*) username defaults to $DEF_USER
22 # => ssh:yourlogin@foo
24 # => rsh:yourlogin@bar
28 # => local:nologin@$(hostname)
30 # the config file can optionnally get passed as an argument
32 COMMAND=$(basename $0)
33 REV=$(echo '$Revision$' | sed -e 's,\$,,g')
35 DEF_CONFIG="$HOME/.$(basename ${COMMAND} .sh)rc"
40 ### try different ways to get username
42 DEF_USER="$(id --user --name 2>/dev/null)"
43 # macintosh : id does not accept this syntax
44 [[ -z "$DEF_USER" ]] && DEF_USER="$LOGNAME"
45 # some other systems I met
46 [[ -z "$DEF_USER" ]] && DEF_USER="$USER"
47 # still not found ? let's bail out
48 if [[ -z "$DEF_USER" ]] ; then
49 echo "$COMMAND : could not get DEF_USER - emergency exit"
55 echo "Usage: $0 [config-file]"
56 echo " default config file is $DEF_CONFIG"
57 echo "Config file syntax :"
58 echo " line containing a # is taken as a comment"
59 echo " other lines should match the following syntax"
60 echo " [method:][username@]hostname"
62 echo " method defaults to ssh (as opposed to rsh or local)"
63 echo " username defaults to $DEF_USER"
68 if [ -n "$OPT_VERBOSE" ] ; then
81 if [ ! -f $config ] ; then
82 error "$config not found"
86 descriptions=$(grep -v '#' $config)
88 for desc in $descriptions ; do
95 method=$(echo $desc | sed -e 's,:.*$,,')
96 user=$(echo $desc | sed -e 's,^.*:,,' -e 's,@.*$,,')
97 host=$(echo $desc | sed -e 's,^.*@,,')
100 method=$(echo $desc | sed -e 's,:.*$,,')
101 host=$(echo $desc | sed -e 's,^.*:,,')
104 user=$(echo $desc | sed -e 's,@.*$,,')
105 host=$(echo $desc | sed -e 's,^.*@,,')
112 if [ "$host" == localhost ] ; then
119 get_crontab $method "$user" $host
122 error "ERROR: $desc - method must be rsh, ssh or local"
137 if [ "$user" = "$DEF_USER" ] ; then
138 backup=~/.crontab-$host
140 backup=~/.crontab-$host-"$user"
143 info "getting crontab for $user on $host with method $method into $backup"
145 ## temporary - overwites backup only if needed
146 tmp=/tmp/$COMMAND-$$.txt
153 ssh "$user"@$host crontab -l
156 rsh -l "$user" $host crontab -l
158 esac > $tmp 2> /dev/null
160 if [ "$?" != 0 ] ; then
161 ### info and not error : allow for too extensive crontabsrc
162 info " could not get crontab on $host"
167 diff $tmp $backup > /dev/null 2>&1
169 if [ "$?" != 0 ] ; then
172 if [ "$?" = 0 ] ; then
173 info "crontab on host $host saved in $backup"
175 error "could not overwrite $backup"
178 info "no changes in crontab on $host - kept $backup"
185 while [[ -n "$@" ]] ; do
188 OPT_VERBOSE="" ; shift ;;
190 OPT_VERSION="true" ; shift ;;
198 if [ -n "$OPT_VERSION" ] ; then
199 echo "This is $COMMAND, $REV"
203 if [[ -n "$@" ]] ; then
207 [ -f $CONFIG ] || echo localhost > $CONFIG