-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·255 lines (217 loc) · 8.61 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·255 lines (217 loc) · 8.61 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
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env bash
set -eo pipefail
# Installation script for db-backupper
# Installs db-backupper as a global command and sets up configuration
INSTALL_DIR="/usr/local/bin"
LIB_DIR="/usr/local/lib/db-backupper"
USER_BIN_DIR="$HOME/.local/bin"
USER_LIB_DIR="$HOME/.local/lib/db-backupper"
USER_CONFIG_DIR="$HOME/.config/db-backupper"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_OWNER_USER=""
CONFIG_OWNER_GROUP=""
CONFIG_OWNER_HOME=""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
check_root() {
[[ $EUID -eq 0 ]]
}
resolve_config_owner() {
if check_root && [[ -n "${SUDO_USER:-}" ]] && [[ "${SUDO_USER}" != "root" ]]; then
CONFIG_OWNER_USER="$SUDO_USER"
CONFIG_OWNER_HOME="$(getent passwd "$SUDO_USER" | cut -d: -f6)"
else
CONFIG_OWNER_USER="$(id -un)"
CONFIG_OWNER_HOME="$HOME"
fi
CONFIG_OWNER_GROUP="$(id -gn "$CONFIG_OWNER_USER")"
}
install_system_wide() {
log_info "Installing db-backupper system-wide..."
# Create directories
sudo mkdir -p "$LIB_DIR"
# Copy library files
sudo cp -r "${SCRIPT_DIR}/lib/"* "$LIB_DIR/"
# Copy main script and update paths for installed version
sudo cp "${SCRIPT_DIR}/db-backupper" "${INSTALL_DIR}/db-backupper"
sudo sed -i "s|^SCRIPT_DIR=.*|SCRIPT_DIR=\"$LIB_DIR\"|" "${INSTALL_DIR}/db-backupper"
sudo sed -i 's|${SCRIPT_DIR}/lib/|${SCRIPT_DIR}/|g' "${INSTALL_DIR}/db-backupper"
sudo sed -i '/^CONFIG_DIR=.*get_script_dir/d' "${INSTALL_DIR}/db-backupper"
sudo sed -i '/echo.*CONFIG_DIR/d' "${INSTALL_DIR}/db-backupper"
sudo sed -i 's|${CONFIG_DIR}/backup.conf|backup.conf (auto-discovered)|' "${INSTALL_DIR}/db-backupper"
sudo chmod +x "${INSTALL_DIR}/db-backupper"
log_success "db-backupper installed to ${INSTALL_DIR}/db-backupper"
log_success "Library files installed to $LIB_DIR/"
}
install_user_only() {
log_info "Installing db-backupper for current user only..."
# Create directories
mkdir -p "$USER_BIN_DIR" "$USER_LIB_DIR"
# Copy library files
cp -r "${SCRIPT_DIR}/lib/"* "$USER_LIB_DIR/"
# Copy main script and update paths for installed version
cp "${SCRIPT_DIR}/db-backupper" "$USER_BIN_DIR/db-backupper"
sed -i "s|^SCRIPT_DIR=.*|SCRIPT_DIR=\"$USER_LIB_DIR\"|" "$USER_BIN_DIR/db-backupper"
sed -i 's|${SCRIPT_DIR}/lib/|${SCRIPT_DIR}/|g' "$USER_BIN_DIR/db-backupper"
sed -i '/^CONFIG_DIR=.*get_script_dir/d' "$USER_BIN_DIR/db-backupper"
sed -i '/echo.*CONFIG_DIR/d' "$USER_BIN_DIR/db-backupper"
sed -i 's|${CONFIG_DIR}/backup.conf|backup.conf (auto-discovered)|' "$USER_BIN_DIR/db-backupper"
chmod +x "$USER_BIN_DIR/db-backupper"
log_success "db-backupper installed to $USER_BIN_DIR/db-backupper"
log_success "Library files installed to $USER_LIB_DIR/"
# Check if ~/.local/bin is in PATH
if [[ ":$PATH:" != *":$USER_BIN_DIR:"* ]]; then
log_warning "$USER_BIN_DIR is not in your PATH"
log_info "Add this to your ~/.bashrc or ~/.zshrc:"
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
fi
}
setup_config() {
local target_config_dir="$1"
local target_projects_dir="${target_config_dir}/projects"
log_info "Setting up configuration directory: $target_config_dir"
if check_root; then
sudo mkdir -p "$target_config_dir"
sudo mkdir -p "$target_projects_dir"
if [[ ! -f "$target_config_dir/backup.conf" ]]; then
sudo cp "${SCRIPT_DIR}/backup.conf.example" "$target_config_dir/backup.conf"
sudo chmod 600 "$target_config_dir/backup.conf"
log_success "Configuration template created at $target_config_dir/backup.conf"
log_warning "Please edit $target_config_dir/backup.conf with your settings"
else
log_info "Configuration file already exists at $target_config_dir/backup.conf"
fi
if [[ ! -f "$target_projects_dir/example.conf" ]]; then
sudo cp "${SCRIPT_DIR}/project.conf.example" "$target_projects_dir/example.conf"
sudo chmod 600 "$target_projects_dir/example.conf"
log_success "Project configuration template created at $target_projects_dir/example.conf"
else
log_info "Project configuration template already exists at $target_projects_dir/example.conf"
fi
sudo chown -R "${CONFIG_OWNER_USER}:${CONFIG_OWNER_GROUP}" "$target_config_dir"
else
mkdir -p "$target_config_dir"
mkdir -p "$target_projects_dir"
if [[ ! -f "$target_config_dir/backup.conf" ]]; then
cp "${SCRIPT_DIR}/backup.conf.example" "$target_config_dir/backup.conf"
chmod 600 "$target_config_dir/backup.conf"
log_success "Configuration template created at $target_config_dir/backup.conf"
log_warning "Please edit $target_config_dir/backup.conf with your settings"
else
log_info "Configuration file already exists at $target_config_dir/backup.conf"
fi
if [[ ! -f "$target_projects_dir/example.conf" ]]; then
cp "${SCRIPT_DIR}/project.conf.example" "$target_projects_dir/example.conf"
chmod 600 "$target_projects_dir/example.conf"
log_success "Project configuration template created at $target_projects_dir/example.conf"
else
log_info "Project configuration template already exists at $target_projects_dir/example.conf"
fi
fi
}
usage() {
echo "Usage: $0 [--system|--user] [--no-config]"
echo ""
echo "Options:"
echo " --system Install system-wide (requires sudo)"
echo " --user Install for current user only"
echo " --no-config Don't set up configuration files"
echo " --help Show this help"
echo ""
echo "If no installation type is specified, the script will:"
echo " - Try system-wide installation if running as root"
echo " - Fall back to user installation otherwise"
}
main() {
log_info "db-backupper installation script"
resolve_config_owner
# Check if files exist
if [[ ! -f "${SCRIPT_DIR}/db-backupper" ]]; then
log_error "db-backupper executable not found in ${SCRIPT_DIR}"
exit 1
fi
if [[ ! -d "${SCRIPT_DIR}/lib" ]]; then
log_error "lib directory not found in ${SCRIPT_DIR}"
exit 1
fi
# Parse command line arguments
local install_type="auto"
local setup_config_flag=true
while [[ $# -gt 0 ]]; do
case "$1" in
--system)
install_type="system"
shift
;;
--user)
install_type="user"
shift
;;
--no-config)
setup_config_flag=false
shift
;;
--help|-h)
usage
exit 0
;;
*)
log_error "Unknown option: $1"
usage
exit 1
;;
esac
done
# Determine installation method
if [[ "$install_type" == "auto" ]]; then
if check_root; then
install_type="system"
else
install_type="user"
fi
fi
# Perform installation
case "$install_type" in
system)
if ! check_root; then
log_error "System-wide installation requires root privileges"
log_info "Please run with sudo or use --user for user installation"
exit 1
fi
install_system_wide
if [[ "$setup_config_flag" == true ]]; then
log_info "Preparing user-scoped config for ${CONFIG_OWNER_USER} at ${CONFIG_OWNER_HOME}/.config/db-backupper"
setup_config "${CONFIG_OWNER_HOME}/.config/db-backupper"
fi
;;
user)
install_user_only
if [[ "$setup_config_flag" == true ]]; then
setup_config "$USER_CONFIG_DIR"
fi
;;
esac
log_success "Installation complete!"
log_info "Run 'db-backupper help' to see usage information"
if [[ "$setup_config_flag" == true ]]; then
log_info "Active configs live under ${CONFIG_OWNER_HOME}/.config/db-backupper"
log_info "Configure backup.conf for legacy mode or add files under projects/ for named project mode"
fi
}
main "$@"