Add configuration file for script 'remote-copy_gateway-config.sh'.

This commit is contained in:
Christoph 2018-05-08 00:45:40 +02:00
parent 355d383fdf
commit 05fbd45b23
3 changed files with 497 additions and 30 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/BAK/* /BAK/*
*.swp *.swp
conf/*.conf

View File

@ -0,0 +1,36 @@
# ==========
# - Configuration file for script remote-copy_gateway-config.sh
# ==========
# - LOGGING
# -
# - Enables/Disables script output. Setting this value to 'true' is
# - only useful if NOT running in a terminal (i.e. as cronjob).
# -
# - If script is running in a terminal, script output is enabled and
# - cannot be disabled.
# -
# - Running this script in a
# -
# - Default value: false
# -
#LOGGING=false
# - OFFICE_BASE_DIR
# -
# - This is the base directory. For each Gateway/Network, there is a seperate
# - folder, which contains all the configuration files and directories.
# -
# - Default value: $HOME/Office_Networks
# -
#OFFICE_BASE_DIR="$HOME/Office_Networks"
# - REMOTE_USER
# -
# - This remote user hold all configuration file in a subdirectory of
# - his/her home directory (called <NAME>-config - i.e. B3-Bornim-config)
# -
# - Default value: chris
# -
#REMOTE_USER="chris"

View File

@ -1,15 +1,32 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function usage() { script_name="$(basename $(realpath $0))"
if [ -n "$1" ];then working_dir="$(dirname $(realpath $0))"
echo -e "\nError: $1"
fi
cat<<EOF conf_file="${working_dir}/conf/${script_name%%.*}.conf"
Usage: `basename $0` -h <remote host> -u <remote user> <network-name> LOCK_DIR="/tmp/$(basename $0).$$.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
Example: `basename $0` -h wf.oopen.de -u chris WF backup_date="$(date +%Y-%m-%d-%H%M)"
declare -a files_backuped_arr=()
declare -a dirs_backuped_arr=()
# ----------
# Base Function(s)
# ----------
usage() {
[[ -n "$1" ]] && error "$1"
if [[ $terminal ]] ; then
cat<<EOF
Usage: `basename $0` -h <remote host> [ -u <remote user> -d <base-dest.-directory> ] <network-name>
Example: `basename $0` -h wf.oopen.de -u chris -d /home/chris/devel/git/git.oopen.de/o.open/Office_Networks WF
network-name possible values are: network-name possible values are:
@ -38,16 +55,241 @@ Example: `basename $0` -h wf.oopen.de -u chris WF
WF WF
EOF EOF
exit fi
clean_up 1
} }
clean_up() {
# Perform program exit housekeeping
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n " $*\\c" 1>&2
else
echo -e -n " $*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ] $*"
else
echo -e " [ Fatal ] $*"
fi
echo ""
if $terminal ; then
echo -e " \033[1mScript terminated\033[m.."
else
echo -e " Script terminated.."
fi
echo ""
rm -rf $LOCK_DIR
exit 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ] $*"
else
echo " [ Error ] $*"
fi
echo ""
}
warn (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
else
echo " [ Warn ] $*"
fi
echo ""
fi
}
nfo (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
else
echo " [ Info ] $*"
fi
echo ""
fi
}
ok (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
else
echo " [ Ok ] $*"
fi
echo ""
fi
}
echo_done() {
if $terminal ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
fi
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
# - Backup file or directory
# -
backup() {
is_directory=false
if [[ -z "$1" ]] ; then
error "No file/directory for backup given!"
return
elif [[ -h "$1" ]] ; then
_source="$(realpath $1)"
warn "'$1' is a symlink to $_source."
elif [[ ! -d "$1" ]] && [[ ! -f "$1" ]] ; then
warn "'$1' not found. Backup skipped."
return
else
_source="$(realpath $1)"
fi
if [[ -d "$_source" ]]; then
is_directory=true
echononl "Backup directory '$_source' .."
else
echononl "Backup file '$_source' .."
fi
cp -a "$_source" "${_source}.$backup_date" > $log_file 2>&1
if [[ $? -eq 0 ]] ; then
echo_ok
if $is_directory ; then
dirs_backuped_arr+=("$_source")
else
files_backuped_arr+=("$_source")
fi
else
echo_failed
error "$(cat "$log_file")"
fi
}
# - Remove script generated backups, if source wasn't changed
# -
rm_unchanged_backup() {
if [[ ${#files_backuped_arr[@]} -gt 0 ]] ; then
for _file in "${files_backuped_arr[@]}" ; do
if $(diff "$_file" "${_file}.$backup_date" > /dev/null 2>&1) ; then
echononl "File '$(basename "${_file}")' wasn't changed.\n Delete the previous generated backup. .."
rm "${_file}.$backup_date" > "$log_file" 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
blank_line
fi
done
fi
if [[ ${#dirs_backuped_arr[@]} -gt 0 ]] ; then
for _dir in "${dirs_backuped_arr[@]}" ; do
if $(diff -Nur "$_dir" "${_dir}.$backup_date" > /dev/null 2>&1) ; then
echononl "Directory '$(basename "${_dir}")' wasn't changed.\n Delete the previous generated backup. .."
rm -rf "${_dir}.$backup_date" > "$log_file" 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
blank_line
fi
done
fi
}
manage_files() { manage_files() {
for file in $* ; do for file in $* ; do
if ssh -l $remote_user ${remote_host} [ -f ${_network}-config/$file ] ; then blank_line
scp -o User=$remote_user ${remote_host}:${_network}-config/$file . echononl "Get file '${_network}-config/$file' .."
ssh -l $remote_user ${remote_host} "rm ${_network}-config/$file" if ssh -l $REMOTE_USER ${REMOTE_HOST} [ -f ${_network}-config/$file ] ; then
scp -o User=$REMOTE_USER ${REMOTE_HOST}:${_network}-config/$file . > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
echononl "Remove remote file '${_network}-config/$file' .."
ssh -l $REMOTE_USER ${REMOTE_HOST} "rm ${_network}-config/$file" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
else
echo_failed
error "$(cat "$log_file")"
fi
else
echo_skipped
if [[ -f "${OFFICE_DIR}/$file" ]] ; then
echononl "Remove local file '$file' .."
rm "${OFFICE_DIR}/$file" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
fi
fi fi
done done
@ -55,30 +297,183 @@ manage_files() {
manage_archives() { manage_archives() {
for archive in $* ; do for archive in $* ; do
if ssh -l $remote_user ${remote_host} [ -f "${_network}-config/$archive" ]; then blank_line
scp -o User=$remote_user ${remote_host}:${_network}-config/$archive . echononl "Get archive '${_network}-config/$archive' .."
gunzip < $archive | tar -xpf - if ssh -l $REMOTE_USER ${REMOTE_HOST} [ -f "${_network}-config/$archive" ]; then
rm -f $archive scp -o User=$REMOTE_USER ${REMOTE_HOST}:${_network}-config/$archive . > $log_file 2>&1
ssh -l $remote_user ${remote_host} "rm ${_network}-config/$archive" if [[ $? -eq 0 ]]; then
echo_ok
echononl "Unpack archive '$archive' .."
gunzip < $archive | tar -xpf - > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
echononl "Remove downloaded archive '$archive' .."
rm -f "$archive" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
echononl "Remove remote archive '$archive' .."
ssh -l $REMOTE_USER ${REMOTE_HOST} "rm ${_network}-config/$archive" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
else
echo_failed
error "$(cat "$log_file")"
fi
else
echo_failed
error "$(cat "$log_file")"
fi
else
echo_skipped
if [[ "$archive" = "root-dot-opennvpndir_NONE-WF.tar.gz" ]] ; then
_dir=".openvpn "
elif [[ "$archive" = "etc_check_net_NONE-WF.tar.gz" ]] ; then
_dir="check_net"
else
archive_suffix="${archive%%.*}"
_tmp_name="${archive_suffix%_*}"
_dir="${_tmp_name##*_}"
fi
if [[ -d "${OFFICE_DIR}/$_dir" ]] ; then
echononl "Remove local directory '$_dir' .."
rm -rf "${OFFICE_DIR}/$_dir" > $log_file 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
fi
fi fi
done done
} }
while getopts h:u: opt ; do
# ----------
# - Jobhandling
# ----------
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
# -
trap clean_up SIGHUP SIGINT SIGTERM
# - Create lock directory '$LOCK_DIR"
#
mkdir "$LOCK_DIR"
# ----------
# - Some checks ..
# ----------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# -Is systemd supported on this system?
# -
systemd_supported=false
systemd=$(which systemd)
systemctl=$(which systemctl)
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
systemd_supported=true
fi
# - Print help?
# -
if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then
usage
fi
if [[ -z "$(which basename)" ]]; then
fatal 'It seems "basename" is not installed, but needed!'
fi
if [[ -z "$(which realpath)" ]]; then
fatal 'It seems "realpath" is not installed, but needed!'
fi
n Main Script
# ==========
# ----------
# - Headline
# ----------
if $terminal ; then
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
echo ""
fi
# ----------
# Read Configurations from $conf_file
# ----------
# - Give your default values here
# -
LOGGING=false
BATCH_MODE=false
DEFAULT_OFFICE_BASE_DIR="$HOME/Office_Networks"
DEFAULT_REMOTE_USER="chris"
echononl "Read configuration file '$(basename "$conf_file")' .."
if [[ -f "$conf_file" ]]; then
source "$conf_file"
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fi
else
echo_skipped
warn "No configuration file '$conf_file' present.\n
Loading default values.."
fi
# ----------
# - Read commandline parameter
# ----------
while getopts d:h:u: opt ; do
case $opt in case $opt in
h) remote_host="$OPTARG" h) REMOTE_HOST="$OPTARG"
;; ;;
u) remote_user="$OPTARG" u) REMOTE_USER="$OPTARG"
;;
d) OFFICE_BASE_DIR="$OPTARG"
;; ;;
*) usage *) usage
;; ;;
esac esac
done done
if [ -z "$remote_host" ]; then [[ -z "$REMOTE_USER" ]] && REMOTE_USER="$DEFAULT_REMOTE_USER"
usage "Remote host not given" [[ -z "$OFFICE_BASE_DIR" ]] && OFFICE_BASE_DIR="$DEFAULT_OFFICE_BASE_DIR"
fi
if [ -z "$remote_user" ]; then if [ -z "$REMOTE_HOST" ]; then
usage "Remote host not given" usage "Remote host not given"
fi fi
@ -87,12 +482,44 @@ shift `expr $OPTIND - 1`
[ $# -ne "1" ] && usage "wrong number of arguments" [ $# -ne "1" ] && usage "wrong number of arguments"
_network=$1 _network=$1
OFFICE_DIR=${OFFICE_BASE_DIR}/${_network}
office_base_dir=$HOME/Office_Networks if [[ "$_network" != "NONE-WF" ]] \
office_dir=${office_base_dir}/${_network} && [[ "$_network" != "NONE-CKUBU" ]] \
&& [[ "$_network" != "123" ]] \
&& [[ "$_network" != "AK" ]] \
&& [[ "$_network" != "AKB" ]] \
&& [[ "$_network" != ""ANW-URB ]] \
&& [[ "$_network" != "ANW-KM" ]] \
&& [[ "$_network" != "B3-Bornim" ]] \
&& [[ "$_network" != "CKUBU" ]] \
&& [[ "$_network" != "FLR-BRB" ]] \
&& [[ "$_network" != "GA-AL" ]] \
&& [[ "$_network" != "GA-Ersatz" ]] \
&& [[ "$_network" != "GA-NH" ]] \
&& [[ "$_network" != "GA-Schloss" ]] \
&& [[ "$_network" != "JONAS" ]] \
&& [[ "$_network" != "Kanzlei-Kiel" ]] \
&& [[ "$_network" != "MBR" ]] \
&& [[ "$_network" != "OOLM" ]] \
&& [[ "$_network" != "OPP" ]] \
&& [[ "$_network" != "ReachOut" ]] \
&& [[ "$_network" != "SPR-BE" ]] \
&& [[ "$_network" != "WF" ]] ;then
usage "Wrong network '$_network'!"
fi
mkdir -p $office_dir
cd $office_dir if $terminal ; then
echo ""
echo ""
echo -e " \033[1mMain part of script ..\033[m"
echo ""
fi
mkdir -p $OFFICE_DIR
cd $OFFICE_DIR
# - Nameserver Bind # - Nameserver Bind
# - /etc/bind/* # - /etc/bind/*
@ -245,9 +672,12 @@ manage_files $files
cat <<EOF > $office_dir/README.txt cat <<EOF > $OFFICE_DIR/README.txt
-------
Notice: Notice:
-------
You have to change some configuration files becaus the because You have to change some configuration files becaus the because
the configuration of network interfaces must not be equal. the configuration of network interfaces must not be equal.
@ -269,11 +699,11 @@ Notice:
interfaces.${_network}: see above interfaces.${_network}: see above
default_isc-dhcp-server.${_network} default_isc-dhcp-server.${_network}
ipt-firewall.${_network}: LAN device (mostly $local_if_1) = eth1 ipt-firewall.${_network}: LAN device (mostly $local_if_1) = eth1
second LAN WLAN or what ever (if present) = eth0 second LAN WLAN or what ever (if present) = eth0
EOF EOF
cat $office_dir/README.txt cat $OFFICE_DIR/README.txt
#rm /tmp/README$$ #rm /tmp/README$$