BFO2LSDF.sh 5.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh
# This is <BFO2LSDF.sh>
# ----------------------------------------------------------------------------
#
# 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
# 
# Copyright (c) 2012 by Daniel Armbruster (BFO Schiltach) 
# 
# Purpose: Backup BFO data to LSDF remotely.
# 
# REVISIONS and CHANGES 
Daniel Armbruster's avatar
Daniel Armbruster committed
24
25
#     05/08/2012   V1.0   Daniel Armbruster
#     10/09/2012   V1.1   set bandwidth limit for rsync
26
#     05/02/2013   V1.2   print rsync EXIT CODE to log message
27
#     03/07/2014   V1.4   add DL1 data directory
28
29
#     07/07/2014   V1.5   - systematically missed rsync exit status
#                         - re-establish VPN tunnel for each directory
30
31
#     15/05/2016   V1.6   add subdirectory for long term archive of
#                         experiments
32
33
34
#     02/09/2016   V1.7   make program fit for bromo:
#                         - support dry-run
#                         - use openvpn instead of vpnc
35
36
37
38
39
# 
# ============================================================================
#

TUN_INTERFACE=tun0
Daniel Armbruster's avatar
Daniel Armbruster committed
40
# approx. 80% of 256 kBit/second
41
BWLIMIT=20
42
EXCLUDE="--exclude='.lock'"
43
44
DRYRUN=
DRYRUN=--dry-run
45
BFO_BASEDIR=/media/BFO_archive
46
47
#LSDF_BASEDIR=\
#gpi-lsdf-seis-0001@gpilsdf.gpi.uni-karlsruhe.de:/home/gpi-lsdf-seis-0001/LSDF/SEIS/projects/BFO 
48
LSDF_BASEDIR=\
49
gpi-lsdf-seis-0001@gpilsdf.gpi.uni-karlsruhe.de:LSDF/SEIS/projects/BFO 
50
51
# NOTE: If adding an additional data directory an exclude pattern must be added,
# too. Add an empty string if no data has to be excluded.
52
DATADIRS=('seedlink/' 'DL1/' 'SG/UIPC/' 'ExperimentsLTA/')
Daniel Armbruster's avatar
Daniel Armbruster committed
53
54
55
56
EXCLUDE_PATTERNS=(\
"--exclude-from=/home/csback/csback.config/BFO2LSDF.sl.exclude" '')
#DATADIRS=('SG/UIPC/')
#EXCLUDE_PATTERNS=('')
57
58
59
60
61
62
63
64
65

# programs
IP=/bin/ip
LOGGER=/bin/logger
NETSTAT=/bin/netstat
JNC=/usr/local/bin/jnc
GREP=/usr/bin/grep
RSYNC=/usr/bin/rsync

66
67
68
# logger
LOGINFO="(BFO2LSDF) INFO:"
LOGERROR="(BFO2LSDF) ERROR:"
69
70

# check for further active processes
71
if pidof -x $(basename $0) > /dev/null
72
then
73
  for p in $(pidof -x $(basename $0)) 
74
75
76
  do
    if [ $p -ne $$ ]
    then
77
78
      ${LOGGER} -i -p user.err "${LOGERROR}  $(basename $0) already running \
with PID $p."
79
80
81
82
83
84
85
86
      exit 2
    fi
  done
fi

# keychain related
source $HOME/.keychain/$HOSTNAME-sh

87
SSHCMD="/usr/bin/ssh -i $HOME/.ssh/BFOLSDFoper"
88
# transfer data
Daniel Armbruster's avatar
Daniel Armbruster committed
89
for i in ${!DATADIRS[*]}
90
do
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  # establish VPN connection to KIT
  ${IP} addr show ${TUN_INTERFACE} > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    tunIPs=$(${IP} addr show ${TUN_INTERFACE} | ${GREP} -e "inet" | \
      awk '{print $2}')
    ${LOGGER} -i -p user.info "${LOGINFO}  VPN connection already had been \
      established using IP ${tunIPs}."
  else
    # start juniper network client
    ${JNC} --nox kit > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      ${LOGGER} -i -p user.err "${LOGERROR}  Could not establish VPN \
        connection."
      exit 2
    else
      tunIPs=$(${IP} addr show ${TUN_INTERFACE} | ${GREP} -e "inet" | \
        awk '{print $2}')
      ${LOGGER} -i -p user.info "${LOGINFO}  VPN connection successfully \
        established using IP ${tunIPs}."
    fi
  fi

115
116
117
118
119
120
  if test "$DRYRUN" = "--dry-run"
  then
    ${LOGGER} -i -p user.info "${LOGINFO} \
      dry-run of copy operation (i.e. files are not actually copied)"
  fi

121
  # copy data (compressed)
thomas.forbriger's avatar
thomas.forbriger committed
122
  ${LOGGER} -i -p user.info "${LOGINFO}\
123
    ${RSYNC} -v -z -a --protocol=29 --bwlimit ${BWLIMIT} ${EXCLUDE} \
124
    -e \"${SSHCMD}\" $DRYRUN \
125
    --backup --suffix=$(date +'%Y%m%d%H%M') \
thomas.forbriger's avatar
thomas.forbriger committed
126
127
    ${EXCLUDE_PATTERNS[$i]} ${BFO_BASEDIR}/${DATADIRS[$i]} \
    ${LSDF_BASEDIR}/${DATADIRS[$i]}"
128
129
  set -o pipefail
  ${RSYNC} -v -z -a --protocol=29 --bwlimit ${BWLIMIT} ${EXCLUDE} \
130
    -e "${SSHCMD}" $DRYRUN \
131
    --backup --suffix=$(date +'%Y%m%d%H%M') \
Daniel Armbruster's avatar
Daniel Armbruster committed
132
    ${EXCLUDE_PATTERNS[$i]} ${BFO_BASEDIR}/${DATADIRS[$i]} \
133
134
    ${LSDF_BASEDIR}/${DATADIRS[$i]} \
      |& ${LOGGER} -i -p user.info -t "csback ${LOGINFO}" -s
135
136

  # write status to logfile
137
  RSYNCSTATUS=$?
138
  if [ $RSYNCSTATUS -eq 0 ]
139
  then
140
    ${LOGGER} -i -p user.info "${LOGINFO}  Copied data \
141
142
143
      '${BFO_BASEDIR}/${DATADIRS[$i]}' \
      successfully to LSDF (rsync EXIT CODE: \
      ${RSYNCSTATUS})."
144
  else
145
    ${LOGGER} -i -p user.err "${LOGERROR}  While copying data \
146
147
      '${BFO_BASEDIR}/${DATADIRS[$i]}' to LSDF (rsync EXIT CODE: \
      ${RSYNCSTATUS})."
148
149
150
151
152
153
154
  fi
done

# close VPN connection
${JNC} stop > /dev/null 2>&1
if [ $? -eq 0 ]
then
155
  ${LOGGER} -i -p user.info "${LOGINFO}  VPN connection successfully closed."
156
else
157
  ${LOGGER} -i -p user.err "${LOGERROR}  While closing VNP connection."
158
159
160
fi

# ----- END OF BFO2LSDF.sh -----