-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgbackup
More file actions
129 lines (103 loc) · 2.85 KB
/
Copy pathpgbackup
File metadata and controls
129 lines (103 loc) · 2.85 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
#!/usr/bin/env bash
set -euo pipefail
# ============================================================
# pgbackup — safe PostgreSQL backup & optional export
# ============================================================
EXPORT_PATH=""
PGBR_CONF="/etc/pgbackrest/pgbackrest.conf"
# ---------------- HELP ----------------
show_help() {
cat <<EOF
pgbackup — manual PostgreSQL backup helper
USAGE:
pgbackup
Take a fresh pgBackRest backup for all ACTIVE clusters
pgbackup -path <directory>
Take a fresh backup, then export pgBackRest repo to directory
EOF
exit 0
}
# ---------------- FLAGS ----------------
while [[ $# -gt 0 ]]; do
case "$1" in
-path)
EXPORT_PATH="${2:-}"
shift 2
;;
-h|--help)
show_help
;;
*)
echo "❌ Unknown option: $1"
show_help
;;
esac
done
# ---------------- HELPERS ----------------
fail() {
echo "❌ $1"
exit 1
}
require_pgbackrest() {
command -v pgbackrest >/dev/null || fail "pgBackRest not installed"
[ -f "$PGBR_CONF" ] || fail "pgBackRest config not found at $PGBR_CONF"
}
get_repo_path() {
grep '^repo1-path=' "$PGBR_CONF" \
| head -n1 \
| cut -d'=' -f2
}
get_active_clusters() {
pg_lsclusters | awk 'NR>1 && $4=="online" {print $2}'
}
validate_export_path() {
local path="$1"
[ "$path" != "/" ] || fail "Refusing to use / as export path"
if [ ! -d "$path" ]; then
echo "📁 Creating export directory: $path"
mkdir -p "$path" || fail "Failed to create export directory"
fi
[ -w "$path" ] || fail "Export path not writable: $path"
}
# ---------------- MAIN ----------------
require_pgbackrest
REPO_PATH="$(get_repo_path)"
[ -n "$REPO_PATH" ] || fail "repo1-path not defined in pgbackrest.conf"
[ -d "$REPO_PATH" ] || fail "pgBackRest repo directory not found: $REPO_PATH"
CLUSTERS=($(get_active_clusters))
[ "${#CLUSTERS[@]}" -eq 0 ] && fail "No active PostgreSQL clusters found"
echo
echo "💾 Starting PostgreSQL backup"
echo "-----------------------------"
echo "pgBackRest repo : $REPO_PATH"
echo
for cluster in "${CLUSTERS[@]}"; do
echo "→ Backing up cluster: $cluster"
sudo -u postgres pgbackrest --stanza="$cluster" backup
done
echo
echo "✅ Active clusters backed up successfully"
# ---------------- EXPORT ----------------
if [ -n "$EXPORT_PATH" ]; then
validate_export_path "$EXPORT_PATH"
DEST="$EXPORT_PATH/pgbackrest"
if [ "$(realpath "$REPO_PATH")" = "$(realpath "$DEST")" ]; then
echo
echo "ℹ️ Export skipped (repo already stored at destination)"
echo
else
echo
echo "📦 Exporting pgBackRest repository"
echo "---------------------------------"
echo "Source : $REPO_PATH"
echo "Destination : $DEST"
echo
sudo rsync -a --delete \
"$REPO_PATH/" \
"$DEST/"
echo "✅ Backup exported successfully"
fi
fi
echo
echo "🎉 pgbackup completed safely"
echo