-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbackup.sh
More file actions
196 lines (170 loc) · 4.08 KB
/
backup.sh
File metadata and controls
196 lines (170 loc) · 4.08 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
#!/usr/bin/env bash
# backup.sh — capture OCS/OpenCode state before clearing
set -euo pipefail
if [[ -z "${HOME:-}" ]]; then
HOME="$(getent passwd "$(id -u)" | cut -d: -f6 2>/dev/null || true)"
if [[ -z "${HOME:-}" ]]; then
HOME="$(cd ~ 2>/dev/null && pwd || true)"
fi
if [[ -z "${HOME:-}" ]]; then
HOME="/tmp"
fi
export HOME
fi
resolve_target_home() {
if [[ -n "${OCS_TARGET_HOME:-}" ]]; then
printf '%s\n' "${OCS_TARGET_HOME}"
return 0
fi
if [[ "${EUID:-$(id -u)}" -eq 0 && -n "${SUDO_USER:-}" && "${SUDO_USER}" != "root" ]]; then
local sudo_home=""
sudo_home="$(getent passwd "${SUDO_USER}" | cut -d: -f6 2>/dev/null || true)"
if [[ -z "${sudo_home}" ]]; then
sudo_home="$(eval printf '%s' "~${SUDO_USER}" 2>/dev/null || true)"
fi
if [[ -n "${sudo_home}" ]]; then
printf '%s\n' "${sudo_home}"
return 0
fi
fi
printf '%s\n' "${HOME:-/tmp}"
}
TARGET_HOME="$(resolve_target_home)"
if [[ -n "${TARGET_HOME}" && "${TARGET_HOME}" != "${HOME}" ]]; then
HOME="${TARGET_HOME}"
export HOME
fi
YES_MODE=0
DRY_RUN=0
BACKUP_DIR="${HOME}/.opencode-suites-uninstall-backups"
OUTPUT_PATH=""
info() { echo " $*"; }
success() { echo "✅ $*"; }
warn() { echo "⚠️ $*" >&2; }
error() { echo "❌ $*" >&2; exit 1; }
show_usage() {
cat <<'EOF'
Usage: backup.sh [options]
Options:
--yes, -y Skip confirmation prompts
--backup-dir <path> Directory to store archives (default: ~/.opencode-suites-uninstall-backups)
--output <path> Exact archive path (overrides --backup-dir)
--dry-run Print planned actions without touching disk
--help, -h Show this help
Env alternatives:
OCS_TARGET_HOME Target HOME when running under sudo
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--yes|-y)
YES_MODE=1
shift
;;
--backup-dir)
[[ $# -ge 2 ]] || error "Missing value for --backup-dir"
BACKUP_DIR="$2"
shift 2
;;
--output)
[[ $# -ge 2 ]] || error "Missing value for --output"
OUTPUT_PATH="$2"
shift 2
;;
--dry-run)
DRY_RUN=1
shift
;;
--help|-h)
show_usage
exit 0
;;
*)
error "Unknown option: $1 (use --help for usage)"
;;
esac
done
run_cmd() {
if (( DRY_RUN == 1 )); then
info "[dry-run] $*"
return 0
fi
"$@"
}
collect_sources() {
local candidate
for candidate in \
"${HOME}/.config/opencode" \
"${HOME}/.opencode" \
"${HOME}/.opencode-suites" \
"${HOME}/.cache/opencode" \
"${HOME}/.local/share/opencode" \
; do
if [[ -e "${candidate}" ]]; then
printf '%s\n' "${candidate}"
fi
done
}
confirm_proceed() {
if (( YES_MODE == 1 )); then
return 0
fi
echo "About to archive the following OCS/OpenCode directories:"
echo " HOME=${HOME}"
echo
echo "Sources:"
for src in "$@"; do
printf ' - %s\n' "${src/#${HOME}/~}"
done
echo
cat <<'EOF'
Continue? [Y/n]
EOF
local answer=""
read -r answer || true
case "${answer}" in
y|Y|yes|YES|"")
return 0
;;
*)
info "Cancelled."
exit 0
;;
esac
}
prepare_archive_path() {
local dest_dir ts
if [[ -n "${OUTPUT_PATH}" ]]; then
dest_dir="$(dirname "${OUTPUT_PATH}")"
run_cmd mkdir -p "${dest_dir}"
printf '%s\n' "${OUTPUT_PATH}"
return 0
fi
ts="$(date +%Y%m%d-%H%M%S)"
run_cmd mkdir -p "${BACKUP_DIR}"
printf '%s\n' "${BACKUP_DIR}/ocs-backup-${ts}.tar.gz"
}
main() {
local sources=()
local src=""
while IFS= read -r src; do
[[ -n "${src}" ]] && sources+=("${src}")
done < <(collect_sources)
if (( ${#sources[@]} == 0 )); then
info "No OCS/OpenCode config/cache directories found to archive."
return 0
fi
confirm_proceed "${sources[@]}"
local archive
archive="$(prepare_archive_path)"
info "Archive destination: ${archive}"
local rel_sources=()
for src in "${sources[@]}"; do
rel_sources+=("${src#/}")
done
run_cmd tar -C / -czf "${archive}" "${rel_sources[@]}"
if (( DRY_RUN == 0 )); then
success "Backup created: ${archive}"
fi
}
main