Skip to content

Commit

Permalink
feat(backup/source/postgres): Add source PostgresServer (pg_dumpall)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Apr 13, 2022
1 parent d33d8c0 commit 1ec62a3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
67 changes: 67 additions & 0 deletions backup/source/postgres_server/postgres_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package postgres_server

import (
"fmt"
"io"
"os"
"os/exec"
)

type PostgresServerSource struct {
Host string `yaml:"Host"`
Port string `yaml:"Port"`
User string `yaml:"User"`
Password string `yaml:"Password"`
}

func (s PostgresServerSource) Validate() error {
if s.Host == "" {
return fmt.Errorf("PostgresServerSource need to have a Host")
}
if s.Port == "" {
return fmt.Errorf("PostgresServerSource requires Port")
}
if s.User == "" {
return fmt.Errorf("PostgresServerSource requires User")
}
if s.Password == "" {
return fmt.Errorf("PostgresServerSource requires Password")
}
return nil
}

func (s PostgresServerSource) Backup() (io.ReadSeeker, error) {
var err error

outputFile, err := os.CreateTemp("", "tergum-dump-postgres-")
if err != nil {
return nil, err
}
defer os.Remove(outputFile.Name())

cmd := exec.Command(
"pg_dumpall",
"--host", s.Host,
"--port", s.Port,
"--user", s.User,
"--no-password",
)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "PGPASSWORD="+s.Password)
cmd.Stdout = outputFile
cmd.Stderr = os.Stderr

err = cmd.Start()
if err != nil {
return nil, err
}

err = cmd.Wait()
if err != nil {
fmt.Println(err)
return nil, err
}

outputFile.Seek(0, 0)
return outputFile, nil
}
16 changes: 16 additions & 0 deletions backup/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
"github.com/sikalabs/tergum/backup/source/mysql_server"
"github.com/sikalabs/tergum/backup/source/notion"
"github.com/sikalabs/tergum/backup/source/postgres"
"github.com/sikalabs/tergum/backup/source/postgres_server"
"github.com/sikalabs/tergum/backup/source/single_file"
)

type Source struct {
Mysql *mysql.MysqlSource `yaml:"Mysql"`
MysqlServer *mysql_server.MysqlServerSource `yaml:"MysqlServer"`
Postgres *postgres.PostgresSource `yaml:"Postgres"`
PostgresServer *postgres_server.PostgresServerSource `yaml:"PostgresServer"`
Mongo *mongo.MongoSource `yaml:"Mongo"`
SingleFile *single_file.SingleFileSource `yaml:"SingleFile"`
KubernetesTLSSecret *kubernetes_tls_secret.KubernetesTLSSecret `yaml:"KubernetesTLSSecret"`
Expand All @@ -43,6 +45,11 @@ func (s Source) Validate() error {
return p.Validate()
}

if s.PostgresServer != nil {
p := *s.PostgresServer
return p.Validate()
}

if s.Mongo != nil {
p := *s.Mongo
return p.Validate()
Expand Down Expand Up @@ -92,6 +99,11 @@ func (s Source) Backup() (io.ReadSeeker, error) {
return p.Backup()
}

if s.PostgresServer != nil {
p := *s.PostgresServer
return p.Backup()
}

if s.Mongo != nil {
p := *s.Mongo
return p.Backup()
Expand Down Expand Up @@ -138,6 +150,10 @@ func (s Source) Name() string {
return "Postgres"
}

if s.PostgresServer != nil {
return "PostgresServer"
}

if s.Mongo != nil {
return "Mongo"
}
Expand Down

0 comments on commit 1ec62a3

Please sign in to comment.