-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync-backup.bash
executable file
·149 lines (121 loc) · 2.77 KB
/
rsync-backup.bash
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
#!/bin/bash
BACKUP_TIME="$(date +%s)"
BACKUP_LOGS="/tmp/rsync-backup.log"
BACKUP_PATH="/pool/backup/twoface-rsync"
BACKUP_ENTRIES=(
"/var/lib/plexmediaserver"
"/home/rmf/scripts"
"/home/rmf/repositories"
)
#
# ensure script is run as root
#
function enforce_root() {
if [[ "$EUID" -ne 0 ]]; then
out_error "This script must be run as root. Try \"sudo\" perhaps?"
exit 255
fi
}
#
# output error
#
function out_error() {
local format="[ERROR] ${1}"
shift
printf "${format}\n" $@ | tee -a "${BACKUP_LOGS}"
}
#
# output line of text
#
function out_line() {
local format="${1}"
shift
printf "${format}\n" $@ | tee -a "${BACKUP_LOGS}"
}
#
# output action error
#
function out_action_error() {
local format=" • [ERROR] ${1}"
shift
printf "${format}\n" $@ | tee -a "${BACKUP_LOGS}"
}
#
# output action line of text
#
function out_action_line() {
local format=" • ${1}"
shift
printf "${format}\n" $@ | tee -a "${BACKUP_LOGS}"
}
#
# check configuration
#
function do_config_check() {
local size=${#BACKUP_ENTRIES[@]}
if [[ ${size} -eq 0 ]]; then
out_error "No backup path entries found in configuration array!"
exit 255
fi
local size="$((${#BACKUP_PATH} - 1))"
if [[ "${BACKUP_PATH:$size:1}" == "/" ]]; then
BACKUP_PATH="${BACKUP_PATH:0:$size}"
fi
if [[ ! -d "${BACKUP_PATH}" ]]; then
mkdir -p "${BACKUP_PATH}"
if [[ ! -d "${BACKUP_PATH}" ]]; then
out_error 'Unable to create backup destination path %s' "${BACKUP_PATH}"
fi
fi
out_line 'Initiating backup operations (found %d configuration entries):' "${#BACKUP_ENTRIES[@]}"
}
#
# perform rsync backup operation
#
function do_rsync_backup() {
local path="${1}"
local log="$(dirname "${BACKUP_LOGS}")/$(basename "${BACKUP_LOGS}" .log).rsync.${BACKUP_TIME}.log"
local bin="$(which rsync)"
local opt="-r -a -A -X -i --log-file=${log}"
local cmd="${bin} ${opt} ${path} ${BACKUP_PATH}/${BACKUP_TIME}"
${cmd} &> /dev/null
if [[ $? -ne 0 ]]; then
out_action_error 'Encountered an rsync error while running "%s" command' "${cmd}"
fi
}
#
# perform backup operation on config entry
#
function do_path_backup() {
local path="${1}"
local size="$((${#path} - 1))"
if [[ "${path:$size:1}" == "/" ]]; then
path="${path:0:$size}"
fi
if [[ ! -d "${path}" ]]; then
out_action_error 'Skipping non-existant path for "%s" backup configuration entry...' "${path}"
return
fi
out_action_line 'Performing backup operations for "%s" backup configuration entry...' "${path}"
do_rsync_backup "${path}"
}
#
# perform backup operation on config array
#
function do_path_backups() {
for path in "${BACKUP_ENTRIES[@]}"; do
do_path_backup "${path}"
done
}
#
# main
#
function main() {
enforce_root
do_config_check
do_path_backups
}
#
# go!
#
main $@