Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
897ad48a2d | ||
|
|
a488d2ea08 | ||
|
|
fa1817e614 |
No files matched your search
@@ -8,44 +8,115 @@
|
||||
|
||||
Всё это делает один бинарник `backupctl`. Как он устроен внутри — в [docs/DESIGN.md](docs/DESIGN.md).
|
||||
|
||||
## Сборка
|
||||
|
||||
```bash
|
||||
make build # dist/backupctl-linux-amd64, dist/backupctl-linux-arm64
|
||||
make test
|
||||
```
|
||||
|
||||
Готовые бинарники собирает Gitea Actions. `.gitea/workflows/ci.yml` на каждый push и PR проверяет форматирование, запускает vet и тесты и собирает бинарники. `release.yml` на тег `v*` публикует релиз с `backupctl-linux-{amd64,arm64}`, `checksums.txt` и файлами из `deploy/`:
|
||||
|
||||
```bash
|
||||
git tag v0.1.0 && git push origin v0.1.0
|
||||
```
|
||||
|
||||
Без make:
|
||||
|
||||
```bash
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "-s -w" -o dist/backupctl-linux-amd64 ./cmd/backupctl
|
||||
```
|
||||
|
||||
## Установка сервера
|
||||
|
||||
Ставим из [последнего релиза](https://gitea.hsrv.site/mrleo1nid/backup-master/releases/latest). Ссылка `releases/download/latest/...` всегда ведёт на свежую версию. Чтобы поставить конкретную версию, замените `latest` на тег, например `v0.1.0`.
|
||||
|
||||
```bash
|
||||
REL=https://gitea.hsrv.site/mrleo1nid/backup-master/releases/download/latest
|
||||
ARCH=$(dpkg --print-architecture) # amd64 или arm64
|
||||
|
||||
# пользователь и каталоги
|
||||
sudo useradd -r -s /usr/sbin/nologin -d /var/lib/backup-master backup-master
|
||||
sudo install -d -o backup-master -g backup-master -m 700 /var/lib/backup-master
|
||||
sudo install -d /opt/backup-master/dist
|
||||
sudo cp dist/backupctl-linux-* /opt/backup-master/dist/
|
||||
sudo cp dist/backupctl-linux-amd64 /usr/local/bin/backupctl
|
||||
|
||||
# бинарники: свой — в /usr/local/bin, оба — в dist для раздачи агентам
|
||||
for a in amd64 arm64; do sudo curl -fsSL "$REL/backupctl-linux-$a" -o "/opt/backup-master/dist/backupctl-linux-$a"; done
|
||||
sudo curl -fsSL "$REL/checksums.txt" -o /tmp/backupctl-checksums.txt
|
||||
(cd /opt/backup-master/dist && grep backupctl-linux /tmp/backupctl-checksums.txt | sha256sum -c -)
|
||||
sudo install -m 755 "/opt/backup-master/dist/backupctl-linux-$ARCH" /usr/local/bin/backupctl
|
||||
backupctl version
|
||||
|
||||
# администратор веб-морды
|
||||
sudo -u backup-master BM_DATA=/var/lib/backup-master backupctl admin-passwd admin
|
||||
sudo cp deploy/backup-master.service /etc/systemd/system/ # поправьте BM_PUBLIC_URL
|
||||
|
||||
# systemd: поправьте BM_PUBLIC_URL на внешний адрес сервера
|
||||
sudo curl -fsSL "$REL/backup-master.service" -o /etc/systemd/system/backup-master.service
|
||||
sudo sed -i 's|https://backup.example.com|https://backup.ВАШ-ДОМЕН|' /etc/systemd/system/backup-master.service
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now backup-master
|
||||
```
|
||||
|
||||
Сервер слушает `127.0.0.1:8080`, TLS даёт Caddy (пример — `deploy/Caddyfile.example`). Агентам сервер раздаёт бинарники из `BM_DIST`. Для своей архитектуры сервер раздаёт сам себя.
|
||||
Сервер слушает `127.0.0.1:8080`, TLS даёт Caddy:
|
||||
|
||||
В «Настройках» укажите топик ntfy. Уведомления приходят:
|
||||
```bash
|
||||
curl -fsSL "$REL/Caddyfile.example" # блок для /etc/caddy/Caddyfile, замените домен
|
||||
sudo systemctl reload caddy
|
||||
```
|
||||
|
||||
Агентам сервер раздаёт бинарники из `BM_DIST` (`/opt/backup-master/dist`).
|
||||
|
||||
### Caddy на другом хосте (например, сервер в VM Proxmox, Caddy на хосте)
|
||||
|
||||
Здесь Caddy проксирует не на `127.0.0.1`, а на IP виртуалки. В примере VM — `10.0.0.10`, хост Proxmox — `10.0.0.1`.
|
||||
|
||||
На VM сервис должен слушать сеть, а порт должен быть открыт только хосту с Caddy:
|
||||
|
||||
```bash
|
||||
sudo sed -i 's|BM_LISTEN=127.0.0.1:8080|BM_LISTEN=0.0.0.0:8080|' /etc/systemd/system/backup-master.service
|
||||
sudo systemctl daemon-reload && sudo systemctl restart backup-master
|
||||
sudo ufw allow from 10.0.0.1 to any port 8080 proto tcp # если на VM включён UFW
|
||||
```
|
||||
|
||||
На хосте с Caddy сначала проверьте доступ — должно вернуться `ok`:
|
||||
|
||||
```bash
|
||||
curl -s http://10.0.0.10:8080/healthz
|
||||
```
|
||||
|
||||
Затем укажите в Caddyfile адрес VM:
|
||||
|
||||
```
|
||||
backup.example.com {
|
||||
request_body {
|
||||
max_size 200GB
|
||||
}
|
||||
reverse_proxy 10.0.0.10:8080 {
|
||||
transport http {
|
||||
read_timeout 6h
|
||||
write_timeout 6h
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
sudo systemctl reload caddy
|
||||
```
|
||||
|
||||
`BM_TRUST_PROXY=1` (включён в юните) говорит серверу брать IP клиента из `X-Forwarded-For` от Caddy — по нему ограничивается перебор пароля. Поэтому порт 8080 должен быть доступен **только** с хоста Caddy, иначе заголовок можно подделать. Если включён файрвол Proxmox для VM, разрешите 8080 и там.
|
||||
|
||||
Если Caddy с хоста получает 502, а на VM `curl http://127.0.0.1:8080/healthz` отвечает `ok`, проблема в `BM_LISTEN` или в файрволе.
|
||||
|
||||
### Администратор
|
||||
|
||||
Логин и пароль задаёт одна команда. Её же используют, чтобы сменить логин, пароль или оба сразу. Перезапуск сервиса не нужен, все открытые сессии сбрасываются:
|
||||
|
||||
```bash
|
||||
sudo -u backup-master BM_DATA=/var/lib/backup-master backupctl admin-passwd новый_логин
|
||||
```
|
||||
|
||||
**Обновление сервера:** снова выполните блок «бинарники», затем `sudo systemctl restart backup-master`. Агенты обновляются командой `curl -fsSL https://backup.ВАШ-ДОМЕН/agent.sh | sudo bash` без `--token`, конфиг при этом сохраняется.
|
||||
|
||||
### Уведомления
|
||||
|
||||
В «Настройках» включаются каналы: **ntfy** и/или **Telegram**. Оба необязательные, уведомления уходят во все настроенные.
|
||||
|
||||
Уведомления приходят:
|
||||
- об ошибках бэкапа;
|
||||
- когда от клиента нет бэкапа дольше заданного срока (по умолчанию 26 ч);
|
||||
- если включено — и об успешных бэкапах.
|
||||
- если включено — и об успешных бэкапах. В Telegram они приходят без звука.
|
||||
|
||||
Каждое уведомление содержит ссылку на карточку клиента.
|
||||
|
||||
Как подключить Telegram:
|
||||
1. Создайте бота у [@BotFather](https://t.me/BotFather) и скопируйте токен.
|
||||
2. Напишите боту что-нибудь (или добавьте его в группу). Затем откройте `https://api.telegram.org/bot<токен>/getUpdates` и возьмите `chat.id`. У групп он вида `-100…`.
|
||||
3. В «Настройках» укажите токен и chat id. Чатов можно несколько, через запятую. Для группы с темами дополнительно укажите ID темы.
|
||||
4. Отметьте «Отправить тестовое уведомление» и сохраните — на странице будет видно, дошло ли сообщение в каждый канал.
|
||||
|
||||
Если `api.telegram.org` недоступен с сервера бэкапов, укажите в поле «Адрес Bot API» свой [Bot API сервер](https://github.com/tdlib/telegram-bot-api) или прокси.
|
||||
|
||||
## Добавление клиента
|
||||
|
||||
@@ -126,6 +197,29 @@ categories:
|
||||
restore_cmd: mysql < "$DUMP_FILE"
|
||||
```
|
||||
|
||||
## Сборка из исходников
|
||||
|
||||
```bash
|
||||
make build # dist/backupctl-linux-amd64, dist/backupctl-linux-arm64
|
||||
make test
|
||||
```
|
||||
|
||||
Без make:
|
||||
|
||||
```bash
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "-s -w" -o dist/backupctl-linux-amd64 ./cmd/backupctl
|
||||
```
|
||||
|
||||
CI (Gitea Actions):
|
||||
- `.gitea/workflows/ci.yml` на каждый push и PR проверяет форматирование, запускает vet и тесты и собирает бинарники;
|
||||
- `release.yml` на тег `v*` публикует релиз с бинарниками, `checksums.txt` и файлами из `deploy/`.
|
||||
|
||||
Новый релиз:
|
||||
|
||||
```bash
|
||||
git tag v0.2.0 && git push origin v0.2.0
|
||||
```
|
||||
|
||||
## Хранение
|
||||
|
||||
Бэкапы лежат в `/var/lib/backup-master/backups/<домен>/<дата>-<id>/`: `main.tar.zst`, heavy-части и `manifest.json`. Архив — обычный tar+zstd, его можно разобрать и без backupctl:
|
||||
|
||||
@@ -296,7 +296,7 @@ func (s *Server) apiComplete(w http.ResponseWriter, r *http.Request) {
|
||||
if len(m.Warnings) > 0 {
|
||||
msg += fmt.Sprintf(", предупреждений %d", len(m.Warnings))
|
||||
}
|
||||
s.notify("Бэкап готов: "+c.Domain, msg, "low", "white_check_mark")
|
||||
s.notify(notification{Title: "Бэкап готов: " + c.Domain, Msg: msg, Priority: "low", Tags: "white_check_mark", Link: s.clientLink(c.ID)})
|
||||
}
|
||||
if c2, err := s.st.GetClient(c.ID); err == nil {
|
||||
if err := s.applyRetention(c2); err != nil {
|
||||
|
||||
+181
-21
@@ -1,11 +1,17 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
"log"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -16,36 +22,100 @@ const (
|
||||
settingNtfyURL = "ntfy_url"
|
||||
settingNtfyAuth = "ntfy_auth" // токен tk_... или "user:password"
|
||||
settingNotifySuccess = "notify_success"
|
||||
|
||||
settingTGToken = "tg_token"
|
||||
settingTGChats = "tg_chats" // chat_id через запятую
|
||||
settingTGThread = "tg_thread" // id темы в группе-форуме, необязательно
|
||||
settingTGAPIURL = "tg_api_url" // свой Bot API сервер или прокси, по умолчанию api.telegram.org
|
||||
)
|
||||
|
||||
// notify отправляет уведомление в ntfy (асинхронно, ошибки только в лог).
|
||||
func (s *Server) notify(title, msg, priority, tags string) {
|
||||
url := s.st.Setting(settingNtfyURL)
|
||||
if url == "" {
|
||||
return
|
||||
}
|
||||
auth := s.st.Setting(settingNtfyAuth)
|
||||
go func() {
|
||||
if err := sendNtfy(url, auth, title, msg, priority, tags); err != nil {
|
||||
log.Printf("ntfy: %v", err)
|
||||
}
|
||||
}()
|
||||
const defaultTGAPI = "https://api.telegram.org"
|
||||
|
||||
type notification struct {
|
||||
Title string
|
||||
Msg string
|
||||
Priority string // low, default, high — как в ntfy
|
||||
Tags string // теги ntfy, они же выбирают эмодзи в Telegram
|
||||
Link string // ссылка на страницу в морде
|
||||
}
|
||||
|
||||
func sendNtfy(url, auth, title, msg, priority, tags string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(msg))
|
||||
// channel — канал доставки уведомлений.
|
||||
type channel struct {
|
||||
name string
|
||||
send func(context.Context, notification) error
|
||||
}
|
||||
|
||||
// channels возвращает настроенные каналы (ntfy, Telegram). Пустой список — уведомления выключены.
|
||||
func (s *Server) channels() []channel {
|
||||
var out []channel
|
||||
if url := s.st.Setting(settingNtfyURL); url != "" {
|
||||
auth := s.st.Setting(settingNtfyAuth)
|
||||
out = append(out, channel{"ntfy", func(ctx context.Context, n notification) error {
|
||||
return sendNtfy(ctx, url, auth, n)
|
||||
}})
|
||||
}
|
||||
token, chats := s.st.Setting(settingTGToken), s.st.Setting(settingTGChats)
|
||||
if token != "" && chats != "" {
|
||||
tg := telegram{
|
||||
api: strings.TrimRight(s.st.Setting(settingTGAPIURL), "/"),
|
||||
token: token,
|
||||
chats: splitList(chats),
|
||||
thread: s.st.Setting(settingTGThread),
|
||||
}
|
||||
if tg.api == "" {
|
||||
tg.api = defaultTGAPI
|
||||
}
|
||||
out = append(out, channel{"telegram", tg.send})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// notify рассылает уведомление во все каналы асинхронно, ошибки — в лог.
|
||||
func (s *Server) notify(n notification) {
|
||||
for _, ch := range s.channels() {
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
if err := ch.send(ctx, n); err != nil {
|
||||
log.Printf("уведомление %s: %v", ch.name, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// notifySync рассылает уведомление и возвращает результат по каждому каналу (для теста в настройках).
|
||||
func (s *Server) notifySync(ctx context.Context, n notification) map[string]error {
|
||||
res := map[string]error{}
|
||||
for _, ch := range s.channels() {
|
||||
cctx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
res[ch.name] = ch.send(cctx, n)
|
||||
cancel()
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (s *Server) clientLink(id int64) string {
|
||||
if s.cfg.PublicURL == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%s/clients/%d", s.cfg.PublicURL, id)
|
||||
}
|
||||
|
||||
func sendNtfy(ctx context.Context, url, auth string, n notification) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(n.Msg))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// заголовки должны быть ASCII — кириллицу кодируем по RFC 2047, ntfy это понимает
|
||||
req.Header.Set("Title", mime.BEncoding.Encode("UTF-8", title))
|
||||
if priority != "" {
|
||||
req.Header.Set("Priority", priority)
|
||||
req.Header.Set("Title", mime.BEncoding.Encode("UTF-8", n.Title))
|
||||
if n.Priority != "" {
|
||||
req.Header.Set("Priority", n.Priority)
|
||||
}
|
||||
if tags != "" {
|
||||
req.Header.Set("Tags", tags)
|
||||
if n.Tags != "" {
|
||||
req.Header.Set("Tags", n.Tags)
|
||||
}
|
||||
if n.Link != "" {
|
||||
req.Header.Set("Click", n.Link)
|
||||
}
|
||||
switch {
|
||||
case auth == "":
|
||||
@@ -66,6 +136,96 @@ func sendNtfy(url, auth, title, msg, priority, tags string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type telegram struct {
|
||||
api string
|
||||
token string
|
||||
chats []string
|
||||
thread string
|
||||
}
|
||||
|
||||
var tgEmoji = map[string]string{
|
||||
"white_check_mark": "✅",
|
||||
"x": "❌",
|
||||
"warning": "⚠️",
|
||||
"test_tube": "🧪",
|
||||
}
|
||||
|
||||
func (t telegram) text(n notification) string {
|
||||
var b strings.Builder
|
||||
if e := tgEmoji[n.Tags]; e != "" {
|
||||
b.WriteString(e + " ")
|
||||
}
|
||||
b.WriteString("<b>" + html.EscapeString(n.Title) + "</b>")
|
||||
if n.Msg != "" {
|
||||
b.WriteString("\n" + html.EscapeString(n.Msg))
|
||||
}
|
||||
if n.Link != "" {
|
||||
b.WriteString("\n<a href=\"" + html.EscapeString(n.Link) + "\">Открыть в backup-master</a>")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (t telegram) send(ctx context.Context, n notification) error {
|
||||
var errs []error
|
||||
for _, chat := range t.chats {
|
||||
if err := t.sendOne(ctx, chat, n); err != nil {
|
||||
errs = append(errs, fmt.Errorf("чат %s: %w", chat, err))
|
||||
}
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (t telegram) sendOne(ctx context.Context, chat string, n notification) error {
|
||||
body := map[string]any{
|
||||
"chat_id": chat,
|
||||
"text": t.text(n),
|
||||
"parse_mode": "HTML",
|
||||
"disable_web_page_preview": true,
|
||||
// успешные бэкапы — без звука, ошибки — со звуком
|
||||
"disable_notification": n.Priority == "low",
|
||||
}
|
||||
if t.thread != "" {
|
||||
if id, err := strconv.ParseInt(t.thread, 10, 64); err == nil {
|
||||
body["message_thread_id"] = id
|
||||
}
|
||||
}
|
||||
data, _ := json.Marshal(body)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.api+"/bot"+t.token+"/sendMessage", bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
// в тексте ошибки net/http есть URL с токеном — не выносим его в лог
|
||||
return errors.New(strings.ReplaceAll(err.Error(), t.token, "***"))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var r struct {
|
||||
OK bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
|
||||
_ = json.Unmarshal(raw, &r)
|
||||
if !r.OK {
|
||||
if r.Description == "" {
|
||||
r.Description = resp.Status
|
||||
}
|
||||
return errors.New(r.Description)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func splitList(s string) []string {
|
||||
var out []string
|
||||
for _, p := range strings.FieldsFunc(s, func(r rune) bool { return r == ',' || r == ' ' || r == '\n' || r == ';' }) {
|
||||
if p = strings.TrimSpace(p); p != "" {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func humanBytes(n int64) string {
|
||||
const unit = 1024
|
||||
if n < unit {
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTelegramSend(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var got []map[string]any
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/botTOKEN/sendMessage" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
mu.Lock()
|
||||
got = append(got, body)
|
||||
mu.Unlock()
|
||||
if body["chat_id"] == "bad" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(`{"ok":false,"description":"Bad Request: chat not found"}`))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{"ok":true}`))
|
||||
}))
|
||||
defer api.Close()
|
||||
|
||||
tg := telegram{api: api.URL, token: "TOKEN", chats: []string{"1", "-1002"}, thread: "7"}
|
||||
n := notification{Title: "Бэкап готов: a<b>.example", Msg: "1 ГБ", Priority: "low", Tags: "white_check_mark", Link: "https://bm.test/clients/1"}
|
||||
if err := tg.send(context.Background(), n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("отправлено %d сообщений, ожидалось 2", len(got))
|
||||
}
|
||||
m := got[0]
|
||||
text, _ := m["text"].(string)
|
||||
if !strings.HasPrefix(text, "✅ <b>Бэкап готов: a<b>.example</b>") || !strings.Contains(text, `href="https://bm.test/clients/1"`) {
|
||||
t.Errorf("текст: %q", text)
|
||||
}
|
||||
if m["parse_mode"] != "HTML" || m["disable_notification"] != true || m["message_thread_id"] != float64(7) {
|
||||
t.Errorf("параметры: %v", m)
|
||||
}
|
||||
|
||||
tg.chats = []string{"bad"}
|
||||
err := tg.send(context.Background(), notification{Title: "x", Priority: "high"})
|
||||
if err == nil || !strings.Contains(err.Error(), "chat not found") {
|
||||
t.Fatalf("ошибка: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelsFromSettings(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
if len(s.channels()) != 0 {
|
||||
t.Fatal("без настроек каналов быть не должно")
|
||||
}
|
||||
_ = s.st.SetSetting(settingTGToken, "T")
|
||||
if len(s.channels()) != 0 {
|
||||
t.Fatal("Telegram без chat id включаться не должен")
|
||||
}
|
||||
_ = s.st.SetSetting(settingTGChats, "1")
|
||||
_ = s.st.SetSetting(settingNtfyURL, "https://ntfy.test/x")
|
||||
if n := len(s.channels()); n != 2 {
|
||||
t.Fatalf("каналов %d, ожидалось 2", n)
|
||||
}
|
||||
if got := splitList("1, -100;2\n 3"); strings.Join(got, ",") != "1,-100,2,3" {
|
||||
t.Fatalf("splitList: %v", got)
|
||||
}
|
||||
}
|
||||
@@ -181,7 +181,7 @@ func (s *Server) tick() {
|
||||
if c.LastBackupAt == 0 {
|
||||
msg = fmt.Sprintf("%s: от клиента ещё не пришло ни одного бэкапа", c.Domain)
|
||||
}
|
||||
s.notify("Бэкап просрочен: "+c.Domain, msg, "high", "warning")
|
||||
s.notify(notification{Title: "Бэкап просрочен: " + c.Domain, Msg: msg, Priority: "high", Tags: "warning", Link: s.clientLink(c.ID)})
|
||||
_ = s.st.SetStaleNotified(c.ID)
|
||||
}
|
||||
}
|
||||
@@ -200,7 +200,7 @@ func (s *Server) failBackup(b *Backup, msg string) {
|
||||
domain = c.Domain
|
||||
}
|
||||
log.Printf("бэкап #%d (%s) провален: %s", b.ID, domain, msg)
|
||||
s.notify("Бэкап не удался: "+domain, msg, "high", "x")
|
||||
s.notify(notification{Title: "Бэкап не удался: " + domain, Msg: msg, Priority: "high", Tags: "x", Link: s.clientLink(b.ClientID)})
|
||||
}
|
||||
|
||||
// --- токены ---
|
||||
|
||||
@@ -76,6 +76,8 @@ td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-sp
|
||||
.badge.fail { background: var(--fail-bg); color: var(--fail); }
|
||||
.badge.off { background: var(--code-bg); color: var(--muted); }
|
||||
.ok-text { color: var(--ok); }
|
||||
.fail-text { color: var(--fail); }
|
||||
ul.results { list-style: none; padding: 0; margin: 0 0 16px; }
|
||||
|
||||
.facts { display: flex; flex-wrap: wrap; gap: 12px 32px; margin: 16px 0; }
|
||||
.facts dt { font-size: 12px; color: var(--muted); }
|
||||
|
||||
@@ -2,15 +2,39 @@
|
||||
{{define "content"}}
|
||||
<h1>Настройки</h1>
|
||||
{{if .Saved}}<p class="ok-text">Сохранено.</p>{{end}}
|
||||
{{if .Tested}}
|
||||
{{if .TestResults}}
|
||||
<ul class="results">
|
||||
{{range .TestResults}}
|
||||
<li>{{.Channel}}: {{if .Err}}<span class="fail-text">ошибка — {{.Err}}</span>{{else}}<span class="ok-text">отправлено</span>{{end}}</li>
|
||||
{{end}}
|
||||
</ul>
|
||||
{{else}}
|
||||
<p class="error">Ни один канал уведомлений не настроен.</p>
|
||||
{{end}}
|
||||
{{end}}
|
||||
<form method="post" action="/settings" class="card narrow">
|
||||
<input type="hidden" name="csrf" value="{{.CSRF}}">
|
||||
<h2>Уведомления ntfy</h2>
|
||||
|
||||
<h2>ntfy</h2>
|
||||
<label>URL топика <input name="ntfy_url" value="{{.NtfyURL}}" placeholder="https://ntfy.example.com/backups"></label>
|
||||
<label>Авторизация <input name="ntfy_auth" type="password" autocomplete="off" placeholder="{{if .NtfyAuthSet}}задана — оставьте пустым, чтобы не менять{{else}}tk_... или user:password{{end}}"></label>
|
||||
{{if .NtfyAuthSet}}<label class="check"><input type="checkbox" name="ntfy_auth_clear" value="1"> Удалить авторизацию</label>{{end}}
|
||||
<label class="check"><input type="checkbox" name="notify_success" value="1" {{if .NotifySuccess}}checked{{end}}> Сообщать и об успешных бэкапах</label>
|
||||
<p class="hint">Пустой URL — ntfy выключен.</p>
|
||||
|
||||
<h2>Telegram</h2>
|
||||
<label>Токен бота <input name="tg_token" type="password" autocomplete="off" placeholder="{{if .TGTokenSet}}задан — оставьте пустым, чтобы не менять{{else}}123456:ABC... от @BotFather{{end}}"></label>
|
||||
{{if .TGTokenSet}}<label class="check"><input type="checkbox" name="tg_token_clear" value="1"> Удалить токен (выключить Telegram)</label>{{end}}
|
||||
<label>Chat ID <input name="tg_chats" value="{{.TGChats}}" placeholder="123456789, -1001234567890"></label>
|
||||
<p class="hint">Можно несколько через запятую. Личный чат — ваш числовой id (напишите боту и откройте <code>https://api.telegram.org/bot<токен>/getUpdates</code>), группа — id вида <code>-100…</code>, бот должен быть её участником.</p>
|
||||
<label>ID темы (необязательно) <input name="tg_thread" value="{{.TGThread}}" placeholder="для группы с темами"></label>
|
||||
<label>Адрес Bot API (необязательно) <input name="tg_api_url" value="{{.TGAPIURL}}" placeholder="https://api.telegram.org"></label>
|
||||
<p class="hint">Свой Bot API сервер или прокси, если api.telegram.org недоступен с сервера бэкапов.</p>
|
||||
|
||||
<h2>Что отправлять</h2>
|
||||
<label class="check"><input type="checkbox" name="notify_success" value="1" {{if .NotifySuccess}}checked{{end}}> Сообщать и об успешных бэкапах (в Telegram — без звука)</label>
|
||||
<p class="hint">Ошибки бэкапа и отсутствие бэкапа дольше заданного у клиента срока приходят всегда, во все настроенные каналы.</p>
|
||||
<label class="check"><input type="checkbox" name="test" value="1"> Отправить тестовое уведомление после сохранения</label>
|
||||
<p class="hint">Ошибки бэкапа и отсутствие бэкапа дольше заданного у клиента срока приходят всегда.</p>
|
||||
<button class="primary">Сохранить</button>
|
||||
</form>
|
||||
<p class="hint">Логин и пароль администратора меняются на сервере: <code>backupctl admin-passwd <логин></code></p>
|
||||
|
||||
+49
-9
@@ -376,29 +376,69 @@ func (s *Server) webBackupDelete(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, fmt.Sprintf("/clients/%d", c.ID), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (s *Server) webSettings(w http.ResponseWriter, r *http.Request) {
|
||||
s.render(w, r, "settings", map[string]any{
|
||||
func (s *Server) settingsData(r *http.Request) map[string]any {
|
||||
return map[string]any{
|
||||
"NtfyURL": s.st.Setting(settingNtfyURL),
|
||||
"NtfyAuthSet": s.st.Setting(settingNtfyAuth) != "",
|
||||
"TGTokenSet": s.st.Setting(settingTGToken) != "",
|
||||
"TGChats": s.st.Setting(settingTGChats),
|
||||
"TGThread": s.st.Setting(settingTGThread),
|
||||
"TGAPIURL": s.st.Setting(settingTGAPIURL),
|
||||
"NotifySuccess": s.st.Setting(settingNotifySuccess) == "1",
|
||||
"Saved": r.URL.Query().Get("saved") != "",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) webSettings(w http.ResponseWriter, r *http.Request) {
|
||||
s.render(w, r, "settings", s.settingsData(r))
|
||||
}
|
||||
|
||||
// setSecret обновляет секретную настройку: пустое поле — не менять, галочка clear — удалить.
|
||||
func (s *Server) setSecret(r *http.Request, key, field string) {
|
||||
if r.FormValue(field+"_clear") == "1" {
|
||||
_ = s.st.SetSetting(key, "")
|
||||
} else if v := strings.TrimSpace(r.FormValue(field)); v != "" {
|
||||
_ = s.st.SetSetting(key, v)
|
||||
}
|
||||
}
|
||||
|
||||
type testResult struct {
|
||||
Channel string
|
||||
Err string
|
||||
}
|
||||
|
||||
func (s *Server) webSettingsSave(w http.ResponseWriter, r *http.Request) {
|
||||
_ = s.st.SetSetting(settingNtfyURL, strings.TrimSpace(r.FormValue("ntfy_url")))
|
||||
if r.FormValue("ntfy_auth_clear") == "1" {
|
||||
_ = s.st.SetSetting(settingNtfyAuth, "")
|
||||
} else if v := strings.TrimSpace(r.FormValue("ntfy_auth")); v != "" {
|
||||
_ = s.st.SetSetting(settingNtfyAuth, v)
|
||||
}
|
||||
s.setSecret(r, settingNtfyAuth, "ntfy_auth")
|
||||
s.setSecret(r, settingTGToken, "tg_token")
|
||||
_ = s.st.SetSetting(settingTGChats, strings.Join(splitList(r.FormValue("tg_chats")), ","))
|
||||
_ = s.st.SetSetting(settingTGThread, strings.TrimSpace(r.FormValue("tg_thread")))
|
||||
_ = s.st.SetSetting(settingTGAPIURL, strings.TrimSpace(r.FormValue("tg_api_url")))
|
||||
ns := "0"
|
||||
if r.FormValue("notify_success") == "1" {
|
||||
ns = "1"
|
||||
}
|
||||
_ = s.st.SetSetting(settingNotifySuccess, ns)
|
||||
if r.FormValue("test") == "1" {
|
||||
s.notify("backup-master: тест", "Тестовое уведомление", "default", "test_tube")
|
||||
res := s.notifySync(r.Context(), notification{
|
||||
Title: "backup-master: тест", Msg: "Тестовое уведомление", Priority: "default", Tags: "test_tube", Link: s.cfg.PublicURL,
|
||||
})
|
||||
var results []testResult
|
||||
for _, name := range []string{"ntfy", "telegram"} {
|
||||
if err, ok := res[name]; ok {
|
||||
tr := testResult{Channel: name}
|
||||
if err != nil {
|
||||
tr.Err = err.Error()
|
||||
}
|
||||
results = append(results, tr)
|
||||
}
|
||||
}
|
||||
data := s.settingsData(r)
|
||||
data["Saved"] = true
|
||||
data["Tested"] = true
|
||||
data["TestResults"] = results
|
||||
s.render(w, r, "settings", data)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/settings?saved=1", http.StatusSeeOther)
|
||||
}
|
||||
Reference in new issue
Block a user