Debian Codegraph @

lib/fai-savelog

raw
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # fai-save-log -- save log files from fai to a local or remote location
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2002-2023 by Thomas Lange, [email protected]
9 # Universitaet zu Koeln
10 #
11 #*********************************************************************
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
21 #
22 # A copy of the GNU General Public License is available as
23 # `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
24 # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You
25 # can also obtain it by writing to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #*********************************************************************
28
29 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
30 save_log_local() {
31
32
33 # can be an extern script
34 # save log files on local disk
35 local logbase=${LOGBASE:-"$FAI_ROOT/var/log/fai"}
36 local thislog=$logbase/$HOSTNAME/$FAI_ACTION-$fai_rundate
37 find $LOGDIR -xdev -size 0 -type f -exec rm {} \;
38 # create the symbolic links forcefully; in case of dirinstalls or
39 # softupdates they already exist as the fai script creates them together
40 # with the directory $LOGDIR
41 ln -snf $FAI_ACTION-$fai_rundate $LOGDIR/../last-$FAI_ACTION
42 ln -snf $FAI_ACTION-$fai_rundate $LOGDIR/../last
43
44 [ -d "$thislog" ] && return # nothing to do, if directory already exists
45
46 mkdir -p $thislog
47 cp -a $LOGDIR/* $thislog
48 chown root:adm $thislog
49 chmod 0750 $thislog
50 ln -snf $HOSTNAME $logbase/localhost
51 ln -snf $FAI_ACTION-$fai_rundate $logbase/$HOSTNAME/last-$FAI_ACTION
52 ln -snf $FAI_ACTION-$fai_rundate $logbase/$HOSTNAME/last
53 }
54 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55 save_log_remote_shell() {
56
57 local thislogdir
58 if [ -n "$LOGREMOTEDIR" ]; then
59 thislogdir=$LOGREMOTEDIR/$HOSTNAME
60 else
61 thislogdir=$HOSTNAME
62 fi
63
64 local thislog
65 thislog=$thislogdir/$FAI_ACTION-$fai_rundate
66
67 echo "Save log files via $remotesh to $LOGUSER@$LOGSERVER:$thislog"
68 find $LOGDIR -xdev -size 0 -type f -exec rm {} \;
69 $remotesh -l $LOGUSER $LOGSERVER " \
70 mkdir -p $thislog ;\
71 cd $thislogdir ;\
72 ln -snf $FAI_ACTION-$fai_rundate last-$FAI_ACTION ;\
73 ln -snf $FAI_ACTION-$fai_rundate last"
74
75 $remotecp -pr $LOGDIR/* $LOGUSER@$LOGSERVER:$thislog
76 error=$?
77 }
78 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79 save_log_remote() {
80
81 # save log files to $LOGUSER/$HOSTNAME/.. on $LOGSERVER
82 # also create a link last-$FAI_ACTION the the directory of the
83 # last action. The name of the log directory contains date and
84 # time of the action performed
85
86 if [ -z "$LOGUSER" ]; then
87 echo "\$LOGUSER is undefined. Not saving log files to remote."
88 return
89 fi
90 # LOGSERVER is overridable until now
91 : ${LOGSERVER:=$SERVER}
92 if [ -z "$LOGSERVER" ]; then
93 echo "\$LOGSERVER is undefined. Not saving log files to remote."
94 return
95 fi
96
97 case "$FAI_LOGPROTO" in
98 ftp)
99 fai-savelog-ftp ;;
100 none)
101 echo "Don't save logs with remote method, only local storage." ;;
102 ssh)
103 export remotesh=ssh
104 export remotecp=scp
105 save_log_remote_shell ;;
106 rsh)
107 export remotesh=rsh
108 export remotecp=rcp
109 save_log_remote_shell ;;
110 *)
111 echo "ERROR: Unknown value for \$FAI_LOGPROTO." ;;
112 esac
113 }
114 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115 usage() {
116
117 cat <<EOF
118 fai-savelog, save log files from fai to a local or remote location
119
120 Copyright (C) 2002-2023 by Thomas Lange
121
122 Usage: fai-savelog [OPTION]
123
124 -r Save log files to \$LOGSERVER using rcp or scp
125 -l [LOGBASE] Save log files to a local directory. If LOGBASE is not provided, use \$FAI_ROOT/var/log/fai
126
127 EOF
128 exit 0
129 }
130 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131
132 [ "$1" ] || usage
133
134 : ${FAI_ACTION:=noaction}
135 while getopts lr opt ; do
136 case "$opt" in
137 l) LOGBASE=$2 save_log_local ;;
138 r) save_log_remote ;;
139 *) usage ;;
140 esac
141 done
142
143 exit $error