Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 437 lines (348 sloc) 12.484 kb
68b1571 @falconindy Initial commit: extremely alpha
authored
1 #!/bin/bash
7f897e2 @falconindy Bump to version 1.0
authored
2 VER="1.0"
68b1571 @falconindy Initial commit: extremely alpha
authored
3
5a4021b @falconindy Squish some operational bugs. More to go...
authored
4 CONFIG="/etc/squashfu.conf"
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
5 . "$CONFIG"
63c55ee @falconindy Flesh out action_backup functionality -- commence testing
authored
6
7a830d0 @falconindy Add finish routine, cleanup unneeded code/comments
authored
7 # Informational output w/ happy colors
68b1571 @falconindy Initial commit: extremely alpha
authored
8 debug () {
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
9 [[ "$DEBUG" == "true" ]] && printf '\033[1;33mDEBUG ::\033[1;m %s\n' "$*"
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
10 }
11
480d999 @falconindy Implement warn() function in caution yellow
authored
12 warn () {
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
13 printf '\033[1;33m::\033[1;m %s\n' "$*"
480d999 @falconindy Implement warn() function in caution yellow
authored
14 }
15
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
16 info () {
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
17 printf '\033[1;34m::\033[1;m %s\n' "$*"
68b1571 @falconindy Initial commit: extremely alpha
authored
18 }
19
20 die () {
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
21 printf '\033[1;31m::\033[1;m %s\n' "$*" >&2
22 exit 1
68b1571 @falconindy Initial commit: extremely alpha
authored
23 }
e27598c @falconindy Add further debugging -- stop crashing you turd
authored
24
93a0e29 @falconindy Reorder functions
authored
25 create_new_squash () {
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
26 # Args: number of bins to be squashed, -1 on initial creation
93a0e29 @falconindy Reorder functions
authored
27 # Returns: 0 on success, non-zero on failure
28
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
29 # If making first seed, create it directly from source
30 if [[ $1 -eq -1 ]]; then
31 info "Creating seed from sources (this may take a while)"
32 mksquashfs "${INCLUDES[@]}" "$SEED" -b 65536 -e "${EXCLUDES[@]}" >/dev/null
33 if [[ $? -eq 0 ]]; then
34 mount_squash
35 info "Seed creation finished. It has been mounted at "$SQUASH_MOUNT" if you would like to make sure the proper files are included"
36 return 0;
37 else
38 die "There was an error creating the initial squash."
93a0e29 @falconindy Reorder functions
authored
39 fi
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
40 fi
93a0e29 @falconindy Reorder functions
authored
41
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
42 # Determine oldest $1 bins and mount them with the current squash
43 local old_bins=($(sort -n -r -t: -k2 "$BINVENTORY" | tail -$1 | cut -d: -f1))
93a0e29 @falconindy Reorder functions
authored
44
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
45 mount_union_with_bins ${old_bins[@]}
93a0e29 @falconindy Reorder functions
authored
46
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
47 info "Merging old incrementals"
48 # Create new squash with temp name, exiting on failure
49 mksquashfs "$UNION_MOUNT" "$SEED.replace" -b 65536 >/dev/null || return 1
93a0e29 @falconindy Reorder functions
authored
50
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
51 unmount_all
93a0e29 @falconindy Reorder functions
authored
52
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
53 # Replace old squash
54 mv "${SEED}.replace" "$SEED"
93a0e29 @falconindy Reorder functions
authored
55
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
56 info "Cleaning up inventory"
57 # Delete old bins, and remove entry from binventory
58 for bin in ${old_bins[@]}; do
59 rm -rf "${BINS_DIR}/$bin"
60 sed -i "/^$bin:/d" "$BINVENTORY"
61 done
93a0e29 @falconindy Reorder functions
authored
62
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
63 # Clean up $binventory
64 sweep_bins
93a0e29 @falconindy Reorder functions
authored
65 }
66
ecc1265 @falconindy Reorder and group functions by purpose
authored
67 create_new_bin () {
68 # Arguments: 1, the number of the bin to create
69 # Returns: 0 on success, non-zero on error
70
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
71 debug "Asked to create new bin: $1"
72 # Create new directory, fail if it exists (something's wrong)
73 mkdir "${BINS_DIR}/$1"
74 [[ $? != 0 ]] && return $?
ecc1265 @falconindy Reorder and group functions by purpose
authored
75
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
76 # Update binventory with new bin name and timestamp
77 echo "${1}:$(date +%s)" >> "$BINVENTORY"
ecc1265 @falconindy Reorder and group functions by purpose
authored
78
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
79 # If write to bin list fails, remove diretory and exit
80 if [[ $? -ne 0 ]]; then
81 rmdir "${BINS_DIR}/${1}"
82 die "Error writing to '$BINVENTORY'"
83 fi
ecc1265 @falconindy Reorder and group functions by purpose
authored
84
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
85 return
ecc1265 @falconindy Reorder and group functions by purpose
authored
86 }
87
88 # Mounting functions
985d175 @falconindy Complete rewrite with new structure. Basic framework and backup function...
authored
89 mount_squash () {
90 # Arguments: none
91 # Returns: return code of mount command
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
92 debug "Mounting Squash"
93 mount -o loop,ro "$SEED" "$SQUASH_MOUNT"
94 return $?
985d175 @falconindy Complete rewrite with new structure. Basic framework and backup function...
authored
95 }
96
97 mount_union_with_bins () {
98 # Arguments: numbers of bins to be mounted (variable number)
99 # Returns: 0 on successful mount, non-zero on failure
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
100 info "Mounting union"
101 debug "Requested to mount bins: $*"
102
103 # Mount first as rw, shift, and mount the rest ro
104 branches="br=${BINS_DIR}/$1=rw:"; shift
105 if [[ -n $1 ]]; then
106 for bin in $*; do
107 branches="${branches}${BINS_DIR}/$bin=ro:"
108 done
109 fi
110 branches="${branches}${SQUASH_MOUNT}=ro"
63f83db @falconindy Split off root priviledge check to its own function -- check for each di...
authored
111
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
112 mount -t aufs none "$UNION_MOUNT" -o udba=reval,$branches
97af6e6 @falconindy Split off directory structure creation to its own function
authored
113
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
114 return $?
6f32d3b @falconindy Alphabetize functions
authored
115 }
116
985d175 @falconindy Complete rewrite with new structure. Basic framework and backup function...
authored
117 # Unmounting functions
118 unmount_union () {
119 # Args: none
120 # Returns: return code from umount
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
121 info "Unmounting union"
122 while mountpoint -q "$UNION_MOUNT"; do
123 umount "$UNION_MOUNT" 2>/dev/null
124 sleep .5
125 done
126 return $?
eef98cf @falconindy Add support for checking custom mounts
authored
127 }
128
5a4021b @falconindy Squish some operational bugs. More to go...
authored
129 unmount_squash () {
985d175 @falconindy Complete rewrite with new structure. Basic framework and backup function...
authored
130 # Args: none
131 # Returns: return code from umount
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
132 info "Unmounting squash"
133 while mountpoint -q "$SQUASH_MOUNT"; do
134 umount "$SQUASH_MOUNT" 2>/dev/null
135 sleep .5
136 done
137 return $?
9aab188 @falconindy Split unmount_all into two separate functions, calling both for _all
authored
138 }
139
985d175 @falconindy Complete rewrite with new structure. Basic framework and backup function...
authored
140 unmount_all () {
141 # Args: none
142 # Returns: none
143
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
144 [[ $UID -ne 0 ]] && die "Must be root to unmount."
955ad5b @falconindy Add checks for root user in rollback, backup, and unmount
authored
145
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
146 # Union MUST be unmounted first
147 mountpoint -q "$UNION_MOUNT" && unmount_union
148 mountpoint -q "$SQUASH_MOUNT" && unmount_squash
7a830d0 @falconindy Add finish routine, cleanup unneeded code/comments
authored
149 }
150
ecc1265 @falconindy Reorder and group functions by purpose
authored
151 check_for_resquash () {
152 # Args: none
153 # Returns: number of bins needing to be merged
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
154 local number_of_bins=$(grep -vE "^[ ]*$" "$BINVENTORY" | wc -l)
155 debug "Found $number_of_bins bins"
156
157 if [[ $number_of_bins -gt $MAX_BINS ]]; then
158 return $(( $number_of_bins - $MIN_BINS ))
159 else
160 return 0
161 fi
ecc1265 @falconindy Reorder and group functions by purpose
authored
162 }
1f96ce1 @falconindy Add usage function and mount checking (and unmounting) in rollback funct...
authored
163
ecc1265 @falconindy Reorder and group functions by purpose
authored
164 get_next_available_bin () {
165 # Arguments: none
166 # Returns: Numeric value of the next unused bin
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
167 next_bin=$(( $(cut -d: -f1 "$BINVENTORY" | sort -n | tail -1) + 1 ))
168 debug "Next available bin = $next_bin"
169 return $next_bin
ecc1265 @falconindy Reorder and group functions by purpose
authored
170 }
1f96ce1 @falconindy Add usage function and mount checking (and unmounting) in rollback funct...
authored
171
ecc1265 @falconindy Reorder and group functions by purpose
authored
172 sweep_bins () {
173 # Arguments: none
174 # Returns: none
1f96ce1 @falconindy Add usage function and mount checking (and unmounting) in rollback funct...
authored
175
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
176 info "Rotating chickens"
177 # Make sure bins are numbered in order, clean up if not. In other words,
178 # if we have 10 bins, make sure they're ordered 1 through 10.
179 count=1
ca48c8e @falconindy minor bug fixes
authored
180 for bin in "${BINS_DIR}"; do
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
181 if [[ ! -d "${BINS_DIR}/$count" ]]; then
182 high_bin=$(ls "${BINS_DIR}" | sort -n | tail -1)
183 debug "Sweeping bin $high_bin into bin $count"
184 mv "${BINS_DIR}/$high_bin" "${BINS_DIR}/$count"
185 sed -i "/^$high_bin:/s/^$high_bin:/$count:/" "$BINVENTORY"
186 fi
187 count=$(( $count + 2 ))
188 done
1f96ce1 @falconindy Add usage function and mount checking (and unmounting) in rollback funct...
authored
189 }
190
20fe278 @falconindy Create skeletons for action handlers
authored
191 action_backup () {
63c55ee @falconindy Flesh out action_backup functionality -- commence testing
authored
192 # Args: options array squashfu was invoked with, shifted 1
193 # Returns: none
194
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
195 [[ $UID -ne 0 ]] && die "Must be root to perform a backup"
63c55ee @falconindy Flesh out action_backup functionality -- commence testing
authored
196
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
197 # Does the binventory exist? If not, prompt to make sure this is an initialization
198 if [[ ! -f "$BINVENTORY" || ! -f "$SEED" ]]; then
199 echo -ne "\033[1;33m::\033[0m"
200 read -N1 -p "Looks like this is your first time running SquashFu. Is this correct? [y/N] " reply
201 echo
202 [[ ! "$reply" =~ [Yy] ]] && die "Your bin inventory and/or seed seem to be missing. Please fix this before continuing."
ceb3329 @falconindy Refine output, at both info and debug levels
authored
203
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
204 # If we got here, the user answered yes, so initialize a new structure
205 mkdir -p "$UNION_MOUNT" "$SQUASH_MOUNT" "${BINS_DIR}"
206 touch "$BINVENTORY"
207 create_new_squash -1
208 exit 0
209 fi
6ad518d @falconindy Debug action_rollback. Ensure unmount_all runs before we try to do a bac...
authored
210
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
211 info "Backup requested at $(date --rfc-3339=seconds)"
5570ac4 @falconindy Merge create_new_incremental() into action_backup()
authored
212
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
213 # Cleanup union, in case user was doing a rollback and forgot to unmount (or error on last run)
214 mountpoint "$UNION_MOUNT" &>/dev/null && unmount_union
5570ac4 @falconindy Merge create_new_incremental() into action_backup()
authored
215
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
216 info "Creating new bin"
217 # Make a new bin for this incremenetal
218 get_next_available_bin
219 local new_bin=$?
220 create_new_bin $new_bin
5570ac4 @falconindy Merge create_new_incremental() into action_backup()
authored
221
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
222 # Determine the mount order via binventory
223 local bin_order=($(sort -n -r -t: -k2 "$BINVENTORY" | cut -d: -f1))
5570ac4 @falconindy Merge create_new_incremental() into action_backup()
authored
224
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
225 mountpoint "${SQUASH_MOUNT}" &>/dev/null || mount_squash
226 mount_union_with_bins ${bin_order[@]} || return 1
5570ac4 @falconindy Merge create_new_incremental() into action_backup()
authored
227
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
228 # Includes are pulled in directly from config
229 EXCLUDES=$(for excl in ${EXCLUDES[@]}; do echo --exclude $excl; done)
63c55ee @falconindy Flesh out action_backup functionality -- commence testing
authored
230
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
231 debug "rsync ${RSYNC_OPTS[@]} ${INCLUDES[@]} ${EXCLUDES[@]} "$UNION_MOUNT""
232 info "Creating new incremental"
233 /usr/bin/rsync ${RSYNC_OPTS[@]} ${INCLUDES[@]} ${EXCLUDES[@]} "$UNION_MOUNT"
234 rsync_ret=$?
647eb3e @falconindy New branch. Goal: optional rollback when rsync fails
authored
235
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
236 for error in ${DEL_BIN_ON_FAIL[@]}; do
237 if [[ $rsync_ret == $error ]]; then
238 warn "Unexpected hangup by rsync ($error). Deleting backup."
239 action_remove_bin $new_bin override
240 break
63c55ee @falconindy Flesh out action_backup functionality -- commence testing
authored
241 fi
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
242 done
20fe278 @falconindy Create skeletons for action handlers
authored
243
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
244 check_for_resquash
ca48c8e @falconindy minor bug fixes
authored
245 [[ val=$? -gt 0 ]] && create_new_squash $val
63c55ee @falconindy Flesh out action_backup functionality -- commence testing
authored
246
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
247 # TODO: Report if requested
ceb3329 @falconindy Refine output, at both info and debug levels
authored
248
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
249 unmount_all
250
251 info "Backup completed at $(date --rfc-3339=seconds)"
20fe278 @falconindy Create skeletons for action handlers
authored
252 }
253
c2ea987 @falconindy Create function to remove a bin, optionally interactive
authored
254 action_remove_bin () {
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
255 # check if the bin exists both in the binventory AND in the bins directory
256 [[ $UID != 0 ]] && die "Must be root to remove a backup"
257 [[ ! -w "$BINVENTORY" ]] && die "Unable to write to ${BINVENTORY}"
258
ca48c8e @falconindy minor bug fixes
authored
259 if grep -E "^$1:" ${BINVENTORY} && [[ -d "${BINS_DIR}/$1" ]]; then
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
260 if [[ -z $2 ]]; then
261 echo "Are you SURE you want to remove this bin?"
262 local timestamp=$(sed -n "/^$1:/s/^[0-9]*:\([0-9]*\)/\1/p" "${BINVENTORY}")
263 printf "\t%15s %s\n\t%15s %s\n\t%15s %s\n" \
264 "Bin:" "$1" \
265 "Date Created:" "$(date --rfc-3339=seconds --date="@$timestamp")" \
266 "Size:" "$(du -sh "${BINS_DIR}/$1" 2>/dev/null | awk '{print $1}')"
267
ca48c8e @falconindy minor bug fixes
authored
268 echo -ne "\033[1;33m::\033[0m"
269 read -N1 -p "Confirm deletion (y/N) " reply
270 echo
271 [[ ! "$reply" =~ [Yy] ]] && die "Delete operation aborted"
205426e @falconindy Check UID and that .bin.list is writeable before proceeding with delete
authored
272 fi
273
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
274 info "Deleting bin $1"
275 sed -i "/^$1:[0-9]*/d" "${BINVENTORY}"
276 rm -rf ${BINS_DIR}/$1
c9e85ab @falconindy Call sweep_bins after a bin is deleted
authored
277
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
278 # tidy up!
279 sweep_bins
280 else
281 die "Bin $1 not found."
282 fi
c2ea987 @falconindy Create function to remove a bin, optionally interactive
authored
283 }
284
20fe278 @falconindy Create skeletons for action handlers
authored
285 action_rollback () {
955ad5b @falconindy Add checks for root user in rollback, backup, and unmount
authored
286 # Args: number of backups to roll back
287 # Returns: none
288
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
289 [[ $UID -ne 0 ]] && die "Must be root to perform a rollback"
f2958c0 @falconindy rollback: sanitize input. insist that the argument passed is purely nume...
authored
290 [[ ! $1 =~ ^[0-9]*$ ]] && die "Invalid argument. Parameter must be a number."
146d12d @falconindy Create rollback action handler. Don't rely on wc -l alone to count bin i...
authored
291
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
292 # Form a chronologically ordered list of bins, assuming the user didn't give bogus input
f2958c0 @falconindy rollback: sanitize input. insist that the argument passed is purely nume...
authored
293 local bin_list=($(sed -n 's/[\t\ ]*\([0-9]*\):.*/\1/p' "$BINVENTORY"))
146d12d @falconindy Create rollback action handler. Don't rely on wc -l alone to count bin i...
authored
294
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
295 [[ $1 -gt ${#bin_list[@]} ]] && die "Cannot rollback more than ${#bin_list[@]} backups"
146d12d @falconindy Create rollback action handler. Don't rely on wc -l alone to count bin i...
authored
296
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
297 local num_to_mount=$(( ${#bin_list[@]} - $1 ))
146d12d @falconindy Create rollback action handler. Don't rely on wc -l alone to count bin i...
authored
298
f2958c0 @falconindy rollback: sanitize input. insist that the argument passed is purely nume...
authored
299 mountpoint -q "$UNION_MOUNT" && unmount_union
300 mountpoint -q "$SQUASH_MOUNT" || mount_squash
146d12d @falconindy Create rollback action handler. Don't rely on wc -l alone to count bin i...
authored
301
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
302 mount_union_with_bins ${bin_list[@]:(-$num_to_mount)}
20fe278 @falconindy Create skeletons for action handlers
authored
303
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
304 local rb_timestamp=$(grep -E "^${bin_list[@]:(-$num_to_mount):1}:" "$BINVENTORY" | cut -d: -f2)
5f75b00 @falconindy Show date of rollback when operation completes
authored
305
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
306 info "You have rolled back to $(date --rfc-3339=seconds --date="@$rb_timestamp")"
307 info "Your files can be found at '${UNION_MOUNT}'"
20fe278 @falconindy Create skeletons for action handlers
authored
308 }
309
310 action_report () {
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
311 info "SquashFu Usage Report"
312 echo
313 # Enumerate bins, sort date order, print human readable create date and size
314
315 cd "$BINS_DIR"
316 OLDIFS=$IFS;IFS=${IFS}:
317 # Collect all data into an array to 'preload' it. Index 0 is the entire
318 # folder. The following indicies correspond to the bin number of that index
319 printf "%30s\r" " .: Loading :." >&2
320 DATA=($(du -sh . * 2>/dev/null | sort -n -k2 | awk '{print $1}'))
321 printf "%30s\r" " " >&2
322
323 printf "%10s\t%25s\t%7s\n" "Bin ID" "Date Created" "Size"
324 grep -vE "^[\t ]*$" "$BINVENTORY" | sort -r -t: -k2 -n | while read bin stamp; do
325 printf "%10d\t%25s\t%7s\n" "$bin" \
326 "$(date --rfc-3339=seconds --date="@$stamp")" \
327 "${DATA[$bin]}"
328 done
329 IFS=$OLDIFS
330 printf "%10s\t%25s\t%7s\n" "" "Incremental Total" "${DATA[0]}"
331
332 # Print totals (not efficient -- reruns du on things we already ran it on)
333 printf "\n%10s\t%25s\t%7s\n" "" "$(basename $SEED)" "$(du -h "${SEED}" | awk '{print $1}')"
334 printf "\n%10s\t%25s\t%7s\n" "" "Grand Total" \
335 "$(du -csh "$BINS_DIR" "$SEED" 2>/dev/null | awk '/total$/{print $1}')"
20fe278 @falconindy Create skeletons for action handlers
authored
336 }
337
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
338 action_resquash_now () {
339 # Args: none
340 # Returns: none
341
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
342 [[ $UID != 0 ]] && die "Must be root to perform a resquash"
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
343
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
344 info "Voluntary resquash requested"
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
345
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
346 local number_of_bins=$(grep -vE "^[ ]*$" "$BINVENTORY" | wc -l)
347 if [[ $number_of_bins -le $MIN_BINS ]]; then
348 die "Nothing to do. Current backups do not exceed MIN_BINS value."
349 else
350 create_new_squash $(( $number_of_bins - $MIN_BINS ))
351 fi
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
352
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
353 exit $?
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
354 }
355
de17d54 @falconindy Conform -U option to set action and not immediately execute
authored
356 action_unmount () {
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
357 unmount_all
de17d54 @falconindy Conform -U option to set action and not immediately execute
authored
358 }
359
ecc1265 @falconindy Reorder and group functions by purpose
authored
360 usage () {
955ad5b @falconindy Add checks for root user in rollback, backup, and unmount
authored
361 info "SquashFu: Super Awesome Backup Express (Professional Edition)"
dd62660 @falconindy Add versioning
authored
362 echo "version: $VER"
ecc1265 @falconindy Reorder and group functions by purpose
authored
363 cat <<HELP
364
365 USAGE
40ca97e @falconindy Add -c option for specifying alternate config
authored
366 squashfu <action> [options]
ecc1265 @falconindy Reorder and group functions by purpose
authored
367
2818589 @falconindy Change 'operations' to 'actions' in usage heredoc
authored
368 ACTIONS
ecc1265 @falconindy Reorder and group functions by purpose
authored
369 -B
a52b1f4 @falconindy Update usage heredoc to actually be accurate for currently supported fea...
authored
370 Runs a regular backup, using the config file at /etc/squashfu.
ecc1265 @falconindy Reorder and group functions by purpose
authored
371
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
372 -C
373 Create a new squash by merging old bins. This will still leave you
ddcb110 @falconindy Fix typo in usage
authored
374 with $MIN_BINS backups (as per the MIN_BINS setting in your config).
1b2a164 @falconindy Add option to manually resquash down to MIN_BINS
authored
375
168655f @falconindy Add remove bin option to usage heredoc and options parser
authored
376 -D <bin number>
377 Delete an incremental backup. This is done interactively and you will have
378 a chance to confirm before any files are deleted.
379
ecc1265 @falconindy Reorder and group functions by purpose
authored
380 -Q
a52b1f4 @falconindy Update usage heredoc to actually be accurate for currently supported fea...
authored
381 Displays the size of the seed, the incrementals, and totals.
ecc1265 @falconindy Reorder and group functions by purpose
authored
382
383 -R <number of bins>
384 Rollback specified number of backups and mount union for browsing. The rolled
385 back data will be mounted at $UNION_MOUNT.
386
387 -U
388 Unmount squash and union. Although SquashFu will always check and unmount as
389 necessary before an operation, this is provided as a safeguard.
390
1723f51 @falconindy Correct spacing on usage heredoc
authored
391 OPTIONS
40ca97e @falconindy Add -c option for specifying alternate config
authored
392 -c <path_to_config>
393 Specify an alternate config file. Options defined will override options in
394 the default config. If you specify alternate locations via a supplmenetal config,
395 you will need to provide the config for all actions
396
ecc1265 @falconindy Reorder and group functions by purpose
authored
397 HELP
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
398 exit 1
ecc1265 @falconindy Reorder and group functions by purpose
authored
399 }
400
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
401 [[ $# -eq 0 ]] && usage
402
168655f @falconindy Add remove bin option to usage heredoc and options parser
authored
403 while getopts :BCD:QR:Uc: opt; do
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
404 case $opt in
405 B)
406 [[ -n $action ]] && die "only one operation may be used at a time"
407 action=backup ;;
408 C)
409 [[ -n $action ]] && die "only one operation may be used at a time"
410 action=resquash_now ;;
411 D)
412 [[ -n $action ]] && die "only one operation may be used at a time"
413 action="remove_bin $OPTARG" ;;
414 Q)
415 [[ -n $action ]] && die "only one operation may be used at a time"
416 action=report ;;
417 R)
418 [[ -n $action ]] && die "only one operation may be used at a time"
419 action="rollback $OPTARG" ;;
420 U)
421 [[ -n $action ]] && die "only one operation may be used at a time"
422 action=unmount ;;
423 c)
424 [[ -f $OPTARG ]] && source $OPTARG ;;
425 \:)
426 die "Argument missing from -$OPTARG"
427 usage ;;
428 \?)
429 die "Unrecognized option -$OPTARG"
430 usage ;;
431 esac >&2
dcff2dd @falconindy Reimplement options using getopts
authored
432 done
433
83c7e76 @falconindy overhaul. redo indents to 2 spaces. use short circuit boolean instead of...
authored
434 [[ -z $action ]] && usage
435
436 action_$action
Something went wrong with that request. Please try again.