summaryrefslogtreecommitdiff
path: root/backup.in
blob: e8caa03db476a8585f42348f0e11ef33d28511a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash

rsyncOptions='-vxcaHAXF'
rsync --help | \
  grep -q -- --may-modify-others && \
  rsyncOptions="${rsyncOptions} --may-modify-others"

[ -r "#ETCDIR#/backup.conf" ] && \
  . "#ETCDIR#/backup.conf"

usage()
{
  >&2 echo \
'Usage:  backup /tmp/pidFile /path/to/destination/ user@source:path [proxy_user@ssh_host]
Backup files from remote with rsync, possibly via SSH-tunnel.

With no arguments, information is expected to be in array $backups in #ETCDIR#/backup.conf with name of executable (e.g. a symlink) as key.
With one argument, information is expected to be in array ${backups[$1]} in #ETCDIR#/backup.conf.

Options:
  /tmp/pidFile           location of file to store PID in
  /path/to/destination   location to store backups in
  user@source:path       remote data to back up
  proxy_user@ssh_host    ssh login to proxy node (optional)
#HELPTEXT#              #'
  [ -z "$1" ] && exit 1
  exit $1
}

extract_ssh_host() {
  {
    printf '%s\n' "$1"
    if [ -f ~/.ssh/config ]; then
      sed '
        /^Host\s\+'"$1"'$/,/^Host\s/ {
          s/^\s*Hostname\s\+//
          t
        }
        d
      ' ~/.ssh/config
    fi
  } \
    | tail -n1
}

extract_ssh_ip_protocols() {
  getent ahosts "$(extract_ssh_host "$1")" \
    | sed '
      s/^\([0-9]\+\.\)\{3\}[0-9]\+\s\+STREAM\(\s.*\)\?$/4/
      t
      s/^[0-9a-f][0-9a-f:]\+\s\+STREAM\(\s.*\)\?$/6/
      t
      d
    ' \
    | sort -u \
    | if [ -f ~/.ssh/config ]; then
      grep -vxF "$(
        sed '
          /^Host\s\+'"$1"'$/,/^Host\s/ {
            s/^\s*AddressFamily\s\+inet\s*$/6/
            t
            s/^\s*AddressFamily\s\+inet6\s*$/4/
            t
          }
          d
        ' ~/.ssh/config
      )"
    else
      cat
    fi
}

if [ $# -eq 1 ]; then
  if [ "$1" == "--help" ]; then
    usage 0
  elif [ "$1" == "--version" ]; then
    echo '#VERSION#'
    exit
  fi
fi

seldom=false
if [ $# -eq 0 ] || [ $# -eq 1 ]; then
  if [ $# -eq 0 ]; then
    backupID="$(basename $0)"
  else
    backupID="$1"
  fi
  [ -z "${backups[${backupID}]}" ] && usage
  set /tmp/${backupID}.pid ${backups[${backupID}]}
  if printf '%s\n' "${seldomBackups[@]}" | \
    grep -qxF "${backupID}"; then
    seldom=true
  fi
fi

Basis="$2"
pidFile="$1"
QuellIP=$(echo "$3" | sed "s|^[a-zA-Z]*://||; s|^[a-zA-Z]*@||; s|:\?/.*$||")

if [ "$#" -eq 3 ]; then
  Quelle="$3"
  ipVer=$(
    extract_ssh_ip_protocols "${QuellIP}" \
      | sort -n \
      | tail -n1
  )
elif [ "$#" -eq 4 ]; then
  sshHopp="$4"
  lokPort=$[$RANDOM/2+8000]
  HoppIP="${sshHopp#*@}"
  ipVer=$(
    {
      extract_ssh_ip_protocols "${QuellIP}"
      extract_ssh_ip_protocols "${HoppIP}"
    } \
      | sort -n \
      | uniq -d \
      | tail -n1
  )
  if [ ${ipVer} -eq 4 ]; then
    localAddress='127.0.0.1'
  elif [ ${ipVer} -eq 6 ]; then
    localAddress='[::1]'
  else
    >&2 echo 'ip version of hopp and target must be the same'
    exit 1
  fi
  rsyncShell="-e ssh -${ipVer} -p${lokPort} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
  tunnelBefehl="ssh -${ipVer} -t -t -L${localAddress}:${lokPort}:${QuellIP}:22 ${sshHopp}"
  Quelle="$(echo "$3" | sed "s|${QuellIP}|${localAddress}|")"
else
  usage
fi

rsyncOptions="${rsyncOptions}${ipVer}"

if [ ! -d ${Basis} ]; then
  for neededMount in "${neededMounts[@]}"; do
    if ! mountpoint -q "${neededMount}"; then
      exit 11
    fi
  done
  exit 2
fi

neues_Datum="${Basis}/$(date "+%Y_%m_%d")"
neues="${Basis}/aktuell"
linkdests=""
for s in $(ls -1 "${Basis}" | sort -r | grep -vxF -m 20 aktuell ); do
  linkdests="${linkdests} --link-dest ${Basis}/${s}"
done

if [ ! "$(whoami)" == "root" ]; then
  echo "I need to be root."
  exit 3
fi

[ -w "${Basis}" ] || exit 11
[ -e "${neues_Datum}" ] && exit 4
if ${seldom}; then
  for date_diff in $(seq ${seldomness}); do
    if [ -e "${Basis}/$(date '+%Y_%m_%d' -d@$(($(date '+%s')-24*60*60*date_diff)))" ]; then
      exit 4
    fi
  done
fi
[ -s "${pidFile}" ] && kill -0 $(cat "${pidFile}") 2>/dev/null && exit 5

echo $$ > "${pidFile}"

if [ -n "${QuellIP}" ]; then
  if ! ping -c1 "$(extract_ssh_host "${QuellIP}")" >/dev/null; then
    rm "${pidFile}"
    exit 11
  fi
fi
if [ -n "${HoppIP}" ]; then
  if ! ping -c1 "$(extract_ssh_host "${HoppIP}")" >/dev/null; then
    rm "${pidFile}"
    exit 11
  fi
fi

if [ -n "${tunnelBefehl}" ]; then
  ${tunnelBefehl} &
  backgroundPid=$!
  sleep 4
fi

for toExclude in "${excludes[@]}"; do
  excludeArgs="${excludeArgs} --exclude ${toExclude}"
done

if [ ! -e "${neues}/wip" ]; then
  mkdir -p "${neues}/wip"
fi
chmod 750 "${neues}"{,/wip}
chown root:root "${neues}"{,/wip}
if [ -z "${rsyncShell}" ]; then
  rsync ${rsyncOptions} \
    ${linkdests} \
    ${excludeArgs} \
    ${Quelle} "${neues}/wip/"
  sleep 1
  rsync ${Quelle}
else
  rsync "${rsyncShell}" \
    ${rsyncOptions} \
    ${linkdests} \
    ${excludeArgs} \
    ${Quelle} "${neues}/wip/"
  sleep 1
  rsync "${rsyncShell}" ${Quelle}
fi
erg=$?

[ -n "${backgroundPid}" ] && kill "${backgroundPid}"

if [ ${erg} -eq 0 ] || [ ${erg} -eq 24 ]; then
  chmod o-rwx "${neues}/wip"
  neueres_Datum="${Basis}/$(date "+%Y_%m_%d")"
  if [ ! -e "${neueres_Datum}" ]; then
    neues_Datum="${neueres_Datum}"
  fi
  mv "${neues}/wip" "${neues_Datum}"
  rmdir "${neues}"
  rm "${pidFile}"
else
  rm "${pidFile}"
  exit 11
fi