-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysbackup.sh
executable file
·416 lines (334 loc) · 13 KB
/
sysbackup.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/bin/bash
#############################################################################
# sysbackup
# Last Modified: Tue 22 May 2012 10:11:09 PM MDT by jbeard
#
# Fairly simple backup script, using rsync
# TODO:
# * Trackdown an issue where empty backups are created if connectivity is lost(?)
# * I think this happens if the remote data is unavailable or if we don't have
# * permission
# * Use logger for logging
# * Port to pure Bourne shell (sh)
# * Add sanity checks
# * Somehow check the real size of the backup before running
# * Add argument parsing
#############################################################################
# Set the trap early
trap my_trap INT
# Full path to a config file. If blank, we'll use the options below
config_file=""
# Address of the host to backup to/from
bk_host="192.168.1.120"
# Remote user
# If pulling, this user should have read rights to the data being pulled
# If pushing, beware that ownership and ACLs won't be preserved (unless
# remote user is root [bad idea])
bk_user="remoteuser"
# Path to backup to (local or remote)
bk_path="/data/backups"
# Path to SSH key to use
ssh_key="/home/sysbackup/.ssh/id_rsa"
# Arguments to pass to SSH (also used in the rsync command)
ssh_args="-F /home/sysbackup/.ssh/config -q -i $ssh_key"
### Rsync Options ###
# Don't pass the (-e) here, as it will be added below
# The following is an example of specifying the rsync path on the remote machine
#rsync_args="-avzXA --progress --stats --rsync-path=\"/usr/bin/sudo /usr/bin/rsync\""
rsync_args="-avXA --progress --stats"
# The path to the rsync binary on this machine
rsync_bin="/usr/bin/rsync"
# The full path to an optional filter file
rsync_filter="/etc/sysbackup/filter.txt"
# 0 = pull from the remote host
# 1 = push to the remote host
backup_method=0
# Check if the host is up via ICMP (ping 3 times)
check_if_up=1
# Log file. Leave empty to disable
log_file="/var/log/sysbackup.log"
log_date_fmt="+%F %T: "
# E-mail report
# Leave empty to disable
mail_to="[email protected]"
mail_only_errors=1
# Verbose shows what commands are being executed
verbose=1
# Array of paths to backup
# You can use "/" to backup the entire system
# If you choose "/", the backup name will be $bk_host.root
data_locs=(
"/svr/important_data"
"/etc"
"/home"
)
# Maximum age (in days) to keep backups
max_age=20
# Have at least this much free space before backing up (in megabytes)
min_free_space=1024 # in MegaBytes
# Format for date (the backups are named with this. See man date)
date_fmt="+%Y-%m-%d-%H-%M"
# Full path to a PID file we can use
pid_file="/tmp/backups.pid"
# Should we try to calculate free space on the destination before backing up?
# This doesn't work that well, and does take time to calculate.
# It requires SSH access to check the remote system's available space
# You'll also need to specify a backup_file_list so we can determine the backup size from that
calculate_free_space=1
#backup_file_list=$(mktemp /tmp/backup.$(date $date_fmt).XXX || exit 1)
#free_padding=200
i_have_configured=0
#############################################################################
# END OF CONFIGURATION
#############################################################################
if [ ! -z "$config_file" ]; then
if [ -e "$config_file" ]; then
if ! source "$config_file"; then
err="FAILURE: Unable to include $config_file\n"
log_print "$err" ; send_email "$err"
quit 1
fi
else
err="FAILURE: config_file is set, but $config_file doesn't exist.\n"
log_print "$err" ; send_email "$err"
quit 1
fi
fi
#############################################################################
# Functions
#############################################################################
function send_email() {
okaytomail=0
# FIXME; This sucks. Need the multiple if condition to work :\
[ "$mail_only_errors" -ne 1 ] && okaytomail=1
if [ "$mail_only_errors" -eq 1 ]; then
if [ ! -z "$3" ] && [ "$3" == "err" ]; then
okaytomail=1
fi
fi
#if [ "$mail_only_errors" -ne 1 ] || [ [ "$mail_only_errors" -eq 1 ] && [ ! -z "$3" ] && [ "$3" == "err" ] ]; then
if [ "$okaytomail" -eq 1 ]; then
[ ! -z "$2" ] && subject="$2" || subject=""
if [ ! -z "$mail_to" ]; then
message="$1\n\n--\nsysbackup running on $(hostname)\n"
printf "$message" | mail -s "$(hostname) backup report ${subject}" $mail_to
fi
fi
}
# Add message to e-mail
function add_email() {
email_msg="${email_msg}$(date '+%Y-%m-%d %H:%M') ${1}\n"
}
function human_readable() {
echo "$1"|awk '{ sum=$1 ; hum[1024**2]="GB";hum[1024**1]="MB";hum[1024]="KB"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}'
}
function is_host_up() {
ping -c 3 -q $1 &>/dev/null
return $?
}
function verify_bkpath() {
if [ $backup_method -eq 0 ]; then
[ -d "$bk_path" ] && return 0 || return 1
else
[ $verbose -eq 1 ] && log_print "ssh $ssh_args $bk_user@$bk_host \"[ -d ${bk_path} ] && exit 0 || exit 1\"\n"
ssh $ssh_args $bk_user@$bk_host "[ -d $bk_path ] && exit 0 || exit 1"
fi
return $?
}
function check_if_same_fs() {
#### This isn't used right now ####
# It would be cool to use stat, but it varies between BSD and Linux and I don't want to over-complicate this
# This is rather hackish
# Uses df to determine if two directories are on the same filesystem
printf "Executing: ssh $ssh_args $bk_user@$bk_host \"dirone=\"\$(df $1|tail -n 1|cut -d ' ' -f 1)\" dirtwo=\"\$(df $2|tail -n 1|cut -d ' ' -f 1)\"; [ \"\$dirone\" == \"\$dirtwo\" ] && exit 0 || exit 1\"\n"
ssh $ssh_args $bk_user@$bk_host "dirone=\"\$(df $1|tail -n 1|cut -d ' ' -f 1)\" dirtwo=\"\$(df $2|tail -n 1|cut -d ' ' -f 1)\"; [ \"\$dirone\" == \"\$dirtwo\" ] && exit 0 || exit 1"
return $?
}
function show_header() {
awk 'BEGIN{$70=OFS="=";print}'
printf "%*s\n" $(((${#1}+70)/2)) "$1"
printf "%*s\n" $(((${#2}+70)/2)) "$2"
awk 'BEGIN{$70=OFS="=";print}'
}
function quit() {
if [ -e "$pid_file" ]; then
rm -f "$pid_file"
else
log_print "OOPS: ${pid_file} is missing!\n"
fi
exit $1
}
function my_trap() {
printf "\n==> Aborted. Cleaning up..."
[ -e "$pid_file" ] && (rm -f "$pid_file" && log_print "Okay" || log_print "Failed to remove ${pid_file}") || log_print "OOPS: ${pid_file} is missing"
log_print "\n"
exit 255
}
function log() {
if [ ! -z "${log_file}" ]; then
printf "$(date "${log_date_fmt}")$1" >> "${log_file}"
fi
}
function log_print() {
log "$1"
printf "> $1"
}
#############################################################################
today="$(date $date_fmt)"
ver="2012-05-22"
#############################################################################
#############################################################################
# Start the program
#############################################################################
# Check if we're already running
if [ -e "$pid_file" ]; then
my_pid=$(cat "$pid_file")
if ps $my_pid &>/dev/null; then
log_print "$0 is already running (pid: ${my_pid})\n"
exit 1
else
err="${pid_file} reports a PID of ${my_pid}, but it doesn't seem to be running.\n"
err="$err If it's really not running, please remove ${pid_file}\n"
log_print "$err" ; send_email "$err" "FAILURE" "err"
exit 1
fi
fi
printf "$$" > $pid_file
##################################
# Did you configure it?
##################################
if [ "$i_have_configured" == "0" ]; then
log_print "FAILURE: You need to configure the program first.\n"
quit 1
fi
##################################
# Set the rsync filter
##################################
if [[ ! -z "$rsync_filter" && -e "$rsync_filter" ]]; then
rsync_args="$rsync_args --include-from=${rsync_filter}"
fi
show_header "sysbackup v.${ver}" "$(date '+%F @ %H:%M')"
log "Starting sysbackup..\n"
##################################
# Check if the host is up (icmp)
##################################
if [ $check_if_up -eq 1 ]; then
log_print "Checking if host $bk_host is up... "
if ! is_host_up $bk_host; then
log_print "failed\n"
err="FAILURE: $bk_host is unreachable (via ICMP)\n"
log_print "$err" ; send_email "$err" "FAILURE" "err"
quit 1
else
log_print "ok\n"
fi
fi
##################################
# Calculate the free space
##################################
if [ "$calculate_free_space" -eq 1 ]; then
log_print "Calculating available space, please wait...\n"
min_free_space=$((min_free_space*1024))
[ $backup_method -eq 0 ] && where="on this host" || where="on the remote host"
# Need to determine if bk_path exists first
if ! verify_bkpath $bk_path; then
err="FAILURE: $bk_path doesn't exist ${where}, which we need to determine accurate free space.\n"
log_print "$err" ; send_email "$err" "FAILURE" "err"
quit 1
fi
if [ $backup_method -eq 0 ]; then
free_space=$(df -kP $bk_path|tail -1|awk '{print $4}')
else
free_space=$(ssh $ssh_args $bk_user@$bk_host "df -kP $bk_path|tail -1|awk '{print \$4}'")
fi
msg=" => Free space: $free_space KB ($(human_readable $free_space))\n"
msg="$msg => Minimum required free space: $min_free_space KB ($(human_readable $min_free_space))\n"
log_print "$msg" ; add_email "$msg"
if [ "$min_free_space" -ge "$free_space" ]; then
err=" >> FAILURE: There is not enough free space ${where} to accomodate this backup\n"
log_print "$err" ; send_email "$err" "FAILURE" "err"
quit 1
fi
# printf "Calculating sizes (this will take some time)...\n"
# [ -f "$backup_file_list" ] && rm -f "$backup_file_list"
#
# for data in "${data_locs[@]}"; do
# rsync -an --stats -e "ssh $ssh_args" --link-dest="${bk_path}/Latest" "$data" $bk_user@$bk_host:$bk_path/$today.calculate >> "$backup_file_list"
# done
#
# #backup_size=$((`awk '{sum+=2}END{print sum}' $backup_file_list`)) # in kilobytes
# backup_size=$(($(grep "total size is" "$backup_file_list"|awk '{sum+=$4}END{print sum}')/1024))
# #backup_free=$(ssh $ssh_args $bk_user@$bk_host "df -k $bk_path|tail -1|awk '{print \$4}'")
#
# echo " Total size (in KB): $backup_size"
# echo " Total Free (in KB): $backup_free"
#
# echo "$backup_file_list"
fi # calculate_free_space
[ $verbose -eq 1 ] && log_print "SSH Key: ${ssh_key} Rsync Arguments: ${rsync_args}\n"
##################################
# The actual rsync process
##################################
for data in "${data_locs[@]}"; do
if ! is_host_up $bk_host; then
err="FAILURE: lost connectivity to $bk_host (via ICMP)\n"
log_print "$err" ; send_email "$err" "FAILURE" "err"
quit 1
fi
# This is sloppy. If we're backing up root, name it after the host.root
if [ "$data" == "/" ]; then
relative="${bk_host}.root"
else
relative=$(basename $data)
fi
if [ $backup_method = 0 ]; then
if [ -e "$bk_path/$relative" ]; then
find $bk_path/$relative -maxdepth 1 -mtime +$max_age -exec echo 'Removing {}' \; -exec rm -rf {} \;
msg="Removing backups older than $max_age days\n"
log_print "$msg" ; add_email "$msg"
else
msg="Creating ${bk_path}/${relative}\n"
log_print "$msg" ; add_email "$msg"
mkdir -p "$bk_path/$relative"
fi
if [ $data == "/" ]; then
echo "here it is"
rsync_string="${bk_user}@${bk_host}:/"
else
rsync_string="${bk_user}@${bk_host}:${data}/"
fi
rsync_string="${rsync_string} ${bk_path}/$relative/${today}.inprogress"
else
msg="Removing backups older than $max_age\n"
msg="${msg}Executing: ssh $ssh_args $bk_user@$bk_host \"find $bk_path/$relative -maxdepth 1 -mtime +$max_age -exec echo 'Removing {}' \; -exec rm -rf {} \;\"\n"
[ $verbose -eq 1 ] && log_print "$msg" ; add_email "$msg"
ssh $ssh_args $bk_user@$bk_host "[ -e \"$bk_path/$relative\" ] && find $bk_path/$relative -maxdepth 1 -mtime +$max_age -exec echo 'Removing {}' \; -exec rm -rf {} \; || mkdir -p \"$bk_path/$relative\""
rsync_string="${data}/. ${bk_host}:${bk_path}/$relative/${today}.inprogress"
fi
msg="Backing up ${data}\n"
msg="${msg}Executing: $rsync_bin $rsync_args -e \"ssh $ssh_args -l ${bk_user}\" --link-dest=\"${bk_path}/$relative/Latest\" --stats ${rsync_string}\n"
[ $verbose -eq 1 ] && log_print "$msg" ; add_email "$msg"
rsync_cmd="$rsync_bin $rsync_args -e \"ssh $ssh_args\" --link-dest=\"${bk_path}/$relative/Latest\" ${rsync_string}"
eval $rsync_cmd
#$rsync_bin $rsync_args -e "ssh $ssh_args" --link-dest="${bk_path}/$relative/Latest" ${rsync_string}
if [ $backup_method = 0 ]; then
msg="Moving ${today}.inprogress to ${today} and symlinking to Latest\n"
log_print "$msg" ; add_email "$msg"
cd $bk_path/$relative
mv $today.inprogress $today
rm -f Latest;ln -s $today Latest
# Update the time so the find routine will work right
touch $today
else
msg="Moving $today}.inprogress to ${today} and symlinking to Latest\n"
msg="${msg}Executing: ssh $ssh_args $bk_user@$bk_host \"mv $bk_path/$today.inprogress $bk_path/$relative/$today;cd $bk_path/$relative;rm -f Latest;ln -s $today Latest;touch $today\"\n"
[ $verbose -eq 1 ] && log_print "$msg" ; add_email "$msg"
ssh $ssh_args $bk_user@$bk_host "mv $bk_path/$relative/$today.inprogress $bk_path/$relative/$today;cd $bk_path/$relative;rm -f Latest;ln -s $today Latest;touch $today"
fi
done
msg="==> Completed\n"
log_print "$msg"
add_email "$msg"
send_email "$email_msg"
quit 0
#EOF