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
19 [[ $UID != 0 ]] && runas='sudo' # important for /etc
21 if [ -d /lib/live/mount/overlay ] ; then # since around December 2012
22 CHANGE_DIR='/lib/live/mount/overlay'
23 elif [ -d /live/overlay ] ; then # until around December 2012
24 CHANGE_DIR='/live/overlay'
25 elif [ -d /live/image ] ; then # old version
26 CHANGE_DIR='/live/image'
28 echo "Error: no overlay directories found (like /lib/live/mount/overlay or /live/overlay)." >&2
32 if [ -d /live/rofs ] ; then
33 ORIG_DIR="$(find /live/rofs/ -maxdepth 1 -name \*.squashfs | head -1)"
38 check4progs mutt &>/dev/null || echo "Warning, mutt not available for mail handling.">&2
39 check4progs tar || { echo "Sorry, can't continue. Exiting.">&2 ; bailout ; exit 1 }
41 CONFIG=/etc/grml/saveconfig
42 [ -r "$CONFIG" ] && . $CONFIG
47 GRML_VERSION=$(awk '{print $1}' /etc/grml_version 2>/dev/null || print "not a grml system")
51 MAILFILE="${TMPDIR}/mail.txt"
53 [ -n "$FILELIST" ] || FILELIST=$(mktemp $TMPDIR/filelist.XXXXXX)
58 if [[ $DEBUG -gt 0 ]] ; then
74 for i in `(cd "$1"; find . -type f 2>/dev/null | sed 's,^\./,,g' | grep -v ' ' )`; do
75 cmp -s "$1/$i" "$2/$i" || echo "$1/$i"
77 elif [ -e "$1" ]; then
78 cmp -s "$1" "$2" || echo "$1"
83 # usage information {{{
87 $bg[black]$fg[green]${boldcolor}${PROGRAMNAME} - save configuration of grml system${reset_color}
89 $bg[black]$fg[blue]${boldcolor}Usage:${reset_color}
90 $PROGRAMNAME [-target_options] -{all,home,etc,configdir}
92 $bg[black]$fg[blue]${boldcolor}Target options:${reset_color}
93 -ssh user@host:/path/to/file copy configuration via ssh/scp to remote host
94 -mail <recipient> send configuration via mail
95 -file foo_bar_config.tbz save configuration in specified file
97 Notice: if no option is specified the default is assumed:
98 create file config.tbz in current directory
100 $bg[black]$fg[blue]${boldcolor}Files-to-store options:${reset_color}
101 -home store hidden files from \$HOME (\$HOME/.*)
102 -grmlhome store hidden files from \$HOME (\$HOME/.*) of user grml [use as user root]
103 -etc store modified files from /etc
104 -configdir store \$HOME/config
105 -all store all configuration files (:= -home, -configdir and -etc)
107 Notice: it is also possible to use environment variables:
108 \$SAVE_HOME, \$SAVE_GRMLHOME, \$SAVE_ETC, \$SAVE_CONFIGDIR and \$SAVE_ALL
110 $bg[black]$fg[blue]${boldcolor}Usage examples:${reset_color}
111 $PROGRAMNAME -all => store all configuration files in config.tbz in current dir
112 $PROGRAMNAME -home -mail devnull@grml.org => store \$HOME/.* in config.tbz and send it via mail
113 $PROGRAMNAME -etc -ssh devnull@grml.org:/path/ => store /etc in config.tbz and scp it to specified host
114 $PROGRAMNAME -all -file foo.tbz => store all configuration files in foo.tbz
115 SAVE_ALL=yes $PROGRAMNAME -file /path/foo.tbz => store all configuration files in /path/foo.tbz
117 More information on save-config can be found in the manual page: man save-config
119 See also: restore-config(1), bootoptions: myconfig=/dev/ice, extract=PATH,
120 netconfig=server.tld/path/to/config.tbz
122 Report bugs, send wishes and feedback to the grml team:
123 http://grml.org/bugs/ - contact (at) grml.org
128 # what do we want to store? {{{
131 for i in $HOME/.* ; do findchanged "$i" /etc/skel/$(basename "$i"); done >> $FILELIST
132 debug "debug: $FILELIST"
136 debug "save grmlhome"
137 if [ -d /home/grml/ ] ; then
138 for i in /home/grml/.* ; do findchanged "$i" /etc/skel/$(basename "$i"); done >> $FILELIST
140 debug "debug: $FILELIST"
145 if [ -n "$NEWLAYOUT" ] ; then
146 $runas find "${CHANGE_DIR}/etc" | sed -e "s#${CHANGE_DIR}## ; /etc$/d" >> $FILELIST
148 $runas findchanged /etc "${ORIG_DIR}/etc" >> $FILELIST
153 debug "save configdir"
154 if [ -d $HOME/config ] ; then
155 ls $HOME/config/* >> $FILELIST 2>/dev/null
156 ls $HOME/config/.* >> $FILELIST 2>/dev/null
161 # create configuration file {{{
163 if ! [ -r "$FILELIST" ]; then
164 echo "Filelist $FILELIST could not be read." >&2
165 echo "Error when generating $FILENAME." >&2
168 # GNU tar sucks so much, really. Avoid the "file changed as we read it":
169 tar cf /dev/null /etc
170 # now really execute the according tar command:
171 if BZIP2=-9 $runas tar -T - -cpPjf "$FILENAME" <"$FILELIST" ; then
172 echo "Successfully stored configuration in file $FILENAME"
174 echo "Error when generating $FILENAME." >&2
181 # commandline parsing {{{
184 zparseopts -K -- help=o_help mail:=o_mail \
185 file:=o_file home=o_home grmlhome=o_grmlhome etc=o_etc \
186 configdir=o_configdir all=o_all ssh:=o_ssh
188 if [[ "$#" == 0 || "$o_help" != "" || "$1" == '-h' || "$1" == '--help' ]]; then
192 if [[ "$o_file" != "" ]]; then
193 FILENAME="$o_file[2]"
195 FILENAME="config.tbz"
198 if [[ "$o_home" != "" ]]; then
203 if [[ "$o_grmlhome" != "" ]]; then
204 debug "grmlhome is set"
208 if [[ "$o_etc" != "" ]]; then
213 if [[ "$o_configdir" != "" ]]; then
214 debug "configdir is set"
218 if [[ "$o_all" != "" ]]; then
219 debug "home, grmlhome, etc and configdir are set"
226 if [[ "$o_ssh" != "" ]]; then
227 debug "scp $FILENAME $o_ssh[2]"
228 scp $FILENAME $o_ssh[2]
231 if [[ "$o_mail" != "" ]]; then
232 check4progs mutt || { echo "Sorry, mutt not available for sending mail. Exiting.">&2 ; exit 1 }
234 debug "send mail to $recipient"
235 echo "Created on $DATE on host $HOSTNAME running grml $GRML_VERSION" > $MAILFILE
236 mutt -s "configuration of $HOSTNAME ($DATE)" -a $FILENAME $recipient < $MAILFILE
242 # execution wrapper {{{
244 if [[ $SAVE_HOME == "yes" ]]; then
245 debug "running save_home"
249 if [[ $SAVE_GRMLHOME == "yes" ]]; then
250 debug "running save_grmlhome"
254 if [[ $SAVE_ETC == "yes" ]] ; then
255 debug "running save_etc"
259 if [[ $SAVE_CONFIGDIR == "yes" ]] ; then
260 debug "running save_configdir"
264 if [ -z $SETSAVE ] ; then
265 echo "Sorry, you did not select any configuration which should be saved. Exiting."
276 ## END OF FILE #################################################################
277 # vim:foldmethod=marker