-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
170 lines (144 loc) · 5.1 KB
/
install.sh
File metadata and controls
170 lines (144 loc) · 5.1 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
#!/usr/bin/env bash
# install.sh - Opinionated first-time installer for sqlBackup
# - Installs system packages (apt/yum/brew)
# - Creates a Python venv, upgrades pip/setuptools/wheel
# - Installs Python requirements and package (editable)
# - Creates `config.ini` from template and ensures a writable backup_dir
set -euo pipefail
echo "== sqlBackup first-time installer =="
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT_DIR"
# Helpers
has_cmd() { command -v "$1" >/dev/null 2>&1; }
info() { echo "[INFO] $*"; }
warn() { echo "[WARN] $*"; }
err() { echo "[ERROR] $*"; exit 1; }
# Require python3
if ! has_cmd python3; then
err "Python 3 is required but not found in PATH. Install python3 and rerun."
fi
python_version=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
py_major=$(echo "$python_version" | cut -d. -f1)
py_minor=$(echo "$python_version" | cut -d. -f2)
if [ "$py_major" -ne 3 ] || [ "$py_minor" -lt 6 ]; then
err "Python 3.6+ required, found Python $python_version"
fi
info "Python $python_version found"
# Detect package manager for system dependencies
PKG_MANAGER=""
if has_cmd apt-get; then
PKG_MANAGER="apt"
elif has_cmd dnf; then
PKG_MANAGER="dnf"
elif has_cmd yum; then
PKG_MANAGER="yum"
elif has_cmd brew; then
PKG_MANAGER="brew"
fi
install_system_deps() {
info "Installing system dependencies"
case "$PKG_MANAGER" in
apt)
sudo apt-get update
sudo apt-get install -y python3-venv python3-pip mysql-client build-essential libssl-dev libffi-dev
;;
dnf)
sudo dnf install -y python3-venv python3-pip mysql-community-client gcc openssl-devel libffi-devel
;;
yum)
sudo yum install -y python3-venv python3-pip mysql-community-client gcc openssl-devel libffi-devel
;;
brew)
brew install python mysql
;;
*)
warn "No supported system package manager found (apt/dnf/yum/brew). Please install: python3-venv, python3-pip, mysql-client, build-essential, libssl-dev, libffi-dev"
;;
esac
}
# Offer to install system deps if manager detected
if [ -n "$PKG_MANAGER" ]; then
info "Detected package manager: $PKG_MANAGER"
read -r -p "Install system dependencies using $PKG_MANAGER? [Y/n] " ans || true
ans=${ans:-Y}
if [[ "$ans" =~ ^[Yy] ]]; then
install_system_deps
else
warn "Skipping system dependency installation. Ensure required packages are present."
fi
else
warn "No package manager detected; ensure required system packages are installed manually."
fi
# Create venv if missing
VENV_DIR="venv"
if [ -d "$VENV_DIR" ]; then
info "Virtualenv '$VENV_DIR' already exists. Reusing."
else
info "Creating virtual environment in $VENV_DIR"
python3 -m venv "$VENV_DIR"
fi
# Activate venv for the remainder of the script
# shellcheck disable=SC1091
. "$VENV_DIR/bin/activate"
info "Upgrading pip, setuptools and wheel"
python -m pip install --upgrade pip setuptools wheel
info "Installing Python dependencies from requirements.txt"
if [ -f requirements.txt ]; then
python -m pip install -r requirements.txt || warn "Some optional requirements failed to install; you can retry later."
else
warn "requirements.txt not found; skipping pip install -r requirements.txt"
fi
info "Installing sqlBackup package in editable mode (development)"
python -m pip install -e .
# Create config.ini from template if missing
if [ ! -f config.ini ]; then
if [ -f config.ini.default ]; then
info "Creating config.ini from config.ini.default"
cp config.ini.default config.ini
# Make backup_dir user-writable by default for first-time installs
python3 - <<'PY'
import configparser, os
cfg = configparser.ConfigParser()
cfg.read('config.ini')
backup_dir = os.path.expanduser('~/backups')
if not cfg.has_section('backup'):
cfg['backup'] = {}
cfg['backup']['backup_dir'] = backup_dir
with open('config.ini', 'w') as f:
cfg.write(f)
print('Wrote config.ini with backup_dir set to', backup_dir)
PY
mkdir -p "$HOME/backups"
info "Created user backup directory: $HOME/backups"
else
warn "config.ini.default not found; please create config.ini manually"
fi
else
info "config.ini already exists; leaving it unchanged"
fi
# Make startup script executable if present
if [ -f sqlBackup.sh ]; then
chmod +x sqlBackup.sh || true
info "Made sqlBackup.sh executable"
fi
info "Installation finished successfully"
cat <<EOF
Next steps:
- Edit 'config.ini' to set your MySQL credentials and notification settings
- Ensure 'backup_dir' in config.ini is writable by the user running sqlBackup
- Run pre-flight checks:
./sqlBackup.sh --check
(or) python3 -m sql_backup
If you plan to run in production, consider creating a systemd unit or cron job that activates the virtualenv and runs the module:
Example systemd unit (edit paths):
[Unit]
Description=sqlBackup service
[Service]
Type=oneshot
User=$(whoami)
WorkingDirectory=$ROOT_DIR
ExecStart=$ROOT_DIR/venv/bin/python -m sql_backup
[Install]
WantedBy=multi-user.target
EOF
exit 0