-
Notifications
You must be signed in to change notification settings - Fork 10
/
hactar
executable file
·150 lines (127 loc) · 3.19 KB
/
hactar
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
#!/usr/bin/env bash
# Hactar - incremental daily rsync backup
# Copyright (c) 2015 by Florian Beer <fb@42dev.eu>
set -e
declare -i RETAIN=30
declare -r VERSION="1.01"
usage() {
cat <<EOM
Hactar - An incremental daily backup script using rsync
Copyright (c)2015 by Florian Beer <fb@42dev.eu>
Version $VERSION
This script comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See CC BY-NC-SA 4.0 for details.
https://creativecommons.org/licenses/by-nc-sa/4.0/
Usage
`basename $0` [OPTIONS] SOURCE TARGET
Options
-r NUM number of days to keep old backups
-e FILE specify (optional) rsync excludes filename
-d dry run
-v increase verbosity
-h show this message
-V show version number
Example
hactar -v -e my_exludes / /backup/
EOM
}
parse_commandline_arguments() {
while [[ ${1:0:1} = "-" ]] ; do
local n=1
local l=${#1}
while [[ $n -lt $l ]] ; do
case ${1:$n:1} in
h)
usage
exit 0
;;
r)
if [[ $n -ne $(($l-1)) || ! -n ${2} ]]; then
usage
exit 1
fi
if [[ ${2} == ?(-)+([0-9]) ]]; then
RETAIN="${2}"
else
printf "Error: -r must be followed by an integer\n" >&2
exit 2
fi
shift
;;
e)
if [[ $n -ne $(($l-1)) || ! -n ${2} ]]; then
usage
exit 1
fi
if [[ -f $2 ]]; then
EXCLUDE="${2}"
shift
else
printf "Error: Excludes file \"%s\" not found\n" "${2}" >&2
exit 3
fi
;;
v)
VERBOSE="--verbose --progress"
;;
V)
printf "Hactar Version %s\n" "${VERSION}"
exit 0
;;
d) DRYRUN="--dry-run"
;;
*)
printf "Error: Unknown option %s\n" "${1}" >&2
exit 4
;;
esac
n=$(($n+1))
done
shift
done
# No arguments where given
if [[ ! -n ${1} ]]; then
usage
exit 0
fi
# Target directory is missing
if [[ ! -n ${2} ]]; then
printf "Error: Please specify target directory\n" >&2
exit 5
fi
SOURCE="${1}"
TARGET="${2}"
}
run_backup() {
# Do we have a backup to hardlink against?
if [[ -d ${TARGET}/`date -I -d "1 day ago"` ]]; then
declare -r linkdest="--link-dest=${TARGET}/$(date -I -d "1 day ago")"
fi
# Do we use a default excludes file?
if [[ -f "/etc/hactar.excludes" && $EXCLUDE = "" ]]; then
EXCLUDE="/etc/hactar.excludes"
fi
# Run rsync with passed parameters
rsync --archive --one-file-system --hard-links --human-readable \
--inplace --numeric-ids --delete --delete-excluded --exclude-from=${EXCLUDE} \
${linkdest} ${SOURCE} ${TARGET}/`date -I` ${DRYRUN} ${VERBOSE}
}
delete_oldest_backup() {
declare -r oldest=`date -I -d "${RETAIN} days ago"`
if [ -d ${TARGET}/${oldest} ]; then
logger -t hactar "Deleting old backup from ${oldest}"
rm -r ${TARGET}/${oldest}
fi
}
main() {
parse_commandline_arguments "$@"
declare -r start=$(date +%s)
logger -t hactar "Starting backup from \"${SOURCE}\" to \"${TARGET}`date -I`\""
run_backup
delete_oldest_backup
declare -r end=$(date +%s)
declare -r runtime=$(python -c "print '%u minutes %02u seconds' % ((${end} - ${start})/60, (${end} - ${start})%60)")
logger -t hactar "Backup from \"${SOURCE}\" to \"${TARGET}`date -I`\" took ${runtime}"
}
main "$@"