#!/bin/sh #---------------------------------------------------------------------------- # /var/install/bin/config_shlib - # shell library for advanced configfile handling # # Creation: 2003-12-24 ap # Last Update: $Id$ # # Copyright (c) 2001-2007 Ansgar Puester, Ansgar.Puester(at)T-Online(dot)de # Copyright (c) 2008-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- # # This library is used by # # /var/install/bin/edit # /var/install/bin/template-backup-config and all it's links # /var/install/bin/template-restore-bconf and all it's links # /var/install/bin/template-restore-dconf and all it's links # /var/install/bin/master-diff-config and all it's links # /var/install/bin/backup-file # # include eislib . /var/install/include/eislib defaultd=/etc/default.d configd=/etc/config.d backupd=/etc/backup.d fmask='*[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]' #debug='true' #trace='true' gotoyx () { echo -e "\033[$1;$2H\c"; } # # get_date # ======== # # Parameters: $1 name of file in backup directory # /. # may contain . # # Returns: # get_date() { old_ifs="$IFS" IFS=. set $1 eval echo \$$# IFS="$old_ifs" } # # check_backupdir # =============== # # Parameters: $1 name of backup directory # # Returns: 0 success # 1 failure # check_backupdir() { dir=$1 if [ ! -d $dir ] then mkdir $dir [ $? != 0 ] && return 1 chmod og-rw $dir [ $? != 0 ] && return 1 fi return 0 } # # check_config_files # ================== # # Parameters: $1 package name # $2 'quiet' - disable screen output # # Returns: 0 success # 1 failure # check_config_files() { [ "$debug" = 'true' ] && mecho "call check_config_files: $*" [ "$trace" = 'true' ] && set -x package=$1 quiet=$2 # count files fcount=`find $backupd -maxdepth 1 -type f -name "$package.$fmask" 2>/dev/null | wc -l` fcount=`echo $fcount` # verify environment variable MAX_BACKUP_CONFIG _ccf_max_backup=`echo ${MAX_BACKUP_CONFIG} | sed 's/[^0-9]//g'` [ "${_ccf_max_backup}" = '' ] && _ccf_max_backup=10 # check number of files if [ "$fcount" -gt "${_ccf_max_backup}" ] then # number of files to remove rmcount=`expr $fcount - ${_ccf_max_backup}` count=0 # find files in sorted order { for FNAME in `find ${backupd} -maxdepth 1 -type f -name "${package}.${fmask}"` do # extract timestamp FDATE=`get_date ${FNAME}` # add timestamp as second field to in-transit output echo "${FNAME}:${FDATE}" done } | sort -t: -k2 | while read line do # remove obsolete files if [ "$count" -lt "$rmcount" ] then # extract filename from line file=`echo ${line} | cut -d: -f1` rm $file count=`expr $count + 1` else break fi done if [ "${quiet}" != "quiet" ] then mecho --info "Backup directory contained $fcount files for package $package ($rmcount removed)." fi fi } # # backup_config # ============= # # Parameters: $1 package name # $2 file to backup # $3 individal backup suffix # $4 'quiet' - disable screen output # # Returns: 0 backup O.K. # 1 failure # backup_config() { [ "$debug" = 'true' ] && mecho "call backup_config: $*" [ "$trace" = 'true' ] && set -x package=$1 file=$2 suffix=$3 quiet=$4 # check backup directory if ! check_backupdir $backupd then mecho --error "Error creating backup directory $backupd ..." return 1 fi if [ "$suffix" = "" ] then # timestamp to add to filename stamp=`date +%Y-%m-%d-%H-%M-%S` else # individual suffix to add to filename stamp=$suffix fi # do the backup if [ -f $file ] then cp -p $file $backupd/$package.$stamp chmod og-rw $backupd/$package.$stamp if [ "$quiet" != "quiet" ] then mecho --info "Configuration file was saved as $package.$stamp in $backupd." fi else if [ "$quiet" != "quiet" ] then mecho --warn "File $file not found - no backup done" fi fi # check if files have to be deleted check_config_files ${package} ${quiet} } # # activate_hint # ============= # activate_hint() { mecho --info "Configuration file $configd/$package was overwritten." mecho --info "You have to activate the configuration in order to" mecho --info "let the changes take effect." mecho --info "For most packages an 'Edit Configuration' without any" mecho --info "changes followed by an answer 'yes' to the question" mecho --info "'Activate the new configuration now (y/n)?' will do" mecho --info "this job." } # # select_file # =========== # # Variables used by select_file # MD_TITLE # MD_SUBTITLE # select_file() { files=$(find ${backupd} -maxdepth 1 -type f -name "${package}.${fmask}" | sort) if [ -n "${omit_file}" ] then files=$(echo "${files}" | grep -v "${omit_file}") fi # header_md if [ -n "${files}" ] then # find files in sorted order MD_FLAGS='--indent 12 --spread' MD_COLS='60*' MD_ROWS=0 export MD_TITLE export MD_FLAGS export MD_COLS for file in $(echo "${files}") do MD_ROWS=$(expr ${MD_ROWS} + 1) eval bfile_${MD_ROWS}='"${file}"' eval MD_${MD_ROWS}_1=\"'${file}'\" export MD_${MD_ROWS}_1 export MD_ROWS done MD_QUESTION='Select' export MD_QUESTION _ask_tmpfile=$(/bin/mktemp -t XXXXXXXXXXXXX) /var/install/bin/choose MD 1 > ${_ask_tmpfile} rc=${?} read answer < ${_ask_tmpfile} rm -f ${_ask_tmpfile} # if ask break, ask returned 255 if [ ${rc} = 255 ] then answer='' fi else if [ -z "${omit_file}" ] then mecho --warn "No files for $package found in $backupd." else mecho --warn "Only one file for $package found in $backupd." fi anykey answer='' fi } # --------------------------------------------------------------------------- # end #----------------------------------------------------------------------------