BFO2LSDF.sh 5.4 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
40
# VPN tunnel interface
TUN_INTERFACE=tap0
Daniel Armbruster's avatar
Daniel Armbruster committed
41
# approx. 80% of 256 kBit/second
42
BWLIMIT=20
43
EXCLUDE="--exclude='.lock'"
44
45
DRYRUN=
DRYRUN=--dry-run
46
BFO_BASEDIR=/media/BFO_archive
47
48
#LSDF_BASEDIR=\
#gpi-lsdf-seis-0001@gpilsdf.gpi.uni-karlsruhe.de:/home/gpi-lsdf-seis-0001/LSDF/SEIS/projects/BFO 
49
LSDF_BASEDIR=\
50
gpi-lsdf-seis-0001@gpilsdf.gpi.uni-karlsruhe.de:LSDF/SEIS/projects/BFO 
51
52
# 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.
53
DATADIRS=('seedlink/' 'DL1/' 'SG/UIPC/' 'ExperimentsLTA/')
Daniel Armbruster's avatar
Daniel Armbruster committed
54
55
56
57
EXCLUDE_PATTERNS=(\
"--exclude-from=/home/csback/csback.config/BFO2LSDF.sl.exclude" '')
#DATADIRS=('SG/UIPC/')
#EXCLUDE_PATTERNS=('')
58
59
60
61
62
63
64
65
66

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

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

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

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

88
SSHCMD="/usr/bin/ssh -i $HOME/.ssh/BFOLSDFoper"
89
# transfer data
Daniel Armbruster's avatar
Daniel Armbruster committed
90
for i in ${!DATADIRS[*]}
91
do
92
93
94
95
96
97
98
99
100
  # 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
101
102
    # start openvpn (requires appropriate system configuration)
    sudo /usr/sbin/openvpn /etc/openvpn/scc.ovpn.bromo > /dev/null 2>&1
103
104
105
106
107
108
    if [ $? -ne 0 ]
    then
      ${LOGGER} -i -p user.err "${LOGERROR}  Could not establish VPN \
        connection."
      exit 2
    else
109
110
111
112
113
114
115
116
117
118
119
120
      ${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
        ${LOGGER} -i -p user.err "${LOGERROR}  Could not establish VPN \
          connection."
        exit 2
      fi
121
122
123
    fi
  fi

124
125
126
127
128
129
  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

130
  # copy data (compressed)
thomas.forbriger's avatar
thomas.forbriger committed
131
  ${LOGGER} -i -p user.info "${LOGINFO}\
132
    ${RSYNC} -v -z -a --protocol=29 --bwlimit ${BWLIMIT} ${EXCLUDE} \
133
    -e \"${SSHCMD}\" $DRYRUN \
134
    --backup --suffix=$(date +'%Y%m%d%H%M') \
thomas.forbriger's avatar
thomas.forbriger committed
135
136
    ${EXCLUDE_PATTERNS[$i]} ${BFO_BASEDIR}/${DATADIRS[$i]} \
    ${LSDF_BASEDIR}/${DATADIRS[$i]}"
137
138
  set -o pipefail
  ${RSYNC} -v -z -a --protocol=29 --bwlimit ${BWLIMIT} ${EXCLUDE} \
139
    -e "${SSHCMD}" $DRYRUN \
140
    --backup --suffix=$(date +'%Y%m%d%H%M') \
Daniel Armbruster's avatar
Daniel Armbruster committed
141
    ${EXCLUDE_PATTERNS[$i]} ${BFO_BASEDIR}/${DATADIRS[$i]} \
142
143
    ${LSDF_BASEDIR}/${DATADIRS[$i]} \
      |& ${LOGGER} -i -p user.info -t "csback ${LOGINFO}" -s
144
145

  # write status to logfile
146
  RSYNCSTATUS=$?
147
  if [ $RSYNCSTATUS -eq 0 ]
148
  then
149
    ${LOGGER} -i -p user.info "${LOGINFO}  Copied data \
150
151
152
      '${BFO_BASEDIR}/${DATADIRS[$i]}' \
      successfully to LSDF (rsync EXIT CODE: \
      ${RSYNCSTATUS})."
153
  else
154
    ${LOGGER} -i -p user.err "${LOGERROR}  While copying data \
155
156
      '${BFO_BASEDIR}/${DATADIRS[$i]}' to LSDF (rsync EXIT CODE: \
      ${RSYNCSTATUS})."
157
158
159
160
161
162
163
  fi
done

# close VPN connection
${JNC} stop > /dev/null 2>&1
if [ $? -eq 0 ]
then
164
  ${LOGGER} -i -p user.info "${LOGINFO}  VPN connection successfully closed."
165
else
166
  ${LOGGER} -i -p user.err "${LOGERROR}  While closing VNP connection."
167
168
169
fi

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