Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Resticara is a wrapper around [Restic](https://restic.net/), designed to simplif
* Simplified Configuration: Uses config.ini for easy setup and configuration.
* Syslog Integration: Logging to syslog is enabled by default for better traceability.
* Email Notifications: Can be configured to send emails upon backup completion or failure.
* Matrix Notifications: Send backup status messages to a Matrix room.
* Single Binary: Written in Go, Resticara is distributed as a single binary—making it extremely easy to deploy.
* Systemd Timer Generation: Create and activate systemd timers with `resticara gentimer`.

Expand Down
7 changes: 7 additions & 0 deletions config.ini-dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ enabled = false
;server = "mail.example.com"
;port = "587"

[matrix]
enabled = false
;server = "https://matrix.example.com"
;username = "@bot:example.com"
;pass = "#################"
;room_id = "!abcdefg:example.com"

[dir:website]
bucket = b2:bucket:wpsites/
directory = /var/www
Expand Down
3 changes: 0 additions & 3 deletions emailsender/go.mod

This file was deleted.

4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module resticara
go 1.21

require (
github.com/yourusername/resticara/emailsender v0.0.0-00010101000000-000000000000
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530
gopkg.in/ini.v1 v1.67.0
)

require github.com/stretchr/testify v1.8.4 // indirect

replace github.com/yourusername/resticara/emailsender => ./emailsender
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U=
github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
37 changes: 35 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
"text/template"
"time"

"github.com/yourusername/resticara/emailsender"
"gopkg.in/ini.v1"
"resticara/notificators/emailsender"
"resticara/notificators/matrixsender"
)

type CommandInfo struct {
Expand Down Expand Up @@ -59,6 +60,11 @@ type Config struct {
HostID string
RetentionPrune int
SMTPEnabled bool
MatrixEnabled bool
MatrixServer string
MatrixUser string
MatrixPass string
MatrixRoomID string
Commands map[string]map[string]string
}

Expand Down Expand Up @@ -86,13 +92,20 @@ func readConfig(file string) (Config, error) {
config.To = cfg.Section("smtp").Key("to").String()
config.SMTPServer = cfg.Section("smtp").Key("server").String()
config.SMTPPort = cfg.Section("smtp").Key("port").String()

config.MatrixEnabled = cfg.Section("matrix").Key("enabled").MustBool(false)
config.MatrixServer = cfg.Section("matrix").Key("server").String()
config.MatrixUser = cfg.Section("matrix").Key("username").String()
config.MatrixPass = cfg.Section("matrix").Key("pass").String()
config.MatrixRoomID = cfg.Section("matrix").Key("room_id").String()

config.HostID = cfg.Section("general").Key("hostID").String()
config.RetentionPrune = cfg.Section("general").Key("retention_prune").MustInt(30)

config.Commands = make(map[string]map[string]string)

for _, section := range cfg.Sections() {
if section.Name() == "smtp" {
if section.Name() == "smtp" || section.Name() == "matrix" {
continue
}

Expand Down Expand Up @@ -567,6 +580,26 @@ func main() {
} else {
fmt.Println("SMTP is disabled, not sending email.")
}

if config.MatrixEnabled {
matrixSender := matrixsender.GomatrixSender{}
message := mailData.StatusMessage + "---" + time.Now().Format(time.RFC1123) + "\n\n" + mailMessageBuffer.String()
matrixConfig := matrixsender.MatrixConfig{
Homeserver: config.MatrixServer,
Username: config.MatrixUser,
Password: config.MatrixPass,
RoomID: config.MatrixRoomID,
Message: message,
}

if err := matrixSender.Send(matrixConfig); err != nil {
fmt.Println(err)
} else {
fmt.Println("Matrix message sent!")
}
} else {
fmt.Println("Matrix is disabled, not sending message.")
}
case "prune":
if len(args) < 2 {
fmt.Println("Usage: resticara prune <all|repository>")
Expand Down
File renamed without changes.
48 changes: 48 additions & 0 deletions notificators/matrixsender/matrixsender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package matrixsender

import (
"fmt"

"github.com/matrix-org/gomatrix"
)

type MatrixConfig struct {
Homeserver string
Username string
Password string
RoomID string
Message string
}

type MatrixSender interface {
Send(cfg MatrixConfig) error
}

type GomatrixSender struct{}

func (s GomatrixSender) Send(cfg MatrixConfig) error {
cli, err := gomatrix.NewClient(cfg.Homeserver, "", "")
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}

resp, err := cli.Login(&gomatrix.ReqLogin{
Type: "m.login.password",
User: cfg.Username,
Password: cfg.Password,
})
if err != nil {
return fmt.Errorf("failed to login: %w", err)
}

cli.SetCredentials(resp.UserID, resp.AccessToken)

if _, err := cli.JoinRoom(cfg.RoomID, "", nil); err != nil {
// attempt to continue even if already joined
}

if _, err := cli.SendText(cfg.RoomID, cfg.Message); err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
return nil
}
Loading