Fix to set VNC password for correct user
[grml-autoconfig.git] / bin / restore-config
1 #!/bin/zsh
2 # Filename:      restore-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 # Latest change: Son Mai 13 11:12:38 CEST 2007 [mika]
8 ################################################################################
9
10 # some zsh-stuff {{{
11   autoload colors ; colors
12   [ -r /etc/grml/sh-lib ] && . /etc/grml/sh-lib
13 # }}}
14
15 # set variables  {{{
16   LANG=C
17   LC_ALL=C
18   PROGRAMNAME=${0##*/}
19   TMPDIR=/tmp
20
21 # functions {{{
22 debug(){
23   if [[ $DEBUG -gt 0 ]] ; then
24     echo "debug: $*"
25   fi
26 # setopt xtrace
27 # set -x
28 }
29
30 bailout(){
31   rm -f "$TMP_FILELIST"
32 }
33
34 trap bailout 1 2 3 15
35 # }}}
36
37 # usage information {{{
38 usage()
39 {
40   print 1>&2 "
41 $bg[black]$fg[green]${boldcolor}${PROGRAMNAME} - restore configuration of grml system${reset_color}
42
43 $bg[black]$fg[blue]${boldcolor}Usage:${reset_color}
44   $PROGRAMNAME [-target_options] <configuration_file>
45
46 $bg[black]$fg[blue]${boldcolor}Target options:${reset_color}
47   -home                         extract hidden files from \$HOME (\$HOME/.*)
48   -grmlhome                     store hidden files from \$HOME (\$HOME/.*) of user grml [use as user root]
49   -etc                          extract modified files from /etc
50   -configdir                    extract \$HOME/config
51
52   Default: restore/extract complete archive.
53
54   Notice: it is also possible to use environment variables:
55           \$RESTORE_HOME, \$RESTORE_ETC, \$RESTORE_CONFIGDIR and \$RESTORE_ALL
56
57 $bg[black]$fg[blue]${boldcolor}Usage examples:${reset_color}
58   $PROGRAMNAME -home foo_bar_config.tbz  => restore configuration from file foo_bar_config.tbz
59   $PROGRAMNAME config.tbz                => restore configuration from file config.tbz
60
61 More information on restore-config can be found in the manual page: man restore-config
62
63 See also: save-config(1), bootoptions: myconfig=/dev/ice, extract=PATH,
64           netconfig=server.tld/path/to/config.tbz
65
66 Report bugs, send wishes and feedback to the grml team:
67 http://grml.org/bugs/ - contact (at) grml.org
68 "
69 }
70 # }}}
71
72
73 # extract configuration file {{{
74 restore_all(){
75   echo "Trying to extract $FILENAME"
76   ( cd / && unp $FILENAME )
77 }
78
79 restore_home(){
80   echo "Trying to extract $FILENAME in $HOME"
81   ( cd $HOME && unp $FILENAME -- -x $HOME )
82 }
83
84 restore_grmlhome(){
85   echo "Trying to extract $FILENAME in /home/grml"
86   ( cd /home/grml/ && unp $FILENAME -- -x /home/grml )
87 }
88
89 restore_etc(){
90   echo "Trying to extract $FILENAME in /etc"
91   ( cd /etc && unp $FILENAME -- -x /etc )
92 }
93
94 restore_config(){
95   echo "Trying to extract $FILENAME in $HOME/config"
96   ( cd $HOME && unp $FILENAME -- -x $HOME/config )
97 }
98 # }}}
99
100
101 # commandline parsing {{{
102 parse_options()
103 {
104    zparseopts -K -- help=o_help file:=o_file home=o_home etc=o_etc \
105                     configdir=o_configdir all=o_all
106
107    if [[ "$#" == 0 || "$o_help" != "" || "$1" == '-h' || "$1" == '--help' ]]; then
108       usage ; exit
109    fi
110
111    if [[ "$1" == "" ]]; then
112      echo "Error: No filename provided." ; exit 1
113    else
114      eval FILENAME=\${$#}
115      FILENAME=`readlink -f $FILENAME`
116    fi
117
118    if [[ "$o_home" != "" ]]; then
119       echo "debug: home is set"
120       RESTORE_HOME="yes"
121    fi
122
123    if [[ "$o_grmlhome" != "" ]]; then
124       echo "debug: grmlhome is set"
125       RESTORE_GRMLHOME="yes"
126    fi
127
128    if [[ "$o_etc" != "" ]]; then
129       echo "debug: etc is set"
130       RESTORE_ETC="yes"
131    fi
132
133    if [[ "$o_configdir" != "" ]]; then
134       echo "debug: configdir is set"
135       RESTORE_CONFIGDIR="yes"
136    fi
137
138 }
139 parse_options $*
140 # }}}
141
142 runit(){
143    if [[ $RESTORE_HOME == "yes" ]]; then
144      debug "running restore_home"
145      restore_home
146      RESTORE_SET=1
147    fi
148    if [[ $RESTORE_GRMLHOME == "yes" ]]; then
149      debug "running restore_grmlhome"
150      restore_grmlhome
151      RESTORE_SET=1
152    fi
153    if [[ $RESTORE_ETC == "yes" ]] ; then
154      debug "running restore_etc"
155      restore_etc
156      RESTORE_SET=1
157    fi
158    if [[ $RESTORE_CONFIGDIR == "yes" ]] ; then
159      debug "running restore_configdir"
160      restore_config
161      RESTORE_SET=1
162    fi
163    debug "FILENAME = $FILENAME"
164    if [ -z $RESTORE_SET ] ; then
165      debug "running restore all"
166      restore_all
167    fi
168 }
169
170 # now run it
171   runit
172   bailout
173
174 ## END OF FILE #################################################################
175 # vim:foldmethod=marker