Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·244 lines (195 loc) · 7.84 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·244 lines (195 loc) · 7.84 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
241
242
243
244
#!/bin/bash
# wp-backup — Site Installer
# Must be run as root.
# Copies wp-backup.sh into a chosen SpinupWP site user's ~/.local/bin
# directory (as `wp-backup`), matching the site user's ownership/permissions
# so that user can simply run: wp-backup
# Ubuntu 22.04+ compatible script
if ((BASH_VERSINFO[0] < 5)); then
echo "ERROR: This script requires Bash 5.0 or higher (current: $BASH_VERSION)"
exit 1
fi
####################################################################################
# COLORS
####################################################################################
if [[ -t 1 ]]; then
COLOR_RESET='\033[0m'
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_YELLOW='\033[0;33m'
COLOR_CYAN='\033[0;36m'
COLOR_WHITE='\033[1;37m'
COLOR_BOLD_GREEN='\033[1;32m'
COLOR_BOLD_YELLOW='\033[1;33m'
COLOR_BOLD_BLUE='\033[1;34m'
COLOR_BOLD_CYAN='\033[1;36m'
else
COLOR_RESET=''
COLOR_RED=''
COLOR_GREEN=''
COLOR_YELLOW=''
COLOR_CYAN=''
COLOR_WHITE=''
COLOR_BOLD_GREEN=''
COLOR_BOLD_YELLOW=''
COLOR_BOLD_BLUE=''
COLOR_BOLD_CYAN=''
fi
####################################################################################
# HELPERS
####################################################################################
print_header() {
echo -e "\n${COLOR_BOLD_CYAN}==== $1 ====${COLOR_RESET}"
}
print_info() {
echo -e "${COLOR_CYAN}[INFO]${COLOR_RESET} $1"
}
print_success() {
echo -e "${COLOR_BOLD_GREEN}[SUCCESS]${COLOR_RESET} $1"
}
print_warning() {
echo -e "${COLOR_BOLD_YELLOW}[WARNING]${COLOR_RESET} $1"
}
print_error() {
echo -e "${COLOR_RED}[ERROR]${COLOR_RESET} $1"
}
print_step() {
echo -e "\n${COLOR_BOLD_BLUE}---> $1${COLOR_RESET}"
}
####################################################################################
# ROOT CHECK
####################################################################################
if [[ $EUID -ne 0 ]]; then
print_error "This installer must be run as root."
echo " Try: sudo $0"
exit 1
fi
####################################################################################
# LOCATE SCRIPT
####################################################################################
# wp-backup.sh is expected to live alongside install.sh (both deployed to
# /opt/wp-backup/ by wp-backup-install.yml)
SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
SOURCE_SCRIPT="${SCRIPT_DIR}/wp-backup.sh"
SCRIPT_NAME="wp-backup"
if [[ ! -f "$SOURCE_SCRIPT" ]]; then
print_error "wp-backup.sh not found in ${SCRIPT_DIR}"
exit 1
fi
# Restrict all .sh files in the source directory to root-only (0700)
find "$SCRIPT_DIR" -maxdepth 1 -name "*.sh" -exec chmod 700 {} \;
####################################################################################
# MAIN
####################################################################################
clear
print_header "wp-backup — Site Installer"
echo -e "${COLOR_WHITE}This installer copies wp-backup into a site user's local bin directory.${COLOR_RESET}"
echo ""
# ── Step 1: List and select site ─────────────────────────────────────────────
print_step "Step 1: Select the site to install for"
echo ""
if [[ ! -d /sites ]]; then
print_error "/sites directory does not exist."
print_info "This installer expects a /sites directory structure (e.g. /sites/<domain>/files/)."
exit 1
fi
print_info "Searching for WordPress installations in /sites/*/files/ ..."
sites=()
while IFS= read -r -d '' files_dir; do
site_dir="$(dirname "$files_dir")"
[[ -d "$site_dir" ]] && sites+=("$site_dir")
done < <(find /sites -maxdepth 2 -type d -name "files" -print0 2>/dev/null)
if [[ ${#sites[@]} -eq 0 ]]; then
print_error "No sites found matching /sites/*/files/"
exit 1
fi
echo ""
for i in "${!sites[@]}"; do
printf " ${COLOR_YELLOW}%2d${COLOR_RESET} %s\n" "$((i+1))" "${sites[$i]}"
done
echo ""
while true; do
read -r -p "$(echo -e "${COLOR_CYAN}Enter site number [1-${#sites[@]}]:${COLOR_RESET} ")" site_choice
if [[ "$site_choice" =~ ^[0-9]+$ ]] && (( site_choice >= 1 && site_choice <= ${#sites[@]} )); then
break
fi
print_warning "Please enter a number between 1 and ${#sites[@]}."
done
selected_site="${sites[$((site_choice-1))]}"
print_success "Selected site: ${selected_site}"
# ── Step 2: Determine owner and bin directory ────────────────────────────────
print_step "Step 2: Determining site user and install path"
# Determine the owner of the site directory
if stat -c '%U' "$selected_site" >/dev/null 2>&1; then
site_owner="$(stat -c '%U' "$selected_site")"
elif stat -f '%Su' "$selected_site" >/dev/null 2>&1; then
site_owner="$(stat -f '%Su' "$selected_site")"
else
site_owner=""
fi
if [[ -z "$site_owner" || "$site_owner" == "root" ]]; then
print_warning "Could not reliably detect a non-root site owner for ${selected_site}."
read -r -p "$(echo -e "${COLOR_CYAN}Enter the username to install for:${COLOR_RESET} ")" site_owner
if [[ -z "$site_owner" ]]; then
print_error "No username provided."
exit 1
fi
fi
# Verify the user exists
if ! id "$site_owner" >/dev/null 2>&1; then
print_error "User '${site_owner}' does not exist on this system."
exit 1
fi
site_owner_home="$(getent passwd "$site_owner" | cut -d: -f6)"
bin_dir="${selected_site}/.local/bin"
install_path="${bin_dir}/${SCRIPT_NAME}"
print_info "Site user : ${site_owner}"
if [[ -n "$site_owner_home" ]]; then
print_info "Home dir : ${site_owner_home}"
fi
print_info "Install to : ${install_path}"
# ── Step 3: Confirm ───────────────────────────────────────────────────────────
echo ""
echo -e "${COLOR_WHITE}About to install ${COLOR_BOLD_CYAN}${SCRIPT_NAME}${COLOR_RESET}${COLOR_WHITE} for user ${COLOR_BOLD_CYAN}${site_owner}${COLOR_RESET}${COLOR_WHITE}.${COLOR_RESET}"
read -r -p "$(echo -e "${COLOR_CYAN}Proceed? [y/N]:${COLOR_RESET} ")" confirm
case "$confirm" in
[yY]|[yY][eE][sS]) ;;
*)
echo "Aborted."
exit 0
;;
esac
# ── Step 4: Create directory and copy script ─────────────────────────────────
print_step "Step 4: Copying script"
if [[ ! -d "$bin_dir" ]]; then
print_info "Creating directory: ${bin_dir}"
if ! mkdir -p "$bin_dir"; then
print_error "Failed to create ${bin_dir}"
exit 1
fi
fi
if ! cp "$SOURCE_SCRIPT" "$install_path"; then
print_error "Failed to copy script to ${install_path}"
exit 1
fi
print_success "Script copied to: ${install_path}"
# ── Step 5: Set ownership and permissions ────────────────────────────────────
print_step "Step 5: Setting ownership and permissions"
if chown -R "${site_owner}:${site_owner}" "$bin_dir"; then
print_success "Ownership set to ${site_owner}:${site_owner} on ${bin_dir}"
else
print_warning "Could not set ownership on ${bin_dir} — you may need to fix this manually."
fi
if chmod 0700 "$install_path"; then
print_success "Permissions set to 0700 on ${install_path}"
else
print_error "Failed to set permissions on ${install_path}"
exit 1
fi
# ── Done ──────────────────────────────────────────────────────────────────────
echo ""
print_header "Installation Complete"
print_success "${SCRIPT_NAME} installed for user ${site_owner}"
print_info "Location : ${install_path}"
print_info "Run as : su - ${site_owner} -c '${SCRIPT_NAME}'"
echo ""