-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup-vm-data-maintenance.sh
More file actions
150 lines (118 loc) · 3.31 KB
/
Copy pathsetup-vm-data-maintenance.sh
File metadata and controls
150 lines (118 loc) · 3.31 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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_NAME="setup-vm-data-maintenance"
# -------- defaults --------
REPO_BASE="${REPO_BASE:-https://gitea.qortal.link/crowetic/QORTector-scripts}"
BRANCH="${BRANCH:-main}"
RAW_BASE="${RAW_BASE:-$REPO_BASE/raw/branch/$BRANCH}"
REMOTE_SCRIPT="vm-data-maintenance.sh"
REMOTE_CONF="vm-data-maintenance.conf"
BIN_PATH="/usr/local/sbin/vm-data-maintenance"
CONF_PATH="/etc/vm-data-maintenance.conf"
SERVICE_PATH="/etc/systemd/system/vm-data-maintenance.service"
TIMER_PATH="/etc/systemd/system/vm-data-maintenance.timer"
FORCE=0
UNINSTALL=0
log() { echo "[$(date -Is)] $SCRIPT_NAME: $*"; }
die() { echo "ERROR: $*" >&2; exit 1; }
need_root() {
[[ "${EUID:-$(id -u)}" -eq 0 ]] || die "Run as root (sudo)."
}
have_cmd() { command -v "$1" >/dev/null 2>&1; }
fetch() {
local url="$1" out="$2"
if have_cmd curl; then
curl -fsSL "$url" -o "$out"
elif have_cmd wget; then
wget -qO "$out" "$url"
else
die "Need curl or wget"
fi
}
usage() {
cat <<EOF
$SCRIPT_NAME [options]
Options:
--force Overwrite existing config and script
--uninstall Remove service, timer, and script
-h, --help Show help
Env overrides:
REPO_BASE, BRANCH
EOF
}
# -------- parse args --------
while [[ $# -gt 0 ]]; do
case "$1" in
--force) FORCE=1 ;;
--uninstall) UNINSTALL=1 ;;
-h|--help) usage; exit 0 ;;
*) die "Unknown argument: $1" ;;
esac
shift
done
need_root
# -------- UNINSTALL --------
if [[ "$UNINSTALL" == "1" ]]; then
log "Uninstalling VM Data Maintenance..."
systemctl disable --now vm-data-maintenance.timer 2>/dev/null || true
rm -f "$SERVICE_PATH" "$TIMER_PATH"
rm -f "$BIN_PATH"
log "Removed service, timer, and binary."
if [[ -f "$CONF_PATH" ]]; then
read -rp "Remove config $CONF_PATH? [y/N]: " ans
[[ "$ans" =~ ^[Yy]$ ]] && rm -f "$CONF_PATH" && log "Config removed."
fi
systemctl daemon-reload
log "Uninstall complete."
exit 0
fi
# -------- INSTALL --------
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT
SCRIPT_URL="$RAW_BASE/$REMOTE_SCRIPT"
CONF_URL="$RAW_BASE/$REMOTE_CONF"
log "Installing from $SCRIPT_URL"
fetch "$SCRIPT_URL" "$TMPDIR/$REMOTE_SCRIPT" || die "Failed to download script"
# Config
if [[ -f "$CONF_PATH" && "$FORCE" -ne 1 ]]; then
log "Config exists, skipping (use --force to overwrite)"
else
fetch "$CONF_URL" "$TMPDIR/$REMOTE_CONF" || die "Failed to download config"
install -m 0644 "$TMPDIR/$REMOTE_CONF" "$CONF_PATH"
log "Installed config -> $CONF_PATH"
fi
# Script
if [[ -f "$BIN_PATH" && "$FORCE" -ne 1 ]]; then
log "Binary exists, skipping (use --force to overwrite)"
else
install -m 0755 "$TMPDIR/$REMOTE_SCRIPT" "$BIN_PATH"
log "Installed script -> $BIN_PATH"
fi
# -------- systemd units --------
cat >"$SERVICE_PATH" <<EOF
[Unit]
Description=VM Data Maintenance (logs/tmp/caches + fstrim)
After=network-online.target
[Service]
Type=oneshot
Environment=CONFIG_FILE=$CONF_PATH
ExecStart=$BIN_PATH
Nice=10
IOSchedulingClass=best-effort
IOSchedulingPriority=7
EOF
cat >"$TIMER_PATH" <<'EOF'
[Unit]
Description=Run VM Data Maintenance daily
[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=1h
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now vm-data-maintenance.timer
log "Install complete."
log "Test with dry run:"
log " sudo env DRY_RUN=1 $BIN_PATH"