Merge remote branch 'mru/pump_switch'
[grml-autoconfig.git] / bin / save-config
1 #!/bin/zsh
2 # Filename:      save-config
3 # Purpose:       generate grml configuration archive and store it anywhere
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 # some zsh-stuff {{{
10   autoload colors ; colors
11   setopt nonomatch
12   . /etc/grml/sh-lib
13   . /etc/grml/script-functions
14 # }}}
15
16 # set variables  {{{
17
18   # old linuxrc version:
19   [ -d /cdrom ]      && OLDLAYOUT=1 || OLDLAYOUT=''
20   # new initramfs layout:
21   [ -d /live/image ] && NEWLAYOUT=1 || NEWLAYOUT=''
22
23   LANG=C
24   LC_ALL=C
25   [[ $(id -u) != 0 ]] && runas='sudo' # important for /etc
26
27   check4progs findchanged tar || { echo "Sorry, can't continue. Exiting.">&2 ; exit 1 }
28
29   CONFIG=/etc/grml/saveconfig
30   [ -r "$CONFIG" ] && . $CONFIG
31
32   PROGRAMNAME=${0##*/}
33   HOSTNAME=$(hostname)
34   DATE=$(date)
35   GRML_VERSION=$(awk '{print $1}' /etc/grml_version 2>/dev/null || print "not a grml system")
36   KERNEL=$(uname -a)
37
38   TMPDIR=/tmp
39   MAILFILE="$TMPDIR/mail.txt"
40
41   [ -n "$FILELIST" ] || FILELIST=$(mktemp $TMPDIR/filelist.XXXXXX)
42 # }}}
43
44 # functions {{{
45 debug(){
46   if [[ $DEBUG -gt 0 ]] ; then
47     echo "debug: $*"
48   fi
49 # setopt xtrace
50 # set -x
51 }
52
53 bailout(){
54   rm -f "$FILELIST"
55   rm -f "$MAILFILE"
56 }
57
58 trap bailout 1 2 3 15
59 # }}}
60
61 # usage information {{{
62 usage()
63 {
64   print 1>&2 "
65 $bg[black]$fg[green]${boldcolor}${PROGRAMNAME} - save configuration of grml system${reset_color}
66
67 $bg[black]$fg[blue]${boldcolor}Usage:${reset_color}
68   $PROGRAMNAME [-target_options] -{all,home,etc,configdir}
69
70 $bg[black]$fg[blue]${boldcolor}Target options:${reset_color}
71   -ssh user@host:/path/to/file  copy configuration via ssh/scp to remote host
72   -mail <recipient>             send configuration via mail
73   -file foo_bar_config.tbz      save configuration in specified file
74
75   Notice: if no option is specified the default is assumed:
76           create file config.tbz in current directory
77
78 $bg[black]$fg[blue]${boldcolor}Files-to-store options:${reset_color}
79   -home                         store hidden files from \$HOME (\$HOME/.*)
80   -grmlhome                     store hidden files from \$HOME (\$HOME/.*) of user grml [use as user root]
81   -etc                          store modified files from /etc
82   -configdir                    store \$HOME/config
83   -all                          store all configuration files (:= -home, -configdir and -etc)
84
85   Notice: it is also possible to use environment variables:
86           \$SAVE_HOME, \$SAVE_GRMLHOME, \$SAVE_ETC, \$SAVE_CONFIGDIR and \$SAVE_ALL
87
88 $bg[black]$fg[blue]${boldcolor}Usage examples:${reset_color}
89   $PROGRAMNAME -all                                  => store all configuration files in config.tbz in current dir
90   $PROGRAMNAME -home -mail  devnull@grml.org         => store \$HOME/.* in config.tbz and send it via mail
91   $PROGRAMNAME -etc  -ssh   devnull@grml.org:/path/  => store /etc in config.tbz and scp it to specified host
92   $PROGRAMNAME -all  -file  foo.tbz                  => store all configuration files in foo.tbz
93   SAVE_ALL=yes $PROGRAMNAME -file /path/foo.tbz      => store all configuration files in /path/foo.tbz
94
95 More information on save-config can be found in the manual page: man save-config
96
97 See also: restore-config(1), bootoptions: myconfig=/dev/ice, extract=PATH,
98           netconfig=server.tld/path/to/config.tbz
99
100 Report bugs, send wishes and feedback to the grml team:
101 http://grml.org/bugs/ - contact (at) grml.org
102 "
103 }
104 # }}}
105
106 # what do we want to store? {{{
107 save_home(){
108   debug "save home"
109   for i in $HOME/.* ; do findchanged "$i" /etc/skel/$(basename "$i"); done >> $FILELIST
110   debug "debug: $FILELIST"
111 }
112
113 save_grmlhome(){
114   debug "save grmlhome"
115   if [ -d /home/grml/ ] ; then
116      for i in /home/grml/.* ; do findchanged "$i" /etc/skel/$(basename "$i"); done >> $FILELIST
117   fi
118   debug "debug: $FILELIST"
119 }
120
121 save_etc(){
122   debug "save etc"
123   if [ -n "$NEWLAYOUT" ] ; then
124      $runas find /live/cow/etc -type f -o -type l | sed -e 's#/live/cow## ; /etc$/d' >> $FILELIST
125   else
126      $runas findchanged /etc /GRML/etc >> $FILELIST
127   fi
128 }
129
130 save_configdir(){
131   debug "save configdir"
132   if [ -d $HOME/config ] ; then
133      ls $HOME/config/*  >> $FILELIST 2>/dev/null
134      ls $HOME/config/.* >> $FILELIST 2>/dev/null
135   fi
136 }
137
138 # create configuration file {{{
139 create_config(){
140   if ! [ -r "$FILELIST" ]; then
141      echo "Sorry, filelist $FILELIST could not be read." >&2
142      echo "Error when generating $FILENAME." >&2
143      exit 1
144   else
145      # GNU tar sucks so much, really. Avoid the "file changed as we read it":
146      tar cf /dev/null /etc 2>/dev/null
147      # now really execute the according tar command:
148      BZIP2=-9 $runas tar -T - -cpPjf "$FILENAME" <"$FILELIST" && \
149      echo "Successfully stored configuration in file $FILENAME"   || \
150      echo "Error when generating $FILENAME." >&2
151   fi
152 }
153 # }}}
154
155 # commandline parsing {{{
156 parse_options()
157 {
158    zparseopts -K -- help=o_help mail:=o_mail \
159                     file:=o_file home=o_home grmlhome=o_grmlhome etc=o_etc \
160                     configdir=o_configdir all=o_all ssh:=o_ssh
161
162    if [[ "$#" == 0 || "$o_help" != "" || "$1" == '-h' || "$1" == '--help' ]]; then
163       usage ; exit
164    fi
165
166    if [[ "$o_file" != "" ]]; then
167      FILENAME="$o_file[2]"
168    else
169      FILENAME="config.tbz"
170    fi
171
172    if [[ "$o_home" != "" ]]; then
173       debug "home is set"
174       SAVE_HOME="yes"
175    fi
176
177    if [[ "$o_grmlhome" != "" ]]; then
178       debug "grmlhome is set"
179       SAVE_GRMLHOME="yes"
180    fi
181
182    if [[ "$o_etc" != "" ]]; then
183       debug "etc is set"
184       SAVE_ETC="yes"
185    fi
186
187    if [[ "$o_configdir" != "" ]]; then
188       debug "configdir is set"
189       SAVE_CONFIGDIR="yes"
190    fi
191
192    if [[ "$o_all" != "" ]]; then
193       debug "home, grmlhome, etc and configdir are set"
194       SAVE_HOME="yes"
195       SAVE_GRMLHOME="yes"
196       SAVE_ETC="yes"
197       SAVE_CONFIGDIR="yes"
198    fi
199
200    if [[ "$o_ssh" != "" ]]; then
201       debug "scp $FILENAME $o_ssh[2]"
202       scp $FILENAME $o_ssh[2]
203    fi
204
205    if [[ "$o_mail" != "" ]]; then
206       check4progs mutt || { echo "Sorry, mutt not available for sending mail. Exiting.">&2 ; exit 1 ; }
207       recipient=$o_mail[2]
208       debug "send mail to $recipient"
209       echo "Created on $DATE on host $HOSTNAME running grml $GRML_VERSION" > $MAILFILE
210       mutt -s "configuration of $HOSTNAME ($DATE)" -a $FILENAME $recipient < $MAILFILE
211    fi
212 }
213 parse_options $*
214 # }}}
215
216 runit(){
217    if [[ $SAVE_HOME == "yes" ]]; then
218      debug "running save_home"
219      save_home
220      SETSAVE=1
221    fi
222
223    if [[ $SAVE_GRMLHOME == "yes" ]]; then
224      debug "running save_grmlhome"
225      save_grmlhome
226      SETSAVE=1
227    fi
228
229    if [[ $SAVE_ETC == "yes" ]] ; then
230      debug "running save_etc"
231      save_etc
232      SETSAVE=1
233    fi
234
235    if [[ $SAVE_CONFIGDIR == "yes" ]] ; then
236      debug "running save_configdir"
237      save_configdir
238      SETSAVE=1
239    fi
240
241    if [ -z $SETSAVE ] ; then
242      echo "Sorry, you did not select any configuration which should be saved. Exiting.">&2
243      exit 1
244    fi
245 }
246
247 # now run it
248   runit
249   create_config
250   bailout
251
252 ## END OF FILE #################################################################
253 # vim:foldmethod=marker