#!/bin/bash # ------- # File: llxcfg-archivevars # Description: LliureX variables archiving tool # Author: Luis Garcia Gisbert # # 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 program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA # -------- set -e PATH="/usr/sbin:/usr/bin:/sbin:/bin" # VARS ARCHIVE_PATH="/backup/llxcfg/archive/vars" ARCHIVE_MAIN="varset" ARCHIVE_OLD="archive" declare -a ARCHIVE_LIST # funcs # ----- error_message(){ echo -e $1 >&2 exit 1 } usage(){ SCRIPT_NAME="$(basename "$0")" error_message "${SCRIPT_NAME} VARSET {show|remove|test} [ -r REVISION ] [ VAR_1 [ ... VAR_N ]]\n\ ${SCRIPT_NAME} VARSET archive-list\n\ ${SCRIPT_NAME} VARSET {store|archive} VAR_1 [ ... VAR_N ]\n\ ${SCRIPT_NAME} VARSET {store-all|archive-all}" } revision_list(){ find "$ARCHIVE_PATH" -xtype f -name "${1}.${ARCHIVE_OLD}.*" |sort -u } # MAIN PROGRAM VARS_LIB_FILE="/usr/sbin/llxcfg-vars.sh" [ -e ${VARS_LIB_FILE} ] || error_message "Missing variable library file ${VARS_LIB_FILE}!!" . ${VARS_LIB_FILE} # Initial test llxcfg_vars_test_CONFDIRS || error_message "Variables directory path error !!" VARSET="$1" [ "$VARSET" ] || usage shift ARCHIVE_ACTION="$1" [ "$ARCHIVE_ACTION" ] || usage if ! echo "$VARSET" |grep -q "^[[:alnum:]_-]\+$" ; then error_message "\"$VARSET\" is not a valid VARSET name" fi shift if [ "$1" = "-r" ] ; then shift [ "$1" ] || usage NUMREV=$1 shift [ ! $NUMREV -eq 0 ] || error_message "\"$NUMREV\" is not a valid revision number" ARCHIVE_LIST=($(revision_list "$VARSET")) NUM_ARCHIVE="${#ARCHIVE_LIST[@]}" if [ $NUMREV -gt 0 ] ; then INDEX_NUM=$(($NUMREV - 1)) elif [ $NUMREV -lt 0 ] ; then INDEX_NUM=$(($NUM_ARCHIVE + $NUMREV)) fi if [ $INDEX_NUM -ge $NUM_ARCHIVE ] || [ $INDEX_NUM -lt 0 ]; then error_message "\"$NUMREV\" is not a valid revision number" fi VARSET_FILE=${ARCHIVE_LIST[$INDEX_NUM]} else VARSET_FILE="${ARCHIVE_PATH}/${VARSET}.${ARCHIVE_MAIN}" fi TIMESTAMP_FILE="${ARCHIVE_PATH}/${VARSET}.${ARCHIVE_OLD}.$(date +%Y%m%d-%H%M%S)" case "$ARCHIVE_ACTION" in store-all|archive-all) # force archive name VARSET_FILE="${ARCHIVE_PATH}/${VARSET}.${ARCHIVE_MAIN}" if [ "$ARCHIVE_ACTION" = "archive-all" -a -r "$VARSET_FILE" ] ; then mv "$VARSET_FILE" "$TIMESTAMP_FILE" fi llxcfg-listvars --values |llxcfg-install - "${VARSET_FILE}" ;; archive|store) [ "$1" ] || usage # force archive name VARSET_FILE="${ARCHIVE_PATH}/${VARSET}.${ARCHIVE_MAIN}" if [ "$ARCHIVE_ACTION" = "archive" -a -r "$VARSET_FILE" ] ; then mv "$VARSET_FILE" "$TIMESTAMP_FILE" fi llxcfg-showvars "$@" |llxcfg-install - "${VARSET_FILE}" ;; remove) rm -f "${VARSET_FILE}" ;; show) [ -r "${VARSET_FILE}" ] || error_message "There is not a VARSET named \"$VARSET\"" if [ "$1" ] ; then llxcfg_vars_show "${VARSET_FILE}" "$@" else # all vars llxcfg_vars_show "${VARSET_FILE}" $(llxcfg_vars_names "${VARSET_FILE}") fi ;; test) [ -r "${VARSET_FILE}" ] || error_message "There is not a VARSET named \"$VARSET\"" if [ -z "$1" ] ; then VAR_LIST="$(llxcfg_vars_names "${VARSET_FILE}")" else VAR_LIST="$@" fi # there is something to test ? [ "$VAR_LIST" ] || exit 0 # read current values eval "$(llxcfg-showvars "$VAR_LIST")" for varname in $VAR_LIST ; do # preserve current CURRENT_VALUE="${!varname}" # read old value (if any) eval "$(llxcfg_vars_show "${VARSET_FILE}" $varname)" [ "${!varname}" = "$CURRENT_VALUE" ] || exit 1 done ;; archive-list) i=1 for r in $(revision_list "$VARSET" |sed -e "s%^.*\.%%;") ; do echo -e "$i:\t$r" i=$(($i + 1)) done ;; *) usage ;; esac exit 0