-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-docker-volume.sh
More file actions
executable file
·240 lines (192 loc) · 5 KB
/
backup-docker-volume.sh
File metadata and controls
executable file
·240 lines (192 loc) · 5 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bash
set -Eeuo pipefail
# Global constants
ENV_FILE="$(dirname "$0")/.env"
# ANSI color codes
readonly COLOR_RESET="\033[0m"
readonly COLOR_INFO="\033[0;36m"
readonly COLOR_SUCCESS="\033[0;32m"
readonly COLOR_WARN="\033[0;33m"
readonly COLOR_ERROR="\033[0;31m"
# Logging functions
log() {
local level="$1"
local color="$2"
local message="$3"
local timestamp
timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
echo -e "${color}[${timestamp}] [$level] ${message}${COLOR_RESET}" >&2
}
info() { log INFO "$COLOR_INFO" "$1"; }
success() { log SUCCESS "$COLOR_SUCCESS" "$1"; }
wrong() { log WRONG "$COLOR_ERROR" "$1"; }
error() { log ERROR "$COLOR_ERROR" "$1"; }
# Interactive functions
input() { read -rp "$(echo -e "${COLOR_WARN}${1}${COLOR_RESET}")" "$2"; }
# Error handler
on_error() {
local code=$?
error "Script failed with exit code ${code}"
exit "$code"
}
trap on_error ERR
# Environments & validation
create_env() {
local token chat_id
while true; do
# Get bot token
while true; do
input "Enter the bot token: " token
if [[ -z "$token" ]]; then
wrong "Bot token cannot be empty!"
elif [[ ! "$token" =~ ^[0-9]+:[a-zA-Z0-9_-]{35}$ ]]; then
wrong "Invalid bot token format!"
else
break
fi
done
# Get chat ID
while true; do
input "Enter the chat ID: " chat_id
if [[ -z "$chat_id" ]]; then
wrong "Chat ID cannot be empty!"
elif [[ ! "$chat_id" =~ ^-?[0-9]+$ ]]; then
wrong "Invalid chat ID format!"
else
break
fi
done
# Validate bot token and chat ID
info "Checking Telegram bot..."
response=$(curl -sS -o /dev/null -w "%{http_code}" -X POST \
"https://api.telegram.org/bot${token}/sendMessage" \
-d chat_id="${chat_id}" \
-d text="Hi, it's a test message for the backup script!"
)
if [[ "$response" -ne 200 ]]; then
wrong "Invalid bot token, chat ID, or Telegram API error!"
else
success "Bot token and chat ID are valid."
break
fi
done
# Creating .env file
cat > "$ENV_FILE" <<EOF
TELEGRAM_BOT_TOKEN=${token}
TELEGRAM_CHAT_ID=${chat_id}
EOF
chmod 600 "$ENV_FILE"
success ".env file successfully created!"
}
load_env() {
if [[ ! -f "$ENV_FILE" ]]; then
info ".env file not found. Creating a new one..."
create_env
fi
export $(grep -v '^#' "$ENV_FILE" | xargs)
validate_env
}
validate_env() {
local vars=(
TELEGRAM_BOT_TOKEN
TELEGRAM_CHAT_ID
)
for v in "${vars[@]}"; do
if [[ -z "${!v:-}" ]]; then
error "Environment variable $v is not set"
exit 1
fi
done
}
validate_args() {
if [[ $# -ne 1 ]]; then
error "Usage: $0 <docker_volume>"
exit 1
fi
VOLUME_NAME="$1"
if ! docker volume inspect "$VOLUME_NAME" > /dev/null 2>&1; then
error "Docker volume does not exist: $VOLUME_NAME"
exit 1
fi
}
# Creating & cleanup backup
create_archive() {
local timestamp archive_name archive_path
timestamp="$(date '+%Y-%m-%d_%H-%M-%S')"
archive_name="${VOLUME_NAME// /_}_${timestamp}.tar.gz"
archive_path="/tmp/${archive_name}"
info "Creating archive from Docker volume ${VOLUME_NAME}..."
if ! docker run --rm \
--user $(id -u):$(id -g) \
-v "${VOLUME_NAME}:/data:ro" \
-v "/tmp:/backup" \
alpine \
tar -czf "/backup/${archive_name}" -C /data .
then
error "Docker volume backup failed"
cleanup "$archive_path"
return 1
fi
success "Archive created: $archive_path"
echo "$archive_path"
}
cleanup() {
local path="$1"
rm -f "$path"
info "Temporary file removed"
}
# Sending notification
send_backup() {
local backup_path="$1"
local size caption
size="$(du -h "$backup_path" | cut -f1)"
info "Backup size: $size"
info "Sending archive to Telegram..."
caption="$(cat <<EOF
🟢 <b>Docker volume</b> backup completed <b>successfully</b>.
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
Volume: <b>${VOLUME_NAME}</b>
Backup size: <b>${size}</b>
EOF
)"
response="$(
curl -sS -o /dev/null -w "%{http_code}" \
--connect-timeout 10 \
--max-time 120 \
-X POST \
"https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" \
-F chat_id="$TELEGRAM_CHAT_ID" \
-F document=@"$backup_path" \
-F caption="$caption" \
-F parse_mode="HTML"
)"
curl_code=$?
if [[ "$curl_code" -eq 0 && "$response" == "200" ]]; then
success "Backup sent successfully!"
return 0
fi
# Error handling
if [[ "$curl_code" -eq 28 ]]; then
error "Telegram request timed out"
else
error "Failed to send backup to Telegram (HTTP ${response:-unknown})"
fi
cleanup "$backup_path"
return 1
}
main() {
info "Docker volume backup script started..."
load_env
validate_args "$@"
info "Volume to backup: $VOLUME_NAME"
local archive
if ! archive="$(create_archive)"; then
exit 1
fi
if ! send_backup "$archive"; then
exit 1
fi
cleanup "$archive"
success "Docker volume backup finished successfully!"
}
main "$@"