aboutsummaryrefslogtreecommitdiffstats
path: root/backuper.sh
diff options
context:
space:
mode:
authorGravatar Robin Obůrka <r.oburka@gmail.com> 2016-07-21 21:41:05 +0200
committerGravatar Robin Obůrka <r.oburka@gmail.com> 2016-07-24 16:34:02 +0200
commit6489788d49b84863581040c56c8f93ea88265ae4 (patch)
tree999d1db275d41703bcf960fa0de2e40e266b3376 /backuper.sh
downloadbackuper-6489788d49b84863581040c56c8f93ea88265ae4.tar.xz
Initial commit
Diffstat (limited to 'backuper.sh')
-rw-r--r--backuper.sh241
1 files changed, 241 insertions, 0 deletions
diff --git a/backuper.sh b/backuper.sh
new file mode 100644
index 0000000..b0990d5
--- /dev/null
+++ b/backuper.sh
@@ -0,0 +1,241 @@
+## Some default values of important variables
+
+## Settings
+GPG_RECIPIENT="robin.oburka@nic.cz"
+
+## Style
+TIME_FMT="Time:\t%E"
+
+## Internal variables
+NAME=""
+DST_COMMAND=""
+#GENERATE="sudo"
+GENERATE=""
+COMMAND=""
+PLACES=""
+EXCLUDE=""
+MIDDLE=""
+STORE=""
+
+## Pipeline variables
+PID_GENERATE=""
+PID_MIDDLE=""
+PID_STORE=""
+FIFO_GEN=""
+FIFO_STORE=""
+
+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"
+}
+
+name() {
+ NAME="$1"
+}
+
+generate() {
+ GENERATE="$1"
+}
+
+cmd() {
+ COMMAND="$1"
+}
+
+places() {
+ PLACES="$1"
+ [ "$#" -gt 1 ] && error "Specify places as string"
+
+ #STR=""
+ #for PLACE in "$@"; do
+ # STR="$STR "$PLACE""
+ #done
+ #PLACES="$STR"
+}
+
+exclude() {
+ STR=""
+ for EXC in "$@"; do
+ STR="$STR --exclude=$EXC"
+ done
+ EXCLUDE="$STR"
+}
+
+middle() {
+ MIDDLE="$1"
+}
+
+store() {
+ STORE="$1"
+}
+
+error() {
+ echo "ERROR: $1" >&2
+ exit 1
+}
+
+die() {
+ if [ -n "$1" ]; then
+ echo "DIE: $1" >&2
+ fi
+ kill -6 "$$"
+}
+
+compute_variables() {
+ UTS="$(date +"%s")"
+ 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"
+}
+
+title() {
+ TXT="$1"
+ printf "\n%s\n==========================================\n" "$TXT"
+}
+
+get_size() {
+ FILE="$1"
+ du -h "$FILE" | awk '{ print $1 }'
+}
+
+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"
+}
+
+error_handler() {
+ cleanup
+ exit 42
+}
+
+cleanup() {
+ echo "Cleaning up... ($$)" >&2
+
+ trap - EXIT INT QUIT TERM HUP ILL TRAP BUS FPE SEGV
+
+ [ -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"
+
+}
+
+start_store() {
+ $STORE < "$FIFO_STORE"
+}
+
+start_middle() {
+ $MIDDLE < "$FIFO_GEN" > "$FIFO_STORE"
+}
+
+start_command() {
+ if [ -n "$COMMAND" ]; then
+ echo "$COMMAND" | $GENERATE sh -s > "$FIFO_GEN"
+ else
+ ## Exclude keep without quotes
+ /usr/bin/time -f "$TIME_FMT" $GENERATE tar c --preserve-permissions $EXCLUDE "$PLACES" > "$FIFO_GEN"
+ fi
+
+
+# if [ -z "$GENERATE" ]; then
+# $COMMAND > "FIFO_GEN"
+# else
+# echo $COMMAND | $GENERATE sh -s
+# fi
+}
+
+start_pipeline() {
+ start_store || die &
+ PID_STORE=$!
+
+ start_middle || die &
+ PID_MIDDLE=$!
+
+ start_command || die &
+ PID_COMMAND=$!
+
+# 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"
+}
+
+#backup_cmd() {
+# /usr/bin/time -f "$TIME_FMT" $COMMAND
+# [ "$?" != 0 ] && die "Generator command exited with error"
+#}
+#
+#create_backup() {
+# title "Create"
+#
+# if [ -n "$PLACES" ]; then
+# backup_tar | cat > "$DST"/"$FILENAME"
+# elif [ -n "$COMMAND" ]; then
+# echo "Generating with command: $COMMAND"
+# backup_cmd | cat > "$DST"/"$FILENAME"
+# else
+# error "There is no generating command"
+# fi
+#
+# printf "Backup:\t%s (%s)\n\n" "$FILENAME" "$(get_size "$DST"/"$FILENAME")"
+#}
+#
+#enc() {
+# title "Encrypt"
+# /usr/bin/time -f "$TIME_FMT" gpg --compress-algo zlib --recipient "$GPG_RECIPIENT" -e "$DST"/"$FILENAME"
+# [ "$?" != 0 ] && die "Encription failed"
+# rm "$DST"/"$FILENAME"
+# printf "Encrypted backup:\t%s (%s)\n\n" "$FILENAME_ENC" "$(get_size "$DST"/"$FILENAME_ENC")"
+#}
+#
+#run() {
+# title "Run command"
+# echo "$@"
+# CMD="sh -c \"$@\""
+# echo $@ | $GENERATE sh -s
+# printf "\n"
+#}
+
+backup() {
+ test_input
+ compute_variables
+ prepare
+ start_pipeline
+ #kill $$
+ cleanup
+ #create_backup
+ #secure
+ #enc
+ #send
+}