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)
34 DEF_CONFIG="$HOME/.$(basename ${COMMAND} .sh)rc"
39 ### try different ways to get username
41 DEF_USER="$(id --user --name 2>/dev/null)"
42 # macintosh : id does not accept this syntax
43 [[ -z "$DEF_USER" ]] && DEF_USER="$LOGNAME"
44 # some other systems I met
45 [[ -z "$DEF_USER" ]] && DEF_USER="$USER"
46 # still not found ? let's bail out
47 if [[ -z "$DEF_USER" ]] ; then
48 echo "$COMMAND : could not get DEF_USER - emergency exit"
54 echo "Usage: $0 [config-file]"
55 echo " default config file is $DEF_CONFIG"
56 echo "Config file syntax :"
57 echo " line containing a # is taken as a comment"
58 echo " other lines should match the following syntax"
59 echo " [method:][username@]hostname"
61 echo " method defaults to ssh (as opposed to rsh or local)"
62 echo " username defaults to $DEF_USER"
67 if [ -n "$OPT_VERBOSE" ] ; then
80 if [ ! -f $config ] ; then
81 error "$config not found"
85 descriptions=$(grep -v '#' $config)
87 for desc in $descriptions ; do
94 method=$(echo $desc | sed -e 's,:.*$,,')
95 user=$(echo $desc | sed -e 's,^.*:,,' -e 's,@.*$,,')
96 host=$(echo $desc | sed -e 's,^.*@,,')
99 method=$(echo $desc | sed -e 's,:.*$,,')
100 host=$(echo $desc | sed -e 's,^.*:,,')
103 user=$(echo $desc | sed -e 's,@.*$,,')
104 host=$(echo $desc | sed -e 's,^.*@,,')
111 if [ "$host" == localhost ] ; then
118 get_crontab $method "$user" $host
121 error "ERROR: $desc - method must be rsh, ssh or local"
136 if [ "$user" = "$DEF_USER" ] ; then
137 backup=~/.crontab-$host
139 backup=~/.crontab-$host-"$user"
142 info "getting crontab for $user on $host with method $method into $backup"
144 ## temporary - overwites backup only if needed
145 tmp=/tmp/$COMMAND-$$.txt
152 ssh "$user"@$host crontab -l
155 rsh -l "$user" $host crontab -l
157 esac > $tmp 2> /dev/null
159 if [ "$?" != 0 ] ; then
160 ### info and not error : allow for too extensive crontabsrc
161 info " could not get crontab on $host"
166 diff $tmp $backup > /dev/null 2>&1
168 if [ "$?" != 0 ] ; then
171 if [ "$?" = 0 ] ; then
172 info "crontab on host $host saved in $backup"
174 error "could not overwrite $backup"
177 info "no changes in crontab on $host - kept $backup"
184 while [[ -n "$@" ]] ; do
187 OPT_VERBOSE="" ; shift ;;
189 OPT_VERSION="true" ; shift ;;
197 if [ -n "$OPT_VERSION" ] ; then
198 echo "This is $COMMAND"
202 if [[ -n "$@" ]] ; then
206 [ -f $CONFIG ] || echo localhost > $CONFIG