diff options
-rwxr-xr-x | squashfu | 190 |
1 files changed, 95 insertions, 95 deletions
@@ -18,6 +18,83 @@ CONFIG=/etc/squashfu.conf source $CONFIG [[ $? -gt 0 ]] && die "Error in config file. Please check your syntax" +create_new_seed () { + # Create a new squashfs based on the contents of the union + # It's okay if we make an empty squash, we'll tell the user + # about it at the end and suggest --resquash + debug "Making new squash seed $(basename $SEED)" + mksquashfs "${BKUP_ROOT}/rw" "$SEED" -b 65536 + + # Delete the rsync source since its now squashed + #[[ "$1" == "--initial" ]] && rm -rf "${BKUP_ROOT}/rw/*" +} + +# The meat and potatoes of this bad agent. +do_backup () { + ################################ + # Sanity checks + ################################ + # - Are we root? + # - is our BKUP_ROOT valid? (FAIL) + # - Check for pre-existing mounts just in case (and unmount them) + # - do we have a proper (expected) directory structure in place? + # (Use cd to BKUP_ROOT to avoid issues with brace expansion in a quoted path) + [[ $UID -eq 0 ]] || die Must be root! + + [[ -w "${BKUP_ROOT}" ]] || + die "Backup root is not accessible. Please check your setting in /etc/squashfu" + + unmount_all + + cd "$BKUP_ROOT" && mkdir -p {rw,ro,bins/{1,2,3,4,5,6,7}} + + + ################################ + # Prep work + ################################ + # - does seed exist? (if not, our backup is creating the seed) + # - Prepare union mount with proper bins + [[ -f "$SEED" ]] || { + debug "No seed found -- creating a new one..."; + create_new_seed; + } + + mount_union_ro $(( $(date +%u) + $MODIFIER )) + + mount_union_branch_rw $(( $(date +%u) + $MODIFIER )) + + + ################################ + # Ready for backup! + ################################ + run_rsync + + + ################################ + # Cleanup + ################################ + # - Is this resquash day? If so, we need a new squash + # - If new squash creation fails, we're in trouble. (by default, keep previous week) + [[ $(date +%u) -eq $RESQUASH_DAY ]] && { + create_new_seed + # Set aside last week's tree if user opted to, else delete it all + if [[ $KEEP_LAST_WEEK -eq 1 ]]; then + move_old_tree + else + find "${BKUP_ROOT}/bins/" -type f -delete + rm $SEED + fi + } +} + +mount_seed () { + debug "Mounting seed" + # Mount the squashed seed, failing if we can't + mount -o loop,ro "${SEED}" "${BKUP_ROOT}/ro" || { + die FATAL: Error mounting $SEED; + } +} + mount_union_ro () { # build branch string branches="br=" @@ -31,39 +108,36 @@ mount_union_ro () { mount -t aufs none $BKUP_ROOT/rw -o udba=reval,$branches } +mount_union_up_to_day () { + # convert DoW to a number + mount_union_ro `date --date=$1 +%u` +} + mount_union_branch_rw () { debug "Remount branch $i as read-write" mount -o remount,mod:bins/$1=rw "${BKUP_ROOT}/rw" } -create_new_seed () { - # Create a new squashfs based on the contents of the union - # It's okay if we make an empty squash, we'll tell the user - # about it at the end and suggest --resquash - debug "Making new squash seed $(basename $SEED)" - mksquashfs "${BKUP_ROOT}/rw" "$SEED" -b 65536 - - # Delete the rsync source since its now squashed - #[[ "$1" == "--initial" ]] && rm -rf "${BKUP_ROOT}/rw/*" -} - move_old_tree () { storage="${BKUP_ROOT}/bkup-$(date +%Y-%m-%d)" mkdir "$storage" cd "$BKUP_ROOT" && mv {$SEED,bins/} "$storage" } -mount_seed () { - debug "Mounting seed" - # Mount the squashed seed, failing if we can't - mount -o loop,ro "${SEED}" "${BKUP_ROOT}/ro" || { - die FATAL: Error mounting $SEED; - } -} +query_usage () { + # Do another sanity check -- check sizes of bins versus squash. + bin_size=$(du -s ${BKUP_ROOT}/bins 2>/dev/null | awk '{print $1}') + sfs_size=$(stat -c %s $SEED 2>/dev/null) -mount_aufs_by_day() { - # convert DoW to a number - mount_union_ro `date --date=$1 +%u` + info "Usage report (in mb):\n" + printf "%30s %10.2f\n" "Seed size:" "`echo "scale=2;$sfs_size / 1024 / 1024" | bc`" + printf "%30s %10.2f\n" "Total incremental size:" "`echo "scale=2;$bin_size / 1024 / 1024" | bc`" + + if [[ $bin_size -gt $sfs_size ]]; then + info "Your incrementals are larger than your seed! You might consider resquashing your backup with $0 --resquash" + fi + + #unmount_all } run_rsync() { @@ -90,22 +164,6 @@ unmount_all () { { debug Unmounting squash...; umount "${SEED}"; } } -query_usage () { - # Do another sanity check -- check sizes of bins versus squash. - bin_size=$(du -s ${BKUP_ROOT}/bins 2>/dev/null | awk '{print $1}') - sfs_size=$(stat -c %s $SEED 2>/dev/null) - - info "Usage report (in mb):\n" - printf "%30s %10.2f\n" "Seed size:" "`echo "scale=2;$sfs_size / 1024 / 1024" | bc`" - printf "%30s %10.2f\n" "Total incremental size:" "`echo "scale=2;$bin_size / 1024 / 1024" | bc`" - - if [[ $bin_size -gt $sfs_size ]]; then - info "Your incrementals are larger than your seed! You might consider resquashing your backup with $0 --resquash" - fi - - #unmount_all -} - usage () { info "SquashFu: a backup solution hewn out of boredom" cat <<HELP @@ -143,64 +201,6 @@ HELP exit } -# The meat and potatoes of the script. -do_backup () { - ################################ - # Sanity checks - ################################ - # - Are we root? - # - is our BKUP_ROOT valid? (FAIL) - # - Check for pre-existing mounts just in case (and unmount them) - # - do we have a proper (expected) directory structure in place? - # (Use cd to BKUP_ROOT to avoid issues with brace expansion in a quoted path) - [[ $UID -eq 0 ]] || die Must be root! - - [[ -w "${BKUP_ROOT}" ]] || - die "Backup root is not accessible. Please check your setting in /etc/squashfu" - - unmount_all - - cd "$BKUP_ROOT" && mkdir -p {rw,ro,bins/{1,2,3,4,5,6,7}} - - - ################################ - # Prep work - ################################ - # - does seed exist? (if not, our backup is creating the seed) - # - Prepare union mount with proper bins - [[ -f "$SEED" ]] || { - debug "No seed found -- creating a new one..."; - create_new_seed; - } - - mount_union_ro $(( $(date +%u) + $MODIFIER )) - - mount_union_branch_rw $(( $(date +%u) + $MODIFIER )) - - - ################################ - # Ready for backup! - ################################ - run_rsync - - - ################################ - # Cleanup - ################################ - # - Is this resquash day? If so, we need a new squash - # - If new squash creation fails, we're in trouble. (by default, keep previous week) - [[ $(date +%u) -eq $RESQUASH_DAY ]] && { - create_new_seed - # Set aside last week's tree if user opted to, else delete it all - if [[ $KEEP_LAST_WEEK -eq 1 ]]; then - move_old_tree - else - find "${BKUP_ROOT}/bins/" -type f -delete - rm $SEED - fi - } -} - #Option parsing / Decision making [[ $# -eq 0 ]] && usage |