-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup-util.sh
135 lines (126 loc) · 3.78 KB
/
backup-util.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
#!/bin/bash
#
# utility functions for backup scripts
# set environment variables
set_env(){
export CONF_DIR="/etc/rabe-backup"
export REMOTE_INCLUDE="/etc/rabe-backup.include"
export REMOTE_EXCLUDE="/etc/rabe-backup.exclude"
export RSYNC_PATH="sudo /bin/rsync"
export SSH_KEY="/home/backup/.ssh/id_rsa"
export SSH_USER="backup"
export RSH_CMD="ssh -i $SSH_KEY -l $SSH_USER"
export BACKUP_DST_DIR="/srv/backup/remote-backup"
export ZABBIX_CONFIG="/etc/zabbix/zabbix_agentd.conf"
}
# log with a configurable prefix
# usage: log -n "test 123"
log(){
local prefix
case $1 in
-e) prefix="Error: ";;
-i) prefix="Info: ";;
-n) prefix="Notice: ";;
-s) prefix="Success: ";;
-w) prefix="Warning: ";;
-d) prefix="Debug: ";;
esac
if [[ -n $prefix ]]; then
shift
else
prefix=" "
fi
echo "$prefix $0 $*"
}
# get content of a remote file
# usage: cat_remote_file vm-xyz.domain.net /etc/hosts
cat_remote_file(){
local hostname=$1
local file=$2
$RSH_CMD "$hostname" "cat $file"
}
# send successful backup run and timestamp to Zabbix
# usage: backup_success hostname unix_ts_start
backup_success(){
local hostname; hostname="$1"
local start; start="$2"
local ts; ts=$(date +%s)
local duration=$((ts - start))
local ret=0
local zabbix_hostname;
zabbix_hostname="$(cat_remote_file "$hostname" "/etc/zabbix/zabbix_agentd.conf" | \
grep "^Hostname=.*$" | awk -F "=" '{print $2}')"
if [[ -z $zabbix_hostname ]]; then
zabbix_hostname=$hostname
fi
# send timestamp of last successful backup
zabbix_sender --config "$ZABBIX_CONFIG" --host "$zabbix_hostname" \
--key "rabe.rabe-backup.run.success[]" --value "$ts" || ret=$?
# send duration of last successful backup
zabbix_sender --config "$ZABBIX_CONFIG" --host "$zabbix_hostname" \
--key "rabe.rabe-backup.run.duration[]" --value "$duration" || ret=$?
if [[ $ret -ne 0 ]]; then
log -e "Could not send status for $zabbix_hostname to Zabbix"
fi
}
# handle the exit code of rsync
# usage: rsync a b; handle_rsync_ret $? a b
handle_rsync_ret(){
local ret=$1
local src=$2
local dst=$3
case $ret in
0)
log -s "Sync of $src to $dst successful!";;
12)
log -e "Permission denied on ${src}."
ret=0;;
23)
log -i "$src does not exist"
ret=0;;
255)
log -e "Host $src is not online or could not be resolved.";;
*)
log -e "Unknown error ($ret) occured when trying to rsync $src to $dst.";;
esac
}
# rsync wrapper
# usage: do_rsync a b
do_rsync(){
local src; src=$1
local dst; dst=$2
local custom_rsync_opts; custom_rsync_opts=${3-""}
log -i "Starting rsync from $src to $dst"
# shellcheck disable=SC2086
rsync --rsync-path="$RSYNC_PATH" --rsh="$RSH_CMD" --verbose --archive --recursive --acls \
--devices --specials --delete --numeric-ids --timeout=120 --stats --human-readable --progress \
--inplace --one-file-system --relative $custom_rsync_opts "$src" "$dst" 2>/dev/null
handle_rsync_ret $? "$src" "$dst"
return $?
}
# backup directories specified by the remote system
# usage: backup_custom_dirs vm-xyz.domain.net
backup_custom_dirs(){
local hostname=$1
local dst=$2
local custom_rsync_opts; custom_rsync_opts=${3-""}
local errs=0
# fetch include and exclude files
local includes; includes=$(cat_remote_file "$hostname" "$REMOTE_INCLUDE")
local excludes; excludes=$(cat_remote_file "$hostname" "$REMOTE_EXCLUDE")
local exclude_opts=""
for exclude in $excludes; do
exclude_opts="$exclude_opts --exclude $exclude"
done
custom_rsync_opts="$custom_rsync_opts $exclude_opts"
for include in $includes; do
if ! do_rsync "$hostname:$include" "$dst" "$custom_rsync_opts"; then
((errs++))
fi
done
return "$errs"
}
set -u
set -o xtrace
set_env
# vim: tabstop=2 shiftwidth=2 expandtab