-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·292 lines (236 loc) · 6.62 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
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
#!/bin/bash
#
# Bash backup script using rdiff-backup.
# Copyright (c) 2015, Tobias Bleiker
#
# Source on github: https://github.com/tbleiker/rdiff-backup-script
#
############################################################
# Settings
############################################################
# Options for rdiff backup.
optRdiffBackup="--print-statistics"
# Options for rdiff to remove old files.
optRdiffClean="--remove-older-than 1M --force"
# Where to put the logfile by default.
logfile=/var/log/backup
############################################################
# Helper functions
############################################################
function out() {
while read line; do
# Add prefix to the output.
case $1 in
suc)
#tmp=`echo -e "INF: \e[34mSUCCESS: $line\e[0m"`
tmp=`echo "INF: SUCCESS - $line"`
;;
warn)
tmp=`echo "WAR: $line"`
;;
err)
#tmp=`echo -e "\e[31mERR: $line\e[0m"`
tmp=`echo "ERR: $line"`
;;
*)
tmp=`echo "INF: $line"`
esac
# Print to stdout if
# - verbose is set.
# - an error is printed.
# - a success is printed
if ( [[ $verbose == "true" ]] || [[ $1 == "err" ]] || [[ $1 == "suc" ]] ); then
#echo $tmp > /dev/stdout
echo $tmp
fi
# Add a time stamp and print to the log file.
echo $tmp | grep -v "^$" | sed -e "s/^/$(date +"[%Y-%m-%d %H:%M:%S]") /" >> $logfile
done
}
# Print errors to the log file, to stderr and to stdout.
function exitOnFailure() {
if [ $1 -ne 0 ]; then
sleep 2
echo $2 | out err
echo "" | out inf
exit 1
fi
}
# Print help message.
function usage(){
#echo "Bash script to run backups with duplicity."
echo "Bash script to run backups with rdiff-backup."
echo ""
echo "Usage:"
echo " $0 [options] file"
echo " $0 [options] source destination type"
echo ""
echo "Options:"
echo " -h show this help"
echo " -l specify log file"
#echo " -n dry run"
echo " -v verbose output"
echo ""
}
############################################################
# Main
############################################################
#
# Some initial stuff
#
# Set verbose to false by default.
verbose=false
## Get options ##
while getopts :hl:v opt; do
case $opt in
h) # show help
usage
exit 0
;;
l) # where to put the log file
logfile=$OPTARG
;;
v)
verbose=true
;;
\?) # unrecognized option - show help
echo "EXIT: Invalid option: -$OPTARG"
exit 2
;;
esac
done
## Prepare log file ##
# Create log file if it does not exist.
mkdir -p $(dirname logfile)
if [ $? -ne 0 ]; then
echo "EXIT: Could not create directory for log file."
exit 1
fi
touch $logfile
if [ $? -ne 0 ]; then
echo "EXIT: Could not create log file."
exit 1
fi
# Print log header.
echo "##########################################################################" | out
echo "# Backup, $(date +"%c")" | out
echo "##########################################################################" | out
## Get arguments ##
shift $((OPTIND - 1))
# Check how many arguments are given:
# 0: show help message and exit
# 1: assume its a path to the file containing the tasks
# 2+: assume the arguments describe a single tasks
if [ $# -eq 0 ]
then
usage
exit 1
elif [ $# -eq 1 ]
then
tasks=`cat $1 2> /dev/null | egrep -v '(^#|^\s*$|^\s*\t*#)'`
exitOnFailure $? "file '$1' cannot be read."
else
tasks=$@
fi
#
# Main loop to evaluate and run each task
#
echo "" | out
printf '%s\n' "$tasks" | while read task
do
# Get path to the source and the destination.
src=$(echo $task | awk '{ print $1 }')
dest=$(echo $task | awk '{ print $2 }')
# Print info for the current task.
echo "## Backup $src" | out
echo "" | out
## Check src and dest ##
# Test for zfs.
zfs list $src 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
type="zfs"
fi
# Test for lvm.
if [ -b /dev/$src ] || [ -b $src ]; then
echo "" | out
lvs $src 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
type="lvm"
fi
fi
# Test for directory.
if [ -d $src -a -r $src ]; then
type="dir"
fi
# Exit if src is not a valid type.
if [ -z "$type" ]; then
exitOnFailure 1 "$src is not a valid source."
fi
# Check if dest exists and is writable.
if [ ! -d $dest ]; then
exitOnFailure 1 "$dest is not a valid path for the backup."
fi
if [ ! -w $dest ]; then
exitOnFailure 1 "$dest is not writable."
fi
pathDest=$dest
## Preparation ##
case "$type" in
zfs)
# Get the mount point of the zfs dataset.
pathSrc=`zfs list $src | grep $src | awk '{ print $5 }'`
;;
lvm)
# Get the name of the volume group and the logic volume.
vgName=`lvs $src | tail -n 1 | awk '{ print $2 }'`
lvName=`lvs $src | tail -n 1 | awk '{ print $1 }'`
# Define the name and the size of the snapshot.
snapshotName=snap_${lvName}
snapshotSize=$(lvdisplay -C $src| tail -n+2 | awk '{ print $4 }' )
# Create the snapshot.
lvcreate --size $snapshotSize --snapshot --name $snapshotName $src 2> >(out warn) 1> >(out inf)
exitOnFailure $? "Could not greate snapshot."
# Mount the snapshot.
pathSrc=/mnt/$snapshotName
mkdir $pathSrc 2> >(out warn) 1> >(out inf)
exitOnFailure $? "Could not create folder to mount snapshot."
mount /dev/$vgName/$lvName $pathSrc 2> >(out warn) 1> >(out inf)
exitOnFailure $? "Could not mount snapshot."
;;
dir)
# Nothing to do for type 'dir'.
;;
esac
## Backup ##
# Run the backup.
rdiff-backup $optRdiffBackup $pathSrc $pathDest 2> >(out warn) 1> >(out inf)
exitOnFailure $? "rdiff-backup backup failed."
# Remove old backup files.
rdiff-backup $optRdiffClean $pathDest 2> >(out warn) 1> >(out inf)
exitOnFailure $? "rdiff-backup remove old files failed."
## Clean up ##
case "$type" in
zfs)
# Nothing to do for type 'zfs'.
;;
lvm)
# Unmount the snapshot.
umount $pathSrc 2> >(out warn) 1> >(out inf)
exitOnFailure $? "Could not unmount snapshot."
# Delte the mount point.
rm -rv $pathSrc 2> >(out warn) 1> >(out inf)
exitOnFailure $? "Could not delete mount folder."
# Remove the snapshot.
lvremove -f $vgName/$snapshotName 2> >(out warn) 1> >(out inf)
exitOnFailure $? "Could not remove snapshot."
;;
dir)
# Nothing to do for type 'dir'.
;;
esac
# Print final message.
sleep 2
echo "" | out
echo "Backup $src." | out suc
echo "" | out
done