-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-snapshot
executable file
·231 lines (186 loc) · 6.51 KB
/
make-snapshot
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
#!/bin/bash
############################################################
##################### CONSTANTS ############################
############################################################
Timefile=".snapshot-timestamp"
indent() { sed 's/^/ /'; }
die() { echo "Error: $1 (code=$2)" >&2 ; exit $2; }
warn() { echo "Warning: $1" >&2 ; }
############################################################
set -o pipefail -o nounset
Self=$(basename $0)
OPTIONS=ivn
LONGOPTS=initial,verbose,dry-run
# long options to short ones
for arg in "$@"
do
shift
case "$arg" in
"--initial") set -- "$@" "-i" ;;
"--dry-run") set -- "$@" "-n" ;;
"--verbose") set -- "$@" "-v" ;;
*) set -- "$@" "$arg"
esac
done
# defaults
Initial=n
Verbose=n
DryRun=n
# parse options
OPTIND=1
while getopts "inv" opt
do
case "$opt" in
"i") Initial=y ;;
"n") DryRun=y ;;
"v") Verbose=y ;;
"?") die "unknown option" 10 ;;
esac
done
shift $(expr $OPTIND - 1) # remove options from positional parameters
# handle non-option arguments
[[ $# -gt 1 ]] && die "$Self: only one single configuration file can be provided." 12
############################################################
if [ $# -eq 1 ]
then
[ -f "$1" -a -r "$1" ] || die "$Self: configuration file $1 not found or not readable." 20
# source the job config
source "$1"
if [ -z ${Job+x} ]; then
Job=$(basename -s .conf $1)
fi
Self="${Self}@${Job}"
fi
# check whether all mandatory variables were set in config file or environment
[ -z ${LIB_DIR+x} ] && die "State directory (LIB_DIR) is required." 21
[ -z ${LOG_DIR+x} ] && die "Log directory (LOG_DIR) is required." 21
[ -z ${TAG+x} ] && die "Snapshot tag format (TAG) is required." 21
[ -z ${BACKUP_TREE+x} ] && die "Tree to be snapshotted (BACKUP_TREE) is required." 21
[ -z ${BACKUP_FILTER+x} ] && die "Filter filename (BACKUP_FILTER) is required." 21
[ -z ${REMOTE_BASE+x} ] && die "Destination (REMOTE_BASE) is required." 21
[ -z ${Job+x} ] && die "Job name (Job) is required." 21
############################################################
############################################################
############################################################
if [ -e "${LIB_DIR}/${Job}.last" ]
then
# if we have a last snapshot tag, we shouldn't be on an initial run
[ "$Initial" = "y" ] && die "Error: initial run chosen, but prior snapshot exists. Remove ${LIB_DIR}/${Job}.last to recreate initial snapshot." 30
[ -r "${LIB_DIR}/${Job}.last" ] || die "${LIB_DIR}/${Job}.last is not readable." 31
Oldstamp="$(cat "${LIB_DIR}/${Job}.last")"
else
# if we have no last snapshot tag saved, we should have been called as initial run
[ "$Initial" != "y" ] && die "Error: cannot read tag of last snapshot from ${LIB_DIR}/${Job}.last, and --initial was not passed." 32
fi
############################################################
touch "${BACKUP_TREE}/${Timefile}" || die "Cannot touch(1) timestamp file '${BACKUP_TREE}/${Timefile}'" 33
chmod 0444 "${BACKUP_TREE}/${Timefile}" || warn "Could not make timestamp file '${BACKUP_TREE}/${Timefile}' read-only."
Timestamp="$(date ${TAG_OPTIONS:-} -r "${BACKUP_TREE}/${Timefile}" +"${TAG}")"
[ -z ${Timestamp+x} ] && die "Problem generating new snapshot tag." 34
echo "Creating new snapshot of ${BACKUP_TREE}:"
echo " new tag: $Timestamp"
echo " last tag: ${Oldstamp:-INITIAL RUN}"
echo " target: $REMOTE_BASE"
echo
[ -n "${LOCK_DIR:+x}" ] && [ -e "${LOCK_DIR}/${Job}" ] && die "Snapshot already in progress, exiting." 40
echo -n "1. Placing lock file: "
if [ -n "${LOCK_DIR:+x}" ] # lock dir is optional
then
if [ ! -d "${LOCK_DIR}" ] # it might rest on a temporary filesystem
then
mkdir -p ${LOCK_DIR} || die "Failed, could not create directory ${LOCK_DIR}." 50
fi
touch "${LOCK_DIR}/${Job}" && echo "OK." || die "Failed." 55
else
echo "[ skipped - LOCK_DIR not set]"
fi
############################################################
if [ -z ${Oldstamp+x} ]
then
echo "2. Initial run, skipping check of previous snapshot."
else
echo -n "2. Checking if last tag exists: "
rsync "${REMOTE_AUTH[@]}" "${REMOTE_BASE}$Oldstamp/" > /dev/null
RsyncExit=$?
if [ $RsyncExit -eq 0 ]
then
echo "OK."
else
die "Failed: rsync returned $RsyncExit." 60
fi
fi
############################################################
if [ ! -d ${LOG_DIR}/${Job} ]
then
mkdir -p ${LOG_DIR}/${Job} || die "Error: could not create log directory ${LOG_DIR}/${Job}." 70
fi
Logfile="${LOG_DIR}/${Job}/${Timestamp:-$(date +%Y-%m-%d_%H:%M%z)}"
############################################################
### real work starts here ##################################
############################################################
echo -n "3. Creating snapshot: "
if [ -z ${Oldstamp+x} ] || [ "${Oldstamp}" = "${Timestamp}" ]
then
[ -z ${Oldstamp+x} ] && echo -n "(initial) " || echo -n "(static) "
#
# initial snapshot or static tag - rsync in default mode
#
if [ "$DryRun" = "n" ]
then
rsync -ai --filter="dir-merge ${BACKUP_FILTER}" "${REMOTE_AUTH[@]:---}" "${BACKUP_TREE}/" "${REMOTE_BASE}$Timestamp/" > ${Logfile}
RsyncExit=$?
else
echo
rsync -avn --filter="dir-merge ${BACKUP_FILTER}" "${REMOTE_AUTH[@]:---}" "${BACKUP_TREE}/" "${REMOTE_BASE}$Timestamp/"
RsyncExit=$?
fi
else
echo -n "(timestamped) "
#
# changing tag: snapshot-style, rsync with --link-dest
#
if [ "$DryRun" = "n" ]
then
rsync -ai --filter="dir-merge ${BACKUP_FILTER}" --link-dest="../$Oldstamp" "${REMOTE_AUTH[@]:---}" "${BACKUP_TREE}/" "${REMOTE_BASE}$Timestamp/" > ${Logfile}
RsyncExit=$?
else
echo
rsync -avn --filter="dir-merge ${BACKUP_FILTER}" --link-dest="../$Oldstamp" "${REMOTE_AUTH[@]:---}" "${BACKUP_TREE}/" "${REMOTE_BASE}$Timestamp/"
RsyncExit=$?
fi
fi
if [ $RsyncExit -eq 0 -o $RsyncExit -eq 24 ] # code 24: source file vanished during transfer, that can happen
then
echo "OK."
else
die "Failed: rsync returned $RsyncExit." 71
fi
############################################################
echo -n "4. Saving current tag to ${LIB_DIR}/${Job}.last: "
if [ ! -d ${LIB_DIR} ]
then
mkdir -p ${LIB_DIR}
if [ $? -ne 0 ]
then
die "Error: could not create state directory ${LIB_DIR}." 80
fi
fi
if [ "$DryRun" = "n" ]
then
echo $Timestamp > ${LIB_DIR}/${Job}.last
if [ $? -eq 0 ]
then
echo "OK."
else
die "Failed." 81
fi
else
echo "[skipped - dry run]"
fi
echo -n "5. Removing lock file: "
if [ -n "${LOCK_DIR:+x}" ] # lock dir is optional
then
rm -f "${LOCK_DIR}/${Job}" && echo "Done." || echo "Failed."
else
echo "[ skipped - LOCK_DIR not set]"
fi