diff options
author | Robin Obůrka <r.oburka@gmail.com> | 2016-07-24 15:32:42 +0200 |
---|---|---|
committer | Robin Obůrka <r.oburka@gmail.com> | 2016-07-24 16:34:02 +0200 |
commit | df898514eb3107b41e7c00b6c2ac17a4f3243472 (patch) | |
tree | e4f67532faedb0388be7f4d8a6c072a9a1e99051 /backuper.sh | |
parent | Add support for after-backup hook (diff) | |
download | backuper-df898514eb3107b41e7c00b6c2ac17a4f3243472.tar.xz |
Reorganize file to sections
Diffstat (limited to 'backuper.sh')
-rw-r--r-- | backuper.sh | 134 |
1 files changed, 69 insertions, 65 deletions
diff --git a/backuper.sh b/backuper.sh index 88fa769..ddaac26 100644 --- a/backuper.sh +++ b/backuper.sh @@ -25,13 +25,48 @@ STAGE="0" ## Internal variables - program logic PERSN_FILE_NAME=".backuper.sh.per" -test_input() { - [ -z "$NAME" ] && error "Specify backup name" - [ -z "$COMMAND" -a -z "$PLACES" ] && error "Specify backup subject" - [ -n "$COMMAND" -a -n "$PLACES" ] && error "Do not specify two types of backup at once" - [ -z "$STORE" ] && error "Specify store command" +## Output helpers +title() { + TXT="$1" + printf "\n%s\n==========================================\n" "$TXT" +} + +get_size() { + FILE="$1" + du -h "$FILE" | awk '{ print $1 }' +} + +## Getting data helpers +get_home() { + CURRENT_USER="$(id --real --user --name)" + CU_HOME="$(getent passwd "$CURRENT_USER" | cut -d: -f6)" + echo "$CU_HOME" +} + +result_file() { + if [ -n "$DST" ]; then + echo "$DST"/"$FILENAME_ENC" + else + echo "$FILENAME_ENC" + fi +} + +## Program control +error() { + echo "ERROR: $1" >&2 + exit 1 } +die() { + if [ -n "$1" ]; then + echo "DIE: $1" >&2 + fi + ## -6 ~ SIGABRT + ## This is the only SIGNAL that cleanup doesn't reset + kill -6 "$$" +} + +## Interface functions name() { NAME="$1" compute_variables @@ -70,18 +105,24 @@ after() { AFTER="$1" } -error() { - echo "ERROR: $1" >&2 - exit 1 +run() { + title "Run command" + echo "$@" + echo $@ | $REMOTE sh -s + printf "\n" } -die() { - if [ -n "$1" ]; then - echo "DIE: $1" >&2 - fi - ## -6 ~ SIGABRT - ## This is the only SIGNAL that cleanup doesn't reset - kill -6 "$$" +store_file() { + [ -z "$1" ] && error "Specify destination" + store "sh -c 'cat > ${1}/${FILENAME_ENC}'" +} + +## Internal functions +test_input() { + [ -z "$NAME" ] && error "Specify backup name" + [ -z "$COMMAND" -a -z "$PLACES" ] && error "Specify backup subject" + [ -n "$COMMAND" -a -n "$PLACES" ] && error "Do not specify two types of backup at once" + [ -z "$STORE" ] && error "Specify store command" } compute_variables() { @@ -92,30 +133,6 @@ compute_variables() { RUNDIR="/tmp/backuper_run_dir_$$_$UTS" } -title() { - TXT="$1" - printf "\n%s\n==========================================\n" "$TXT" -} - -get_size() { - FILE="$1" - du -h "$FILE" | awk '{ print $1 }' -} - -get_home() { - CURRENT_USER="$(id --real --user --name)" - CU_HOME="$(getent passwd "$CURRENT_USER" | cut -d: -f6)" - echo "$CU_HOME" -} - -result_file() { - if [ -n "$DST" ]; then - echo "$DST"/"$FILENAME_ENC" - else - echo "$FILENAME_ENC" - fi -} - prepare() { echo "prepare $$" trap 'error_handler' EXIT INT QUIT TERM ABRT HUP ILL TRAP BUS FPE SEGV @@ -154,6 +171,18 @@ cleanup() { fi } +backup_done() { + if [ -n "$AFTER" ]; then + title "After backup hook" + echo "$AFTER" | sh -s || die "After-backup hook failed" + fi +} + +personalization() { + HOME="$(get_home)" + [ -f "$HOME"/"$PERSN_FILE_NAME" ] && . "$HOME"/"$PERSN_FILE_NAME" +} + add_stage() { ## Prepare pipes NEXTSTAGE="$((STAGE+1))" @@ -205,36 +234,11 @@ start_pipeline() { done } -backup_done() { - if [ -n "$AFTER" ]; then - title "After backup hook" - echo "$AFTER" | sh -s || die "After-backup hook failed" - fi -} - -run() { - title "Run command" - echo "$@" - echo $@ | $REMOTE sh -s - printf "\n" -} - -store_file() { - [ -z "$1" ] && error "Specify destination" - store "sh -c 'cat > ${1}/${FILENAME_ENC}'" -} - backup() { test_input prepare + personalization start_pipeline backup_done cleanup } - -main() { - HOME="$(get_home)" - [ -f "$HOME"/"$PERSN_FILE_NAME" ] && . "$HOME"/"$PERSN_FILE_NAME" -} - -main |