aboutsummaryrefslogtreecommitdiffstats
path: root/backuper.sh
diff options
context:
space:
mode:
authorGravatar Robin Obůrka <r.oburka@gmail.com> 2016-07-22 22:02:20 +0200
committerGravatar Robin Obůrka <r.oburka@gmail.com> 2016-07-24 16:34:02 +0200
commite1a60121f774877f174b850c1abba5de2b8f0d39 (patch)
tree9b232c208eb1dbfedb55ecfa3ba74521509c4c46 /backuper.sh
parentInitial commit (diff)
downloadbackuper-e1a60121f774877f174b850c1abba5de2b8f0d39.tar.xz
Use scalable pipeline instead of one middle section
Diffstat (limited to 'backuper.sh')
-rw-r--r--backuper.sh90
1 files changed, 41 insertions, 49 deletions
diff --git a/backuper.sh b/backuper.sh
index b0990d5..5cda722 100644
--- a/backuper.sh
+++ b/backuper.sh
@@ -14,15 +14,13 @@ GENERATE=""
COMMAND=""
PLACES=""
EXCLUDE=""
-MIDDLE=""
STORE=""
## Pipeline variables
PID_GENERATE=""
-PID_MIDDLE=""
PID_STORE=""
-FIFO_GEN=""
-FIFO_STORE=""
+PID_FILE="pids"
+STAGE="0"
test_input() {
[ -z "$NAME" ] && error "Specify backup name"
@@ -61,10 +59,6 @@ exclude() {
EXCLUDE="$STR"
}
-middle() {
- MIDDLE="$1"
-}
-
store() {
STORE="$1"
}
@@ -86,8 +80,7 @@ compute_variables() {
TS="$(date +"%Y-%m-%d_%H-%M-%S")"
FILENAME="backup_"$NAME"_"$TS".tar"
FILENAME_ENC="$FILENAME".gpg
- FIFO_GEN="/tmp/backuper_pipe_gen_$$_$UTS"
- FIFO_STORE="/tmp/backuper_pipe_str_$$_$UTS"
+ RUNDIR="/tmp/backuper_run_dir_$$_$UTS"
}
title() {
@@ -101,15 +94,9 @@ get_size() {
}
prepare() {
-
- #mkdir -p "$DST"
- #touch "$DST"/"$FILENAME"
- ##chown root:root "$DST"/"$FILENAME"
- #chmod 600 "$DST"/"$FILENAME"
echo "prepare $$"
trap 'error_handler' EXIT INT QUIT TERM ABRT HUP ILL TRAP BUS FPE SEGV
- mkfifo "$FIFO_GEN"
- mkfifo "$FIFO_STORE"
+ mkdir -p "$RUNDIR"
}
error_handler() {
@@ -122,38 +109,52 @@ cleanup() {
trap - EXIT INT QUIT TERM HUP ILL TRAP BUS FPE SEGV
+ for PID in $(cat "$RUNDIR"/"$PID_FILE"); do
+ if ps --ppid $$ | grep -q "$PID"; then
+ echo "Killing "$PID""
+ kill "$PID"
+ fi
+ done
+
[ -n "$PID_GENERATE" ] && if ps --ppid $$ | grep -q "$PID_GENERATE"; then
echo "Killing "$PID_GENERATE""
kill "$PID_GENERATE"
fi
- [ -n "$PID_MIDDLE" ] && if ps --ppid $$ | grep -q "$PID_MIDDLE"; then
- echo "Killing "$PID_MIDDLE""
- kill "$PID_MIDDLE"
- fi
+
[ -n "$PID_STORE" ] && if ps --ppid $$ | grep -q "$PID_STORE"; then
echo "Killing "$PID_STORE""
kill "$PID_STORE"
fi
- rm -f "$FIFO_GEN"
- rm -f "$FIFO_STORE"
-
+ if [ -d "$RUNDIR" ]; then
+ rm -rf "$RUNDIR"
+ fi
}
-start_store() {
- $STORE < "$FIFO_STORE"
+add_stage() {
+ ## Prepare pipes
+ NEXTSTAGE="$((STAGE+1))"
+ [ ! -p "$RUNDIR"/fifo_"$STAGE" ] && mkfifo "$RUNDIR"/fifo_"$STAGE"
+ [ ! -p "$RUNDIR"/fifo_"$NEXTSTAGE" ] && mkfifo "$RUNDIR"/fifo_"$NEXTSTAGE"
+
+ ## Run requested command and store PID
+ $@ > "$RUNDIR"/fifo_"$NEXTSTAGE" < "$RUNDIR"/fifo_"$STAGE" || die &
+ echo "$!" >> "$RUNDIR"/"$PID_FILE"
+
+ ## Process started - increment
+ STAGE="$NEXTSTAGE"
}
-start_middle() {
- $MIDDLE < "$FIFO_GEN" > "$FIFO_STORE"
+start_store() {
+ $STORE < "$RUNDIR"/fifo_"$NEXTSTAGE"
}
start_command() {
if [ -n "$COMMAND" ]; then
- echo "$COMMAND" | $GENERATE sh -s > "$FIFO_GEN"
+ echo "$COMMAND" | $GENERATE sh -s > "$RUNDIR"/fifo_0
else
## Exclude keep without quotes
- /usr/bin/time -f "$TIME_FMT" $GENERATE tar c --preserve-permissions $EXCLUDE "$PLACES" > "$FIFO_GEN"
+ /usr/bin/time -f "$TIME_FMT" $GENERATE tar c --preserve-permissions $EXCLUDE "$PLACES" > "$RUNDIR"/fifo_0
fi
@@ -165,30 +166,21 @@ start_command() {
}
start_pipeline() {
+ add_stage cat
+ add_stage cat
+ add_stage cat
+ add_stage cat
+
start_store || die &
PID_STORE=$!
- start_middle || die &
- PID_MIDDLE=$!
-
start_command || die &
- PID_COMMAND=$!
+ PID_GENERATE=$!
-# if ! wait "$PID_COMMAND"; then
-# die "Backup command failed"
-# fi
-# echo "Command done" >&2
-#
-# if ! wait "$PID_STORE"; then
-# die "Store command failed"
-# fi
-# echo "Store done" >&2
-#
-# if ! wait "$PID_MIDDLE"; then
-# die "Middle command failed"
-# fi
-# echo "Middle done" >&2
-wait "$PID_STORE" "$PID_MIDDLE" "$PID_COMMAND"
+ wait "$PID_STORE" "$PID_GENERATE"
+ for PID in $(cat "$RUNDIR"/"$PID_FILE"); do
+ wait "$PID"
+ done
}
#backup_cmd() {