Skip to content

Commit 9fa99b3

Browse files
authored
Update backup.sh
1 parent 18ca050 commit 9fa99b3

1 file changed

Lines changed: 63 additions & 9 deletions

File tree

‎pg-node/backup.sh‎

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#!/usr/bin/env bash
22
#
33
# backup.sh - stops the pg-node stack, archives /opt/pg-node and
4-
# /var/lib/pg-node into backup.tar.gz, then restarts the stack.
4+
# /var/lib/pg-node into a password-protected backup.zip, then restarts
5+
# the stack.
6+
#
7+
# A random 64-character password (upper/lower case letters + digits,
8+
# no symbols) is generated for every run and printed at the end, and
9+
# also saved next to the archive (backup.zip.password) with 600
10+
# permissions. Keep this password safe - it is required by restore.sh
11+
# to extract the archive.
512
#
613
# Usage: sudo ./backup.sh
714
#
@@ -10,8 +17,9 @@ set -Eeuo pipefail
1017
readonly APP_DIR="/opt/pg-node"
1118
readonly DATA_DIR="/var/lib/pg-node"
1219
readonly COMPOSE_FILE="${APP_DIR}/docker-compose.yml"
13-
readonly BACKUP_FILE="backup.tar.gz"
20+
readonly BACKUP_FILE="backup.zip"
1421
readonly BACKUP_PATH="$(pwd)/${BACKUP_FILE}"
22+
readonly PASSWORD_FILE="${BACKUP_PATH}.password"
1523
readonly LOG_FILE="/var/log/pg-node-backup.log"
1624

1725
readonly COLOR_RED="\033[0;31m"
@@ -99,6 +107,26 @@ check_docker() {
99107
log_ok "Docker and Compose plugin present."
100108
}
101109

110+
# Makes sure zip/unzip are available; installs whichever is missing.
111+
check_zip_tools() {
112+
local missing=()
113+
command -v zip &>/dev/null || missing+=("zip")
114+
command -v unzip &>/dev/null || missing+=("unzip")
115+
116+
if [[ "${#missing[@]}" -gt 0 ]]; then
117+
log_info "Installing missing tool(s): ${missing[*]}"
118+
export DEBIAN_FRONTEND=noninteractive
119+
run_quiet "Updating package lists" apt-get update -y
120+
run_quiet "Installing ${missing[*]}" apt-get install -y "${missing[@]}"
121+
fi
122+
123+
if ! command -v zip &>/dev/null; then
124+
log_err "Failed to install 'zip'."
125+
exit 1
126+
fi
127+
log_ok "zip is available."
128+
}
129+
102130
check_paths() {
103131
if [[ ! -f "${COMPOSE_FILE}" ]]; then
104132
log_err "Compose file not found at ${COMPOSE_FILE}."
@@ -119,21 +147,44 @@ stop_stack() {
119147
STACK_STOPPED=1
120148
}
121149

150+
# Generates a random 64-character password made only of upper/lower case
151+
# letters and digits (no symbols), as requested.
152+
BACKUP_PASSWORD=""
153+
generate_password() {
154+
BACKUP_PASSWORD="$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 64)"
155+
if [[ "${#BACKUP_PASSWORD}" -ne 64 ]]; then
156+
log_err "Failed to generate a 64-character password."
157+
exit 1
158+
fi
159+
}
160+
161+
# Runs zip with cwd=/ so archive paths are relative to /, allowing
162+
# restore.sh to extract straight back to /.
163+
_zip_targets() {
164+
cd /
165+
zip -r -y -P "${BACKUP_PASSWORD}" "${BACKUP_PATH}" "$@"
166+
}
167+
122168
create_backup() {
123169
local targets=()
124-
[[ -d "${APP_DIR}" ]] && targets+=("${APP_DIR}")
125-
[[ -d "${DATA_DIR}" ]] && targets+=("${DATA_DIR}")
170+
[[ -d "${APP_DIR}" ]] && targets+=("${APP_DIR#/}")
171+
[[ -d "${DATA_DIR}" ]] && targets+=("${DATA_DIR#/}")
126172

127173
if [[ "${#targets[@]}" -eq 0 ]]; then
128174
log_err "Neither ${APP_DIR} nor ${DATA_DIR} exist. Nothing to back up."
129175
exit 1
130176
fi
131177

132-
# Paths relative to / so restore.sh can extract straight to /.
133-
run_quiet "Creating backup archive" tar -czpf "${BACKUP_PATH}" -C / \
134-
$(printf '%s\n' "${targets[@]}" | sed 's|^/||')
178+
generate_password
179+
180+
rm -f "${BACKUP_PATH}"
181+
run_quiet "Creating encrypted backup archive" _zip_targets "${targets[@]}"
182+
183+
printf '%s\n' "${BACKUP_PASSWORD}" > "${PASSWORD_FILE}"
184+
chmod 600 "${PASSWORD_FILE}"
135185

136186
log_ok "Archive created: ${BACKUP_PATH}"
187+
log_ok "Password saved to: ${PASSWORD_FILE} (permissions 600)"
137188
}
138189

139190
start_stack() {
@@ -146,14 +197,17 @@ print_summary() {
146197
size=$(du -h "${BACKUP_PATH}" | cut -f1)
147198
echo
148199
log_ok "Backup completed successfully."
149-
log_info "File: ${BACKUP_PATH}"
150-
log_info "Size: ${size}"
200+
log_info "File: ${BACKUP_PATH}"
201+
log_info "Size: ${size}"
202+
log_info "Password: ${BACKUP_PASSWORD}"
203+
log_warn "Store this password somewhere safe - it is NOT recoverable and is required for restore.sh."
151204
}
152205

153206
main() {
154207
check_root
155208
init_log
156209
check_docker
210+
check_zip_tools
157211
check_paths
158212
stop_stack
159213
create_backup

0 commit comments

Comments
 (0)