-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackup.sh
More file actions
172 lines (148 loc) · 4.07 KB
/
Backup.sh
File metadata and controls
172 lines (148 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
# ==============================================================================
# Enhanced Backup Tool Launcher
# ==============================================================================
set -euo pipefail
# -------- Configuration -----
DEFAULT_SOURCE="/home/use/documents"
DEFAULT_DESTINATION="/home/backups"
DEFAULT_INTERVAL=""
JAR_FILE="target/backup-tool-1.0-SNAPSHOT.jar"
# -------- Colors ------------
BLUE='\033[0;34m'
GREEN='\033[0;32m'
RED='\033[0;31m'
GRAY='\033[0;90m'
YELLOW='\033[0;33m'
NC='\033[0m'
# -------- Args --------------
SOURCE="${1:-$DEFAULT_SOURCE}"
DESTINATION="${2:-$DEFAULT_DESTINATION}"
INTERVAL="${3:-$DEFAULT_INTERVAL}"
# -------- Cleanup -----------
# Ensures cursor is restored even if the user exits mid-animation
cleanup() {
tput cnorm
echo -e "${NC}"
}
trap cleanup EXIT
# -------- Helpers -----------
die() {
echo -e "${RED}[ERROR] $1${NC}"
exit 1
}
draw_logo() {
echo -e "${BLUE}"
cat <<'EOF'
____ _
| _ \ | |
| |_) | __ _ ___| | ___ _ _ __
| _ < / _` |/ __| |/ / | | | '_ \
| |_) | (_| | (__| <| |_| | |_) |
|____/ \__,_|\___|_|\_\\__,_| .__/
| |
|_|
Backup Tool
EOF
echo -e "${NC}"
}
# -------- Animation Engine -----------
penguin_run() {
local pid=$1
# shellcheck disable=SC2155
local cols=$(tput cols)
local width=12
local pos=0
local dir=1
local height=7 # Number of lines the animation occupies
tput civis # Hide cursor
# Print placeholder lines so we can move up into them
# shellcheck disable=SC2034
for i in $(seq 1 $height); do echo ""; done
while kill -0 "$pid" 2>/dev/null; do
# Move cursor back up to the top of the animation block
printf "\033[%dA" $height
# Waddling logic: Frame 1 vs Frame 2
if (( pos % 2 == 0 )); then
printf "\r\033[K%*s ${GRAY}_~_${NC}\n" "$pos" ""
printf "\r\033[K%*s ${GRAY}(o o)${NC}\n" "$pos" ""
printf "\r\033[K%*s ${BLUE}/ V \\ ${NC}\n" "$pos" ""
printf "\r\033[K%*s${BLUE}/| |${NC}\n" "$pos" ""
printf "\r\033[K%*s ${GRAY} / / ${NC}\n" "$pos" ""
else
printf "\r\033[K%*s ${GRAY}_~_${NC}\n" "$pos" ""
printf "\r\033[K%*s ${GRAY}(o o)${NC}\n" "$pos" ""
printf "\r\033[K%*s ${BLUE}\\ V / ${NC}\n" "$pos" ""
printf "\r\033[K%*s ${BLUE}/| | ${NC}\n" "$pos" ""
printf "\r\033[K%*s ${GRAY}\\ \\ ${NC}\n" "$pos" ""
fi
# Status Line with Spinner
local spinChars="/-\|"
local frame=$((pos % 4))
printf "\r\033[K\n" # Spacer line
# shellcheck disable=SC2059
printf "\r\033[K ${YELLOW}${spinChars:$frame:1}${NC} ${BLUE}Backup in progress...${NC}\n"
sleep 0.1
# Movement physics
(( pos += dir ))
if (( pos >= (cols - width) )); then dir=-1; elif (( pos <= 0 )); then dir=1; fi
done
}
penguin_success() {
echo -e "${GREEN}"
cat <<'EOF'
_~_
(o o)
/ V \
| |
/_|_|_
SUCCESS: Backup completed.
EOF
echo -e "${NC}"
}
penguin_fail() {
echo -e "${RED}"
cat <<'EOF'
_
( )
/ | \
|
/ \
FAILED: Check logs for details.
EOF
echo -e "${NC}"
}
# -------- Main Execution -----------
clear
draw_logo
# Pre-flight Checks
[[ -f "$JAR_FILE" ]] || die "JAR not found: $JAR_FILE"
[[ -d "$SOURCE" ]] || die "Source directory does not exist: $SOURCE"
mkdir -p "$DESTINATION" || die "Failed to create destination"
[[ -z "$(ls -A "$SOURCE")" ]] && die "Source directory is empty"
# Config Summary
echo -e "${GRAY}Settings:${NC}"
echo " From: $SOURCE"
echo " To: $DESTINATION"
[[ -n "$INTERVAL" ]] && echo " Freq: Every $INTERVAL mins" || echo " Mode: Single Run"
echo -e "\n--- Starting Process ---"
# Start Background Process
set +e
if [[ -z "$INTERVAL" ]]; then
java -jar "$JAR_FILE" "$SOURCE" "$DESTINATION" > /dev/null 2>&1 &
else
java -jar "$JAR_FILE" "$SOURCE" "$DESTINATION" "$INTERVAL" > /dev/null 2>&1 &
fi
JAVA_PID=$!
penguin_run "$JAVA_PID"
wait "$JAVA_PID"
EXIT_CODE=$?
set -e
# Final Result
echo -e "\n"
if [[ $EXIT_CODE -eq 0 ]]; then
penguin_success
else
penguin_fail
exit "$EXIT_CODE"
fi