585b7dd31e742f3c3bfe188e3d58ded7f37dadce
[infrastructure.git] / scripts / crontabs.sh
1 #!/bin/bash
2
3 # utility for gathering your crontab file on miscell hosts
4
5 # relies on a config file named
6 # ~/.crontabsrc
7
8 # SYNTAX
9 #
10 # COMMENTS: line containing a sharp (#)
11
12 # any other line should be a HOST declaration like this
13 # [method:][username@]hostname
14 #
15 # defaults :
16 # (*) method defaults to ssh:
17 # (*) username defaults to $DEF_USER
18
19 # examples
20 #
21 # foo
22 #       =>      ssh:yourlogin@foo
23 # rsh:bar
24 #       =>      rsh:yourlogin@bar
25 # joe@tutu
26 #       =>      ssh:joe@tutu
27 # localhost
28 #       =>      local:nologin@$(hostname)
29
30 # the config file can optionnally get passed as an argument
31
32 COMMAND=$(basename $0)
33 REV=$(echo '$Revision: 437 $' | sed -e 's,\$,,g')
34
35 DEF_CONFIG="$HOME/.$(basename ${COMMAND} .sh)rc"
36
37 OPT_VERBOSE="true" 
38 OPT_VERSION="" 
39
40 ### try different ways to get username
41 # linux
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"
50   exit 1
51 fi
52
53 usage () {
54
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"
61   echo "Defaults:"
62   echo "  method defaults to ssh (as opposed to rsh or local)"
63   echo "  username defaults to $DEF_USER"
64 }
65
66 ####################
67 info () {
68   if [ -n "$OPT_VERBOSE" ] ; then
69     echo "INFO : $@"
70   fi
71 }
72
73 error () {
74   echo "ERROR : $@"
75 }
76
77 ####################
78 parse_config () {
79   config=$1; shift
80
81   if [ ! -f $config ] ; then
82     error "$config not found"
83     exit 1
84   fi
85
86   descriptions=$(grep -v '#' $config)
87
88   for desc in $descriptions ; do
89
90     ###defaults
91     method=ssh
92     user="$DEF_USER"
93     case $desc in
94       *:*@*)
95         method=$(echo $desc | sed -e 's,:.*$,,')
96         user=$(echo $desc | sed -e 's,^.*:,,' -e 's,@.*$,,')
97         host=$(echo $desc | sed -e 's,^.*@,,')
98         ;;
99       *:*)
100         method=$(echo $desc | sed -e 's,:.*$,,')
101         host=$(echo $desc | sed -e 's,^.*:,,')
102         ;;
103       *@*)
104         user=$(echo $desc | sed -e 's,@.*$,,')
105         host=$(echo $desc | sed -e 's,^.*@,,')
106         ;;
107       *)
108         host=$desc
109         ;;
110     esac
111
112     if [ "$host" == localhost ] ; then
113       host="$(hostname)"
114       method="local"
115     fi
116
117     case $method in
118       rsh|ssh|local)
119         get_crontab $method "$user" $host
120         ;;
121       *)
122         error "ERROR: $desc - method must be rsh, ssh or local"
123         break
124         ;;
125     esac
126
127   done
128 }
129
130 ####################
131 get_crontab () {
132   method=$1; shift
133   user=$1; shift
134   host=$1; shift
135
136   ## where to backup
137   if [ "$user" = "$DEF_USER" ] ; then
138     backup=~/.crontab-$host
139   else
140     backup=~/.crontab-$host-"$user"
141   fi
142
143   info "getting crontab for $user on $host with method $method into $backup"
144
145   ## temporary - overwites backup only if needed
146   tmp=/tmp/$COMMAND-$$.txt
147
148   case $method in
149     local)
150       crontab -l
151       ;;
152     ssh)
153       ssh "$user"@$host crontab -l
154       ;;
155     rsh)
156       rsh -l "$user" $host crontab -l
157       ;;
158   esac > $tmp 2> /dev/null
159
160   if [ "$?" != 0 ] ; then
161     ### info and not error : allow for too extensive crontabsrc
162     info " could not get crontab on $host"
163     rm -f $tmp
164     return 1
165   fi
166
167   diff $tmp $backup > /dev/null 2>&1
168
169   if [ "$?" != 0 ] ; then
170     ## files differ
171     cp $tmp $backup
172     if [ "$?" = 0 ] ; then
173       info "crontab on host $host saved in $backup"
174     else
175       error "could not overwrite $backup"
176     fi
177   else
178     info "no changes in crontab on $host - kept $backup"
179     rm -f $tmp
180   fi
181 }
182 ####################
183 main () {
184
185   while [[ -n "$@" ]] ; do
186     case "$1" in
187       -s*|--silent*)
188         OPT_VERBOSE="" ; shift ;;
189       -v*|--version*)
190         OPT_VERSION="true" ; shift ;;
191       -*)
192         usage ; exit 1 ;;
193       *)
194         break ;;
195     esac
196   done
197
198   if [ -n "$OPT_VERSION" ] ; then
199     echo "This is $COMMAND, $REV"
200     exit 0
201   fi
202
203   if [[ -n "$@" ]] ; then
204     CONFIG=$1; shift
205   else
206     CONFIG=$DEF_CONFIG
207   fi
208
209   parse_config $CONFIG
210 }
211
212 ####################
213 main "$@"