summaryrefslogtreecommitdiff
path: root/scripts/inc
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2011-11-06 16:01:16 +0000
committerLudovic Pouzenc <ludovic@pouzenc.fr>2011-11-06 16:01:16 +0000
commite3ad43b47e739fc33cf62acf305f627b2d357b80 (patch)
tree9e1fcb7462f9b43ad27c9c76b9ed0c0b6a0aba42 /scripts/inc
parenteabaac8881ef208bdb2eccf67c9a78ea69c42128 (diff)
download2011-bkp-e3ad43b47e739fc33cf62acf305f627b2d357b80.tar.gz
2011-bkp-e3ad43b47e739fc33cf62acf305f627b2d357b80.tar.bz2
2011-bkp-e3ad43b47e739fc33cf62acf305f627b2d357b80.zip
Import initial bkp.sh et lst.sh avec leur conf et la clef publique ssh
git-svn-id: file:///var/svn/2011-bkp/trunk@3 472aae81-79e5-4f1d-a134-eb2ed09d1a98
Diffstat (limited to 'scripts/inc')
-rwxr-xr-xscripts/inc/functions.sh131
1 files changed, 131 insertions, 0 deletions
diff --git a/scripts/inc/functions.sh b/scripts/inc/functions.sh
new file mode 100755
index 0000000..0fdc8a1
--- /dev/null
+++ b/scripts/inc/functions.sh
@@ -0,0 +1,131 @@
+#!/bin/bash
+
+function parse_args() {
+ if [ $# -lt 1 ]
+ then return 1
+ fi
+
+ arg_source_machine="$1"
+ return 0
+}
+
+function print_usage() {
+ echo "Usage : $0 <source_machine>"
+}
+
+export LOG_NOTICE="notice"
+export LOG_WARN="warning"
+export LOG_ERR="error"
+function log() {
+ {
+ echo -n "$(date --rfc-3339 seconds) $(hostname) $0: $2: <$(eval echo \$$1)> "
+ shift 2
+ echo "$*"
+ } | tee -a ./bkp.log
+}
+
+function run_or_die() {
+ local l_exit_value_on_failure="$1"
+ local l_func_to_call="$2"
+ shift 2
+
+ $l_func_to_call "$@"
+ res=$?
+ if [ $res -ne 0 ]
+ then log "$log_type" "$l_func_to_call" "$log_message"
+ exit $l_exit_value_on_failure
+ fi
+}
+
+function check_env() {
+ if [ $UID -ne 0 ]
+ then log LOG_WARN "This script is intended to be ran by root"
+ return 1
+ fi
+ return 0
+}
+
+#INI function from http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/
+cfg_parser ()
+{
+ ini="$(<$1)" # read the file
+ ini="${ini//[/\[}" # escape [
+ ini="${ini//]/\]}" # escape ]
+ IFS=$'\n' && ini=( ${ini} ) # convert to line-array
+ ini=( ${ini[*]//;*/} ) # remove comments with ;
+ ini=( ${ini[*]/\ =/=} ) # remove tabs before =
+ ini=( ${ini[*]/=\ /=} ) # remove tabs be =
+ ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around =
+ini=( ${ini[*]/#/cfg_} ) # lpo : Add cfg prefix for keys
+ ini=( ${ini[*]/#cfg_\\[/\}$'\n'cfg.section.} ) # set section prefix
+ ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
+ ini=( ${ini[*]/=/=\( } ) # convert item to array
+ ini=( ${ini[*]/%/ \)} ) # close array parenthesis
+ ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
+ ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
+ ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
+ ini[0]="" # remove first element
+ ini[${#ini[*]} + 1]='}' # add the last brace
+ eval "$(echo "${ini[*]}")" # eval the result
+}
+
+#function array_key_exists() {
+# local l_key="$1"
+# #Special behavior : pass as 2nd arg the keys of array with "${!array[@]}"
+# shift
+#
+# for k in "$@"
+# do
+# [ "x$l_key" == "x$k" ] && return 0
+# done
+#
+# return 1
+#}
+
+function load_conf() {
+ local l_conffile="$1"
+ local l_source_machine="$2"
+
+ cfg_parser "$l_conffile"
+ cfg.section.common
+ cfg.section.$l_source_machine
+
+# source "$l_conffile"
+
+# if array_key_exists "$l_source_machine" "${!CONF_SOURCE_POSSIBLE_ADDRESSES[@]}"
+# then cfg_source_possible_addresses=${CONF_SOURCE_POSSIBLE_ADDRESSES["$l_source_machine"]}
+# else log LOG_ERROR load_conf "No definition for CONF_SOURCE_POSSIBLE_ADDRESSES['$l_source_machine']"
+# return 1
+# fi
+
+ #TODO : check loaded values
+}
+
+function find_source_address() {
+ local l_oldifs="$IFS"
+ local l_hostname="$(hostname)"
+
+ if [ x"$l_hostname" == x"$arg_source_machine" ]
+ then runtime_source_address="" # Empty string means localhost
+ return 0
+ fi
+
+ runtime_source_address=""
+ IFS=", "
+ for a in $*
+ do
+ ping -w 1 -c 1 -q "$a" &>/dev/null
+ if [ $? -eq 0 ]
+ then runtime_source_address="$a"
+ break
+ fi
+ done
+ IFS="$l_oldifs"
+
+ if [ -z "$runtime_source_address" ]
+ then return 1
+ fi
+
+ return 0
+}
+