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 ################################################################################
10 autoload colors ; colors
13 . /etc/grml/script-functions
18 # old linuxrc version:
19 [ -d /cdrom ] && OLDLAYOUT=1 || OLDLAYOUT=''
20 # new initramfs layout:
21 [ -d /live/image ] && NEWLAYOUT=1 || NEWLAYOUT=''
25 [[ $(id -u) != 0 ]] && runas='sudo' # important for /etc
27 check4progs tar || { echo "Sorry, can't continue. Exiting.">&2 ; exit 1 }
29 CONFIG=/etc/grml/saveconfig
30 [ -r "$CONFIG" ] && . $CONFIG
35 GRML_VERSION=$(awk '{print $1}' /etc/grml_version 2>/dev/null || print "not a grml system")
39 MAILFILE="$TMPDIR/mail.txt"
41 [ -n "$FILELIST" ] || FILELIST=$(mktemp $TMPDIR/filelist.XXXXXX)
46 if [[ $DEBUG -gt 0 ]] ; then
54 [ -n "$1" ] || return 1
57 for i in $(cd "$1"; find . -type f 2>/dev/null | sed 's,^\./,,g' | grep -v ' '); do
58 cmp -s "$1/$i" "$2/$i" || echo "$1/$i"
60 elif [ -e "$1" ]; then
61 cmp -s "$1" "$2" || echo "$1"
73 # usage information {{{
77 $bg[black]$fg[green]${boldcolor}${PROGRAMNAME} - save configuration of grml system${reset_color}
79 $bg[black]$fg[blue]${boldcolor}Usage:${reset_color}
80 $PROGRAMNAME [-target_options] -{all,home,etc,configdir}
82 $bg[black]$fg[blue]${boldcolor}Target options:${reset_color}
83 -ssh user@host:/path/to/file copy configuration via ssh/scp to remote host
84 -mail <recipient> send configuration via mail
85 -file foo_bar_config.tbz save configuration in specified file
87 Notice: if no option is specified the default is assumed:
88 create file config.tbz in current directory
90 $bg[black]$fg[blue]${boldcolor}Files-to-store options:${reset_color}
91 -home store hidden files from \$HOME (\$HOME/.*)
92 -grmlhome store hidden files from \$HOME (\$HOME/.*) of user grml [use as user root]
93 -etc store modified files from /etc
94 -configdir store \$HOME/config
95 -all store all configuration files (:= -home, -configdir and -etc)
97 Notice: it is also possible to use environment variables:
98 \$SAVE_HOME, \$SAVE_GRMLHOME, \$SAVE_ETC, \$SAVE_CONFIGDIR and \$SAVE_ALL
100 $bg[black]$fg[blue]${boldcolor}Usage examples:${reset_color}
101 $PROGRAMNAME -all => store all configuration files in config.tbz in current dir
102 $PROGRAMNAME -home -mail devnull@grml.org => store \$HOME/.* in config.tbz and send it via mail
103 $PROGRAMNAME -etc -ssh devnull@grml.org:/path/ => store /etc in config.tbz and scp it to specified host
104 $PROGRAMNAME -all -file foo.tbz => store all configuration files in foo.tbz
105 SAVE_ALL=yes $PROGRAMNAME -file /path/foo.tbz => store all configuration files in /path/foo.tbz
107 More information on save-config can be found in the manual page: man save-config
109 See also: restore-config(1), bootoptions: myconfig=/dev/ice, extract=PATH,
110 netconfig=server.tld/path/to/config.tbz
112 Report bugs, send wishes and feedback to the grml team:
113 http://grml.org/bugs/ - contact (at) grml.org
118 # what do we want to store? {{{
121 for i in $HOME/.* ; do findchanged "$i" /etc/skel/$(basename "$i"); done >> $FILELIST
122 debug "debug: $FILELIST"
126 debug "save grmlhome"
127 if [ -d /home/grml/ ] ; then
128 for i in /home/grml/.* ; do findchanged "$i" /etc/skel/$(basename "$i"); done >> $FILELIST
130 debug "debug: $FILELIST"
135 if [ -n "$NEWLAYOUT" ] ; then
136 $runas find /live/cow/etc -type f -o -type l | sed -e 's#/live/cow## ; /etc$/d' >> $FILELIST
138 $runas findchanged /etc /GRML/etc >> $FILELIST
143 debug "save configdir"
144 if [ -d $HOME/config ] ; then
145 ls $HOME/config/* >> $FILELIST 2>/dev/null
146 ls $HOME/config/.* >> $FILELIST 2>/dev/null
150 # create configuration file {{{
152 if ! [ -r "$FILELIST" ]; then
153 echo "Sorry, filelist $FILELIST could not be read." >&2
154 echo "Error when generating $FILENAME." >&2
157 # GNU tar sucks so much, really. Avoid the "file changed as we read it":
158 tar cf /dev/null /etc 2>/dev/null
159 # now really execute the according tar command:
160 BZIP2=-9 $runas tar -T - -cpPjf "$FILENAME" <"$FILELIST" && \
161 echo "Successfully stored configuration in file $FILENAME" || \
162 echo "Error when generating $FILENAME." >&2
167 # commandline parsing {{{
170 zparseopts -K -- help=o_help mail:=o_mail \
171 file:=o_file home=o_home grmlhome=o_grmlhome etc=o_etc \
172 configdir=o_configdir all=o_all ssh:=o_ssh
174 if [[ "$#" == 0 || "$o_help" != "" || "$1" == '-h' || "$1" == '--help' ]]; then
178 if [[ "$o_file" != "" ]]; then
179 FILENAME="$o_file[2]"
181 FILENAME="config.tbz"
184 if [[ "$o_home" != "" ]]; then
189 if [[ "$o_grmlhome" != "" ]]; then
190 debug "grmlhome is set"
194 if [[ "$o_etc" != "" ]]; then
199 if [[ "$o_configdir" != "" ]]; then
200 debug "configdir is set"
204 if [[ "$o_all" != "" ]]; then
205 debug "home, grmlhome, etc and configdir are set"
212 if [[ "$o_ssh" != "" ]]; then
213 debug "scp $FILENAME $o_ssh[2]"
214 scp $FILENAME $o_ssh[2]
217 if [[ "$o_mail" != "" ]]; then
218 check4progs mutt || { echo "Sorry, mutt not available for sending mail. Exiting.">&2 ; exit 1 ; }
220 debug "send mail to $recipient"
221 echo "Created on $DATE on host $HOSTNAME running grml $GRML_VERSION" > $MAILFILE
222 mutt -s "configuration of $HOSTNAME ($DATE)" -a $FILENAME $recipient < $MAILFILE
229 if [[ $SAVE_HOME == "yes" ]]; then
230 debug "running save_home"
235 if [[ $SAVE_GRMLHOME == "yes" ]]; then
236 debug "running save_grmlhome"
241 if [[ $SAVE_ETC == "yes" ]] ; then
242 debug "running save_etc"
247 if [[ $SAVE_CONFIGDIR == "yes" ]] ; then
248 debug "running save_configdir"
253 if [ -z $SETSAVE ] ; then
254 echo "Sorry, you did not select any configuration which should be saved. Exiting.">&2
264 ## END OF FILE #################################################################
265 # vim:foldmethod=marker