-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·197 lines (156 loc) · 4.71 KB
/
backup.sh
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
#!/bin/bash
## Generic backup script 1.0 - D250 Laboratories
## Written by István Király - [email protected]
## https://github.com/LaKing/rsync-backup
## The questionmark argument will not do any syncing, only query disk usages.
## This file can be sourced, or edited and backup commands can be added at the bottom.
## ssh key-authentication, and fingerprint configuration required.
## check arguments
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
head -n 9 $0 | grep "##" | tr "##" ' '
echo ''
exit
fi
## logs about the whole process
LOG=/var/log
## function that will timestamp
function log {
NOW=$(date +%Y-%m-%d-%H:%M:%S)
echo "$NOW - $1" >> $LOG
}
## do a test-only/measure disk usage - or - do a full backup?
if [ -z "$BACKUP" ]
then
BACKUP=true
fi
if [ -z "$BACKUP_PATH" ]
then
BACKUP_PATH="$1"
fi
## most important variable
if [ ! -d "$BACKUP_PATH" ]
then
head -n 9 $0 | grep "##" | tr "##" ' '
echo ''
echo "Variable BACKUP_PATH not defined. Define either as argument or exported variable."
echo ''
exit
else
log "backup to: $BACKUP_PATH"
echo $BACKUP_PATH
df -h $BACKUP_PATH
echo ''
if [ "$1" == "query" ] || [ "$1" == "?" ]
then
head -n 9 $0 | grep "##" | tr "##" ' '
echo ''
BACKUP=false
fi
fi
## local backup from a local folder
function local_backup {
backup_dirs="${@:1}"
backup_target=$BACKUP_PATH/$(hostname)
for i in $backup_dirs
do
mkdir -p $LOG/backup/$HOSTNAME
backup_log=$LOG/backup/$HOSTNAME/log
echo $(hostname):$i
mkdir -p $backup_target/$(dirname $i)
du -hs $backup_target/$i
du -hs $i
log "local_backup $(hostname):$i"
if $BACKUP
then
if rsync --delete -av $i $backup_target/$(dirname $i) >> $backup_log
then
log "done $(hostname) @ $i"
else
log "done $(hostname) @ $i"
echo "done $(hostname) @ $i"
fi
fi
echo ''
done
echo ''
}
## local backup from a server
function server_backup {
backup_host="$1"
backup_hostname="$(ssh -n $1 hostname)"
backup_dirs="${@:2}"
backup_target=$BACKUP_PATH/$backup_hostname
for i in $backup_dirs
do
mkdir -p $LOG/backup/$backup_hostname/$i
backup_log=$LOG/backup/$backup_hostname/$i/log
echo $backup_host:$i
mkdir -p $backup_target/$i
du -hs $backup_target/$i
logs "backup @ $backup_host:$i"
cmd='echo "$(du -hs '$i') $(hostname)/'$i'"'
if ssh -n -o BatchMode=yes $backup_host "$cmd"
then
if $BACKUP
then
if rsync --delete -avze ssh $backup_host:/$i $backup_target/$(dirname $i) >> $backup_log
then
log "backup done $backup_host @ $i"
else
log "ERROR $backup_host @ $i"
echo "ERROR $backup_host @ $i"
fi
fi
else
echo "!! Connection failed to $backup_host."
fi
echo ''
done
echo ''
}
## local backup of a host thru an ssh-tunneling proxy-server
function remote_backup {
backup_proxy="$1"
backup_host="$2"
cmd="ssh -n $backup_host hostname"
backup_hostname="$(ssh -n $backup_proxy $cmd)"
backup_dirs="${@:3}"
backup_target=$BACKUP_PATH/$backup_hostname
for i in $backup_dirs
do
mkdir -p $LOG/backup/$backup_hostname/
backup_log=$LOG/backup/$backup_hostname/$i/log
echo $backup_hostname:$i
mkdir -p $backup_target/$i
du -hs $backup_target/$i
logs "backup @ $backup_host:$i"
cmd='echo "$(du -hs '$i') $(hostname)/'$i'"'
if ssh -o BatchMode=yes $backup_proxy "ssh -n -o BatchMode=yes $backup_host '$cmd'"
then
if $BACKUP
then
if rsync --delete -avz -e "ssh -n -A $backup_proxy ssh -n" $backup_host:/$i $backup_target/$(dirname $i) >> $backup_log
then
log "done $backup_host @ $i"
else
log "ERROR $backup_host @ $i"
echo "ERROR $backup_host @ $i"
fi
fi
else
echo "!! Connection failed to $backup_proxy"
fi
echo ''
done
echo ''
}
## examples:
## create a local backup
#local_backup /etc /root /srv
## create a remote backup
#server_backup s1.example.com /etc /root
#server_backup s2.example.com /etc /root
## create a remote tunneled backup
#remote_backup example.com 10.9.8.7 /etc /srv /mnt
## either add your own settings here, or source this file