summaryrefslogtreecommitdiff
path: root/backup.in
blob: fdc467e121eaa29ca118f33638712f23506ebd32 (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
#!/bin/bash

. #ETCDIR#/backup.conf

usage()
{
  >&2 echo 'This is backup version #VERSION#'
  >&2 echo ''
  >&2 echo 'usage:'
  >&2 echo ' backup /tmp/pidFile /path/to/destination/ user@source:path'
  >&2 echo ' backup /tmp/pidFile /path/to/destination/ user@source:path proxy_user@ssh_host'
  >&2 echo 'or as a symlink (e.g. in /etc/cron/daily) named after a key of $backups in #ETCDIR#/backup.conf without arguments'
  exit 1
}

if [ $# -eq 0 ]
then
  backupID="$(basename $0)"
  [ -z "${backups[${backupID}]}" ] && usage
  set /tmp/${backupID}.pid ${backups[${backupID}]}
fi

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

if [ "$#" -eq 3 ]
then
  Quelle="$3"
elif [ "$#" -eq 4 ]
then
  sshHopp="$4"
  lokPort=$[$RANDOM/2+8000]
  rsyncShell="-e ssh -p${lokPort} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
  tunnelBefehl="ssh -t -t -L${lokPort}:${QuellIP}:22 ${sshHopp}"
  HoppIP="${sshHopp#*@}"
  Quelle="$(echo "$3" | sed "s|${QuellIP}|127.0.0.1|")"
else
  usage
fi

ping -c1 ${QuellIP} > /dev/null || exit 11
if [ -n "${HoppIP}" ]
then
  ping -c1 ${HoppIP} > /dev/null || exit 11
fi

if [ ! -d ${Basis} ]
then
  [ $(mount | grep -c "\S\+\s\+\S\+\s\+/var/ftp\s") -eq 0 ] && exit 11
  exit 2
fi

neues_Datum=${Basis}/$(date "+%Y_%m_%d")
neues=${Basis}/aktuell
linkdests=""
for s in $(ls -1tr ${Basis} | grep -v aktuell | tail -n20 )
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
[ -e ${pidFile} ] && exit 5

echo $$ > ${pidFile}

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

for toExclude in ${excludes}
do
  excludeArgs="${excludeArgs} --exclude ${toExclude}"
done

if [ ! -e ${neues} ]
then
  mkdir ${neues}
fi
chmod 750 ${neues}
chown erich:root ${neues}
if [ -z "${rsyncShell}" ]
then
  rsync -av -x --no-p --no-o --no-g ${linkdests} \
    ${excludeArgs} \
    ${Quelle} ${neues}/
  sleep 1
  rsync ${Quelle}
else
  rsync "${rsyncShell}" -av -x --no-p --no-o --no-g ${linkdests} \
    ${excludeArgs} \
    ${Quelle} ${neues}/
  sleep 1
  rsync "${rsyncShell}" ${Quelle}
fi
erg=$?

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

if [ ${erg} -eq 0 ] || [ ${erg} -eq 24 ]
then
  mv ${neues} ${neues_Datum}
  rm ${pidFile}
else
  rm ${pidFile}
  exit 11
fi