Traccar has no native ntfy integration, but its "SMS" notification
channel is a generic HTTP webhook (sms.http.* config) under the hood —
pointing it at ntfy's JSON publish API instead of a real SMS gateway is
a well-known trick. Adds an opt-in prompt during install (or reruns)
for an ntfy server URL — self-hosted on this box, a different server
entirely, or public ntfy.sh — a topic name, and optional username/
password if that topic needs auth.
Everything lands in .env only: NOTIFICATOR_TYPES, SMS_HTTP_URL,
SMS_HTTP_TEMPLATE, and (if given) SMS_HTTP_USER/SMS_HTTP_PASSWORD.
traccar's env_file: .env already passes these straight through to the
container with no docker-compose.yml changes needed, since Traccar's
env-var config naming convention already matches these key names
exactly. The topic name itself isn't consumed by Traccar (it's set
per-user via the web UI's Phone field), so it's stored as a `#
NTFY_TOPIC=` comment line purely so reruns can recall it and so it's
discoverable on disk.
Reruns default every prompt to whatever was configured last time
(reading it back out of the existing .env), so accepting the defaults
is a no-op — the same non-destructive-by-default pattern already used
for the DB password.
Fixed a heredoc bug caught while testing this: `${_NTFY_ENV_BLOCK}
TRACCAR_ENV` on one line never matches the heredoc terminator, because
bash matches heredoc delimiters against the literal source line before
variable expansion, not after — regardless of what the variable
expands to. Moved the terminator to its own line, matching the
${_CADDY_NET_SECTION} / TRACCAR_COMPOSE pattern already used a few
lines above for the same reason.
Verified: bash -n, and four full install runs (ntfy disabled, enabled
without auth, enabled with auth, and a rerun accepting defaults) with
`docker compose config` confirming SMS_HTTP_TEMPLATE's JSON and all
other values resolve correctly through env_file with no compose-level
escaping needed.
504 lines
22 KiB
Bash
504 lines
22 KiB
Bash
#!/bin/bash
|
|
# services/traccar.sh — GPS tracking server (Traccar).
|
|
# Part of the modular post-install system (sourced by setup.sh).
|
|
#
|
|
# Can also be run standalone on any machine:
|
|
# sudo bash traccar.sh
|
|
# (Docker must already be installed when run standalone)
|
|
#
|
|
# Ported from ubuntu-post-install-24.04-crowdsec.sh (# ---- TRACCAR ----).
|
|
# Own ~/docker/traccar/ with a standalone docker-compose.yml + config XML.
|
|
|
|
# ── Standalone bootstrap ──────────────────────────────────────────────────────
|
|
# Detected when the script is executed directly rather than sourced by setup.sh.
|
|
# Sets up helpers and globals, then defers execution until after the function
|
|
# definition at the bottom of this file.
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
[[ "$(id -u)" == "0" ]] || { echo "Run with sudo: sudo bash $0"; exit 1; }
|
|
|
|
_SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
_COMMON="$_SELF_DIR/../lib/common.sh"
|
|
|
|
if [[ -f "$_COMMON" ]]; then
|
|
# Full repo present — use the real helpers (picks up ~/docker/.config too)
|
|
# shellcheck source=../lib/common.sh
|
|
source "$_COMMON"
|
|
else
|
|
# One-off copy — inline minimal stubs so the script works without the repo
|
|
log_info() { echo -e "\033[0;34m[INFO]\033[0m $*"; }
|
|
log_success() { echo -e "\033[0;32m[OK]\033[0m $*"; }
|
|
log_warning() { echo -e "\033[1;33m[WARN]\033[0m $*"; }
|
|
log_error() { echo -e "\033[0;31m[ERROR]\033[0m $*" >&2; }
|
|
|
|
require_docker() {
|
|
command -v docker &>/dev/null || {
|
|
log_error "Docker not found. Install it first:"
|
|
log_error " curl -fsSL https://get.docker.com | sudo sh"
|
|
return 1
|
|
}
|
|
docker compose version &>/dev/null || {
|
|
log_error "Docker Compose plugin missing:"
|
|
log_error " sudo apt-get install -y docker-compose-plugin"
|
|
return 1
|
|
}
|
|
}
|
|
|
|
ensure_docker_dir_ownership() {
|
|
chown -R "$ACTUAL_USER:$ACTUAL_USER" "$@" 2>/dev/null || true
|
|
}
|
|
|
|
# Match common.sh's eval-based pattern so local vars in install_* are set correctly
|
|
prompt_yn() {
|
|
local _q="$1" _def="$2" _var="$3" _r
|
|
[[ "${UNATTENDED:-false}" == "true" ]] && { eval "$_var='$_def'"; return; }
|
|
read -r -p " $_q " _r
|
|
eval "$_var='${_r:-$_def}'"
|
|
}
|
|
|
|
configure_caddy_for_service() {
|
|
local _name="$1" _upstream="$2" _subdomain="$3" _extra="${4:-}"
|
|
local _caddy_dir="$DOCKER_DIR/caddy"
|
|
local _caddyfile="$_caddy_dir/Caddyfile"
|
|
local _display_port="${_upstream##*:}"
|
|
|
|
# Determine mode: local Caddy, remote Caddy, or none
|
|
local _mode="none"
|
|
[[ -d "$_caddy_dir" ]] && _mode="local"
|
|
[[ -n "${CADDY_REMOTE_HOST:-}" ]] && [[ "$_mode" != "local" ]] && _mode="remote"
|
|
[[ "$_mode" == "none" ]] && {
|
|
log_info "Access $_name directly on port $_display_port."
|
|
return 0
|
|
}
|
|
|
|
echo ""
|
|
local _do_caddy=""
|
|
if [[ "$_mode" == "remote" ]]; then
|
|
log_info "Remote Caddy configured (${CADDY_REMOTE_HOST})."
|
|
log_info "A snippet file will be saved to ~/docker/caddy-snippets/."
|
|
fi
|
|
read -r -p " Configure Caddy reverse proxy for $_name? [y/N]: " _do_caddy
|
|
[[ "${_do_caddy,,}" == "y" ]] || {
|
|
log_info "Skipping — access at: http://localhost:$_display_port"
|
|
return 0
|
|
}
|
|
|
|
# Domain prompt — pre-fill from SITE_DOMAIN when available
|
|
local _default_domain=""
|
|
if [[ -n "${SITE_DOMAIN:-}" ]] && [[ "$SITE_DOMAIN" != "example.com" ]]; then
|
|
_default_domain="${_subdomain}.${SITE_DOMAIN}"
|
|
log_info "Default: $_default_domain"
|
|
fi
|
|
local _domain=""
|
|
read -r -p " Domain [${_default_domain:-required}]: " _domain
|
|
_domain="${_domain:-$_default_domain}"
|
|
[[ -n "$_domain" ]] || { log_warning "No domain entered — skipping Caddy."; return 0; }
|
|
|
|
# Build upstream — remote Caddy uses host IP:port, not container name
|
|
local _block_upstream="$_upstream"
|
|
if [[ "$_mode" == "remote" ]]; then
|
|
_block_upstream="${CADDY_REMOTE_HOST}:${_display_port}"
|
|
fi
|
|
|
|
local _site_block
|
|
_site_block="$(cat << CBLOCK
|
|
|
|
# $_name
|
|
${_domain} {
|
|
reverse_proxy ${_block_upstream}
|
|
|
|
header {
|
|
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
|
|
X-Content-Type-Options "nosniff"
|
|
X-Frame-Options "SAMEORIGIN"
|
|
Referrer-Policy "strict-origin-when-cross-origin"
|
|
}
|
|
|
|
log {
|
|
output file /var/log/caddy/${_domain}.log
|
|
format json
|
|
}
|
|
${_extra}
|
|
}
|
|
CBLOCK
|
|
)"
|
|
|
|
if [[ "$_mode" == "local" ]]; then
|
|
if [[ -f "$_caddyfile" ]]; then
|
|
local _bk="$_caddy_dir/Caddyfile.backup.$(date +%Y%m%d-%H%M%S)"
|
|
cp "$_caddyfile" "$_bk"
|
|
log_info "Backed up Caddyfile to $(basename "$_bk")"
|
|
else
|
|
touch "$_caddyfile"
|
|
fi
|
|
|
|
if grep -q "^${_domain}" "$_caddyfile" 2>/dev/null; then
|
|
log_warning "$_domain already in Caddyfile"
|
|
local _ow=""
|
|
read -r -p " Overwrite? [y/N]: " _ow
|
|
[[ "${_ow,,}" == "y" ]] || { log_info "Keeping existing entry."; return 0; }
|
|
sed -i "/^${_domain}/,/^}/d" "$_caddyfile"
|
|
fi
|
|
|
|
printf '%s\n' "$_site_block" >> "$_caddyfile"
|
|
log_success "Added $_domain to Caddyfile"
|
|
docker exec caddy caddy fmt --overwrite /etc/caddy/Caddyfile 2>/dev/null || true
|
|
if docker exec caddy caddy reload --config /etc/caddy/Caddyfile 2>/dev/null; then
|
|
log_success "$_name accessible at: https://$_domain"
|
|
else
|
|
log_warning "Reload failed — check: docker logs caddy"
|
|
log_info "Manual reload: docker exec caddy caddy reload --config /etc/caddy/Caddyfile"
|
|
fi
|
|
else
|
|
local _snippet_dir="$DOCKER_DIR/caddy-snippets"
|
|
local _snippet_file="$_snippet_dir/${_subdomain}.caddy"
|
|
mkdir -p "$_snippet_dir"
|
|
printf '%s\n' "$_site_block" > "$_snippet_file"
|
|
chown "$ACTUAL_USER:$ACTUAL_USER" "$_snippet_file" 2>/dev/null || true
|
|
log_success "Snippet saved: $_snippet_file"
|
|
log_info "Copy to Caddy machine:"
|
|
log_info " scp $_snippet_file caddy-host:~/caddy-snippets/"
|
|
log_info " rsync -av $_snippet_dir/ caddy-host:~/caddy-snippets/ (all at once)"
|
|
fi
|
|
}
|
|
write_readme() {
|
|
local _dir="$1"; shift
|
|
mkdir -p "$_dir"
|
|
cat > "$_dir/README.md"
|
|
}
|
|
fi
|
|
|
|
# Globals — ACTUAL_USER/ACTUAL_HOME must come before DOCKER_DIR
|
|
# ($HOME under sudo is /root, not the real user's home)
|
|
ACTUAL_USER="${ACTUAL_USER:-${SUDO_USER:-$USER}}"
|
|
ACTUAL_HOME="$(getent passwd "$ACTUAL_USER" 2>/dev/null | cut -d: -f6 || echo "${HOME:-/root}")"
|
|
DOCKER_DIR="${DOCKER_DIR:-$ACTUAL_HOME/docker}"
|
|
DRY_RUN="${DRY_RUN:-false}"
|
|
UNATTENDED="${UNATTENDED:-false}"
|
|
SITE_TZ="${SITE_TZ:-$(cat /etc/timezone 2>/dev/null || echo UTC)}"
|
|
SITE_DOMAIN="${SITE_DOMAIN:-example.com}"
|
|
SITE_CADDY_NET="${SITE_CADDY_NET:-caddy_net}"
|
|
|
|
register_service() { :; } # no-op — no wizard to register into
|
|
_RUN_STANDALONE=1
|
|
fi
|
|
# ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
register_service traccar utilities "GPS tracking server — phones, vehicles, assets (Traccar)" 8082
|
|
|
|
install_traccar() {
|
|
require_docker || return 1
|
|
|
|
local TRACCAR_DIR="$DOCKER_DIR/traccar"
|
|
|
|
if [ "$DRY_RUN" = true ]; then
|
|
echo "[DRY-RUN] Traccar would:"
|
|
echo " - Create $TRACCAR_DIR with docker-compose.yml + .env"
|
|
echo " - Deploy a PostgreSQL database container (Traccar no longer ships H2)"
|
|
echo " - Point Traccar at it via env vars (CONFIG_USE_ENVIRONMENT_VARIABLES) — no secrets in a config file"
|
|
echo " - Deploy an autoheal container that restarts Traccar if its healthcheck fails"
|
|
echo " - Expose port 8082 (web) and 5000-5150 (device protocols; 5038/5060/5061 skipped — Asterisk keeps priority on those)"
|
|
echo " - No default login — register the first account at the web UI, it becomes admin"
|
|
echo " - Offer optional ntfy push notifications (self-hosted anywhere, or ntfy.sh)"
|
|
echo " - Offer a Caddy reverse proxy and to start the container"
|
|
return 0
|
|
fi
|
|
|
|
mkdir -p "$TRACCAR_DIR"
|
|
# Non-recursive on purpose — a rerun already has a `db/` full of Postgres's
|
|
# own data files, owned by whatever uid the postgres container runs as
|
|
# internally, not $ACTUAL_USER. ensure_docker_dir_ownership's chown -R
|
|
# would reassign all of those to $ACTUAL_USER, and Postgres can't read
|
|
# its own files anymore afterward ("could not open file
|
|
# global/pg_filenode.map: Permission denied") — confirmed live on a
|
|
# rerun. logs/ and data/ get their own chown -R further down instead;
|
|
# db/ is never touched by this script again once created.
|
|
chown "$ACTUAL_USER:$ACTUAL_USER" "$TRACCAR_DIR" 2>/dev/null || true
|
|
cd "$TRACCAR_DIR" || return 1
|
|
|
|
# Reuse an existing DB password across reruns instead of generating a new
|
|
# one — the Postgres volume keeps the original password from its first
|
|
# init, so overwriting .env with a fresh one would lock Traccar out.
|
|
local DB_PASS=""
|
|
if [ -f ".env" ]; then
|
|
DB_PASS=$(grep '^POSTGRES_PASSWORD=' .env | cut -d= -f2-)
|
|
fi
|
|
[ -n "$DB_PASS" ] || DB_PASS=$(generate_password 32)
|
|
|
|
# ── Optional: ntfy push notifications ──────────────────────────────────
|
|
# Traccar has no native ntfy support, but its "SMS" notification channel
|
|
# is really just a generic HTTP webhook (sms.http.* config) under the
|
|
# hood — pointing it at ntfy's JSON publish API instead of a real SMS
|
|
# gateway is a well-known trick. Reuse whatever was configured last time
|
|
# so a rerun (e.g. to pick up an unrelated fix) doesn't silently drop it.
|
|
local _EXISTING_SMS_HTTP_URL="" _EXISTING_NTFY_TOPIC="" _EXISTING_SMS_HTTP_USER=""
|
|
if [ -f ".env" ]; then
|
|
_EXISTING_SMS_HTTP_URL=$(grep '^SMS_HTTP_URL=' .env | cut -d= -f2-)
|
|
_EXISTING_NTFY_TOPIC=$(grep '^# NTFY_TOPIC=' .env | cut -d= -f2-)
|
|
_EXISTING_SMS_HTTP_USER=$(grep '^SMS_HTTP_USER=' .env | cut -d= -f2-)
|
|
fi
|
|
|
|
echo ""
|
|
local _ntfy_default_enable="n"
|
|
[ -n "$_EXISTING_SMS_HTTP_URL" ] && _ntfy_default_enable="y"
|
|
local ENABLE_NTFY=""
|
|
prompt_yn "Set up push notifications via ntfy — self-hosted (this box or another server) or public ntfy.sh? (y/n):" "$_ntfy_default_enable" ENABLE_NTFY
|
|
|
|
local SMS_HTTP_URL="" NTFY_TOPIC="" SMS_HTTP_USER="" SMS_HTTP_PASSWORD=""
|
|
if [[ "$ENABLE_NTFY" =~ ^[Yy]$ ]]; then
|
|
# Suggest this box's own ntfy install if one exists, but never assume
|
|
# it — the whole point is this can point at any server, anywhere.
|
|
local _default_ntfy_url="$_EXISTING_SMS_HTTP_URL"
|
|
if [ -z "$_default_ntfy_url" ] && [ -d "$DOCKER_DIR/ntfy" ]; then
|
|
if [ -n "$SITE_DOMAIN" ] && [ "$SITE_DOMAIN" != "example.com" ]; then
|
|
_default_ntfy_url="https://ntfy.${SITE_DOMAIN}"
|
|
else
|
|
_default_ntfy_url="http://localhost:8090"
|
|
fi
|
|
fi
|
|
prompt_text "ntfy server URL (self-hosted here, on another server, or https://ntfy.sh) [${_default_ntfy_url:-required}]:" "$_default_ntfy_url" SMS_HTTP_URL
|
|
|
|
if [ -z "$SMS_HTTP_URL" ]; then
|
|
log_warning "No ntfy URL entered — skipping notification setup."
|
|
else
|
|
local _default_topic="${_EXISTING_NTFY_TOPIC:-traccar-$(generate_password 8)}"
|
|
prompt_text "ntfy topic for Traccar alerts [$_default_topic]:" "$_default_topic" NTFY_TOPIC
|
|
|
|
local _ntfy_needs_auth="" _default_auth="n"
|
|
[ -n "$_EXISTING_SMS_HTTP_USER" ] && _default_auth="y"
|
|
prompt_yn "Does that ntfy topic require a username/password? (y/n):" "$_default_auth" _ntfy_needs_auth
|
|
if [[ "$_ntfy_needs_auth" =~ ^[Yy]$ ]]; then
|
|
prompt_text "ntfy username [${_EXISTING_SMS_HTTP_USER:-required}]:" "$_EXISTING_SMS_HTTP_USER" SMS_HTTP_USER
|
|
prompt_text "ntfy password:" "" SMS_HTTP_PASSWORD
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
local TZ_VAL="${SITE_TZ:-$(cat /etc/timezone 2>/dev/null || echo UTC)}"
|
|
|
|
# Mirrors configure_caddy_for_service's own mode resolution (lib/common.sh)
|
|
# so this matches whatever Caddy setup the site actually has: an explicit
|
|
# CADDY_MODE from the site config wins, then a local ~/docker/caddy, then
|
|
# the legacy CADDY_REMOTE_HOST var. Only "local" joins caddy_net — a remote
|
|
# Caddy box can't resolve container names on this host's bridge network
|
|
# anyway, it reaches Traccar via this host's published 8082 port instead.
|
|
local _CADDY_MODE="${CADDY_MODE:-none}"
|
|
[ "$_CADDY_MODE" = "none" ] && [ -d "$DOCKER_DIR/caddy" ] && _CADDY_MODE="local"
|
|
[ "$_CADDY_MODE" = "none" ] && [ -n "${CADDY_REMOTE_HOST:-}" ] && _CADDY_MODE="remote"
|
|
|
|
local _CADDY_NET_BLOCK=""
|
|
local _CADDY_NET_SECTION=""
|
|
if [ "$_CADDY_MODE" = "local" ]; then
|
|
_CADDY_NET_BLOCK=" networks:
|
|
- caddy_net
|
|
"
|
|
_CADDY_NET_SECTION="
|
|
networks:
|
|
caddy_net:
|
|
external: true
|
|
name: ${SITE_CADDY_NET:-caddy_net}
|
|
"
|
|
fi
|
|
|
|
cat > docker-compose.yml << TRACCAR_COMPOSE
|
|
name: traccar
|
|
|
|
services:
|
|
db:
|
|
image: postgres:15-alpine
|
|
container_name: traccar-db
|
|
hostname: traccar-db
|
|
restart: unless-stopped
|
|
env_file: .env
|
|
volumes:
|
|
- ./db:/var/lib/postgresql/data
|
|
${_CADDY_NET_BLOCK} healthcheck:
|
|
test: ["CMD-SHELL", "pg_isready -U \${POSTGRES_USER} -d \${POSTGRES_DB}"]
|
|
interval: 10s
|
|
timeout: 5s
|
|
retries: 5
|
|
|
|
traccar:
|
|
image: traccar/traccar:latest
|
|
container_name: traccar
|
|
hostname: traccar
|
|
restart: unless-stopped
|
|
env_file: .env
|
|
depends_on:
|
|
db:
|
|
condition: service_healthy
|
|
labels:
|
|
- "autoheal=true"
|
|
environment:
|
|
CONFIG_USE_ENVIRONMENT_VARIABLES: "true"
|
|
DATABASE_DRIVER: org.postgresql.Driver
|
|
DATABASE_URL: jdbc:postgresql://traccar-db:5432/\${POSTGRES_DB}?sslmode=disable
|
|
DATABASE_USER: \${POSTGRES_USER}
|
|
DATABASE_PASSWORD: \${POSTGRES_PASSWORD}
|
|
healthcheck:
|
|
test: ["CMD", "wget", "-q", "--spider", "http://localhost:8082/api/health"]
|
|
interval: 2m
|
|
timeout: 5s
|
|
start_period: 1h
|
|
retries: 3
|
|
volumes:
|
|
- ./logs:/opt/traccar/logs:rw
|
|
- ./data:/opt/traccar/data:rw
|
|
ports:
|
|
- "8082:8082"
|
|
# 5038 (AMI), 5060 (SIP, tcp+udp), and 5061 (SIP TLS, tcp) are skipped:
|
|
# they're Asterisk's ports (services/asterisk.sh runs Asterisk with
|
|
# network_mode: host, so it binds them directly on the host, not
|
|
# through Docker networking). Publishing the full 5000-5150 range here
|
|
# would fight Asterisk for those exact host ports on any box running
|
|
# both services from this repo. Confirmed live: this is what made
|
|
# "docker network connect caddy_net traccar" and then a plain
|
|
# `docker compose up -d` both fail with "failed to bind host port
|
|
# 0.0.0.0:5038/tcp" and then "...5060/tcp: address already in use" on
|
|
# a box with Asterisk's PSTN trunk already installed. Checked every
|
|
# other network_mode: host service in this repo (caddy, homeassistant,
|
|
# kyber-server, lyrion, mattermost, watchyourlan, wolf-pair, wolf) —
|
|
# none of them land in 5000-5150, so Asterisk is the only conflict.
|
|
- "5000-5037:5000-5037"
|
|
- "5039-5059:5039-5059"
|
|
- "5062-5150:5062-5150"
|
|
- "5000-5059:5000-5059/udp"
|
|
- "5061-5150:5061-5150/udp"
|
|
${_CADDY_NET_BLOCK}
|
|
autoheal:
|
|
image: willfarrell/autoheal:latest
|
|
container_name: traccar-autoheal
|
|
restart: unless-stopped
|
|
environment:
|
|
AUTOHEAL_CONTAINER_LABEL: autoheal
|
|
AUTOHEAL_INTERVAL: 60
|
|
AUTOHEAL_START_PERIOD: 3600
|
|
volumes:
|
|
- /var/run/docker.sock:/var/run/docker.sock
|
|
${_CADDY_NET_SECTION}
|
|
TRACCAR_COMPOSE
|
|
|
|
# Traccar's env-var config naming (dots→underscore, camelCase→SCREAMING_
|
|
# SNAKE) already matches these key names exactly, and `traccar`'s
|
|
# env_file: .env passes them straight through — no docker-compose.yml
|
|
# changes needed for this at all. NTFY_TOPIC isn't read by anything; it's
|
|
# a comment purely so a rerun can recall it (see the grep for it above)
|
|
# and so it's easy to find on disk instead of scrolling back through chat.
|
|
local _NTFY_ENV_BLOCK=""
|
|
if [ -n "$SMS_HTTP_URL" ]; then
|
|
_NTFY_ENV_BLOCK="
|
|
# ntfy push notifications — piggybacks on Traccar's \"SMS\" channel, which is
|
|
# really just a generic HTTP webhook under the hood.
|
|
NOTIFICATOR_TYPES=web,sms
|
|
SMS_HTTP_URL=$SMS_HTTP_URL
|
|
SMS_HTTP_TEMPLATE={\"topic\": \"{phone}\", \"message\": \"{message}\"}
|
|
# NTFY_TOPIC=$NTFY_TOPIC
|
|
# ^ set this as your Traccar user's Phone field (top-right avatar -> Edit),
|
|
# then enable \"SMS\" per event under the notifications bell.
|
|
"
|
|
if [ -n "$SMS_HTTP_USER" ]; then
|
|
_NTFY_ENV_BLOCK="${_NTFY_ENV_BLOCK}SMS_HTTP_USER=$SMS_HTTP_USER
|
|
SMS_HTTP_PASSWORD=$SMS_HTTP_PASSWORD
|
|
"
|
|
fi
|
|
fi
|
|
|
|
cat > .env << TRACCAR_ENV
|
|
TZ=$TZ_VAL
|
|
CADDY_NET=$SITE_CADDY_NET
|
|
|
|
# PostgreSQL — backs Traccar's database (Traccar's docker image no longer
|
|
# bundles the H2 driver, so a real database is required). Traccar reads
|
|
# these directly (CONFIG_USE_ENVIRONMENT_VARIABLES in docker-compose.yml)
|
|
# instead of a config file, so this is the only place the credentials live.
|
|
POSTGRES_DB=traccar
|
|
POSTGRES_USER=traccar
|
|
POSTGRES_PASSWORD=$DB_PASS
|
|
${_NTFY_ENV_BLOCK}
|
|
TRACCAR_ENV
|
|
chmod 600 .env
|
|
|
|
mkdir -p logs data db
|
|
|
|
# db/ is deliberately excluded — see the comment on the earlier chown.
|
|
chown "$ACTUAL_USER:$ACTUAL_USER" "$TRACCAR_DIR" docker-compose.yml .env
|
|
chown -R "$ACTUAL_USER:$ACTUAL_USER" logs data
|
|
log_success "Traccar configured at $TRACCAR_DIR"
|
|
|
|
configure_caddy_for_service "Traccar" "traccar:8082" "traccar"
|
|
|
|
local _NTFY_README_BLOCK=""
|
|
if [ -n "$SMS_HTTP_URL" ]; then
|
|
_NTFY_README_BLOCK="
|
|
## Push notifications (ntfy)
|
|
Traccar has no native ntfy support — this uses its \"SMS\" channel as a
|
|
generic HTTP webhook pointed at ntfy's publish API instead of a real SMS
|
|
gateway. Configured in \`.env\`: \`SMS_HTTP_URL\`, \`SMS_HTTP_TEMPLATE\`
|
|
(and \`SMS_HTTP_USER\`/\`SMS_HTTP_PASSWORD\` if that topic needs auth).
|
|
|
|
- ntfy server: $SMS_HTTP_URL
|
|
- Topic: $NTFY_TOPIC — set this as your Traccar user's Phone field
|
|
(top-right avatar → Edit)
|
|
- Enable \"SMS\" as a channel for whichever events you want, under the
|
|
notifications bell — the UI still calls it \"SMS\", that's just the label.
|
|
- Subscribe to the topic in the ntfy app to receive them.
|
|
"
|
|
fi
|
|
|
|
write_readme "$TRACCAR_DIR" << MD
|
|
# Traccar
|
|
|
|
GPS tracking server. Track phones, vehicles, and assets via the Traccar
|
|
Android/iOS app, OwnTracks, or any of 200+ supported device protocols.
|
|
|
|
- Web UI: http://localhost:8082
|
|
- No default login — Traccar ships with no built-in account. Open the web UI
|
|
and register the first user; it's automatically made admin. Self-registration
|
|
stays open to anyone who reaches this server until you turn it off, so do
|
|
this right away, then go to Settings → Server → Permissions and uncheck
|
|
Registration.
|
|
- Device protocols: ports 5000-5150 (TCP + UDP; 5038/tcp, 5060/tcp+udp, and
|
|
5061/tcp are skipped — reserved for Asterisk's AMI and SIP if this box also
|
|
runs Asterisk from this repo, which gets priority on those ports)
|
|
- App data: \`data/\` and \`logs/\`
|
|
- Database: PostgreSQL (\`traccar-db\` container, data in \`db/\`)
|
|
- All database settings (name, user, password) live in \`.env\` — Traccar
|
|
reads them directly via env vars, nothing is duplicated in a config file.
|
|
Change the password there (then recreate both containers) if you need to
|
|
rotate it.
|
|
- Autoheal: \`traccar-autoheal\` restarts the \`traccar\` container if its healthcheck fails
|
|
${_NTFY_README_BLOCK}
|
|
## Manage
|
|
\`\`\`bash
|
|
cd $TRACCAR_DIR
|
|
docker compose up -d # start
|
|
docker compose down # stop
|
|
docker compose logs -f # logs
|
|
docker compose pull && docker compose up -d # update
|
|
\`\`\`
|
|
|
|
## Mobile apps
|
|
- Traccar Client (Android/iOS): set server to \`http://YOUR-IP:8082\`
|
|
- OwnTracks (Android/iOS): configure HTTP endpoint to Traccar
|
|
MD
|
|
|
|
local START_TRACCAR=""
|
|
prompt_yn "Start Traccar now? (y/n):" "y" START_TRACCAR
|
|
if [ "$START_TRACCAR" = "y" ] || [ "$START_TRACCAR" = "Y" ]; then
|
|
docker compose up -d && log_success "Traccar started" || log_warning "Failed to start — check: docker compose logs"
|
|
fi
|
|
|
|
echo ""
|
|
echo " Access at: http://localhost:8082"
|
|
echo " No default login — register the first account now; it becomes admin."
|
|
echo " Then disable further registration: Settings → Server → Permissions."
|
|
if [ -n "$SMS_HTTP_URL" ]; then
|
|
echo ""
|
|
echo " ntfy notifications: set your user's Phone field to '$NTFY_TOPIC'"
|
|
echo " (top-right avatar → Edit), then enable \"SMS\" per event under the bell."
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
# Run immediately when executed directly (deferred until after function definition)
|
|
[[ "${_RUN_STANDALONE:-0}" == 1 ]] && install_traccar
|