Skip to content
Open
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
33 changes: 33 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion cmd/backup/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ db:
port: 5432
database: postgres
user: postgres

log:
development: false
level: info
location: true
14 changes: 11 additions & 3 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"runtime"

"github.com/ikitiki/logical_backup/pkg/config"
"github.com/ikitiki/logical_backup/pkg/logger"
"github.com/ikitiki/logical_backup/pkg/logicalbackup"
)

var (
configFile = flag.String("config", "config.yaml", "path to the config file")
version = flag.Bool("version", false, "Print version information")
configFile = flag.String("config", "config.yaml", "path to the config file")
version = flag.Bool("version", false, "Print version information")
logDevelopment = flag.Bool("log-development", false, "enable development logging mode")

Version string
Revision string
Expand Down Expand Up @@ -43,12 +45,18 @@ func main() {
os.Exit(1)
}

cfg, err := config.New(*configFile)
cfg, err := config.New(*configFile, *logDevelopment)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not load config file: %v", err)
os.Exit(1)
}

// Initialize the logger after we've resolved the debug flag but before its first usage at cfg.Print
if err := logger.InitGlobalLogger(cfg.Log); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Could not initialize global logger")
os.Exit(1)
}

cfg.Print()

lb, err := logicalbackup.New(cfg)
Expand Down
30 changes: 29 additions & 1 deletion cmd/restore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/jackc/pgx"

"github.com/ikitiki/logical_backup/pkg/logger"
"github.com/ikitiki/logical_backup/pkg/logicalrestore"
"github.com/ikitiki/logical_backup/pkg/message"
)
Expand All @@ -15,6 +16,8 @@ var (
pgUser, pgPass, pgHost, pgDbname *string
targetTable, backupDir, tableName, schemaName *string
pgPort *uint
logDevelopment, logShowLocation *bool
logLevel *string
)

func init() {
Expand All @@ -29,6 +32,9 @@ func init() {
schemaName = flag.String("schema", "public", "Schema name")
targetTable = flag.String("target-table", "", "Target table name (optional)")
backupDir = flag.String("backup-dir", "", "Backups dir")
logDevelopment = flag.Bool("log-development", false, "Enable development logging mode")
logLevel = flag.String("log-level", "", "Set log level")
logShowLocation = flag.Bool("log-location", true, "Show log location")

flag.Parse()

Expand All @@ -38,7 +44,30 @@ func init() {
}
}

func makeLoggerConfig() *logger.LoggerConfig {
lc := logger.DefaultLogConfig()
lc.Development = *logDevelopment
lc.Location = logShowLocation

if *logLevel != "" {
if err := logger.ValidateLogLevel(*logLevel); err != nil {
log.Fatal(err)
}
lc.Level = *logLevel
}
return lc
}

func main() {

tbl := message.NamespacedName{Namespace: *schemaName, Name: *tableName}

lc := makeLoggerConfig()
if err := logger.InitGlobalLogger(lc, "table to restore", tbl.String()); err != nil {
log.Fatal("Could not initialize global logger: %v", err)
os.Exit(1)
}

if *targetTable != "" {
log.Printf("restoring into %v", targetTable)
}
Expand All @@ -58,7 +87,6 @@ func main() {
}
config = config.Merge(envConfig)

tbl := message.NamespacedName{Namespace: *schemaName, Name: *tableName}
r := logicalrestore.New(tbl, *backupDir, config)

if err := r.Restore(); err != nil {
Expand Down
53 changes: 38 additions & 15 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,36 @@ import (

"github.com/jackc/pgx"
"gopkg.in/yaml.v2"

"github.com/ikitiki/logical_backup/pkg/logger"
)

const (
defaultPrometheusPort = 1999
)

type Config struct {
DB pgx.ConnConfig `yaml:"db"`
SlotName string `yaml:"slotname"`
PublicationName string `yaml:"publication"`
TrackNewTables bool `yaml:"trackNewTables"`
DeltasPerFile int `yaml:"deltasPerFile"`
BackupThreshold int `yaml:"backupThreshold"`
ConcurrentBasebackups int `yaml:"concurrentBasebackups"`
InitialBasebackup bool `yaml:"initialBasebackup"`
Fsync bool `yaml:"fsync"`
StagingDir string `yaml:"StagingDir"`
ArchiveDir string `yaml:"archiveDir"`
ForceBasebackupAfterInactivityInterval time.Duration `yaml:"forceBasebackupAfterInactivityInterval"`
ArchiverTimeout time.Duration `yaml:"archiverTimeout"`
PrometheusPort int `yaml:"prometheusPort"`
DB pgx.ConnConfig `yaml:"db"`
SlotName string `yaml:"slotname"`
PublicationName string `yaml:"publication"`
TrackNewTables bool `yaml:"trackNewTables"`
DeltasPerFile int `yaml:"deltasPerFile"`
BackupThreshold int `yaml:"backupThreshold"`
ConcurrentBasebackups int `yaml:"concurrentBasebackups"`
InitialBasebackup bool `yaml:"initialBasebackup"`
Fsync bool `yaml:"fsync"`
StagingDir string `yaml:"StagingDir"`
ArchiveDir string `yaml:"archiveDir"`
ForceBasebackupAfterInactivityInterval time.Duration `yaml:"forceBasebackupAfterInactivityInterval"`
ArchiverTimeout time.Duration `yaml:"archiverTimeout"`
PrometheusPort int `yaml:"prometheusPort"`
Log *logger.LoggerConfig `yaml:"log"`
}

func New(filename string) (*Config, error) {
// New returns a new config structure, filled-in from the passed filename.
// developmentMode enables development mode logging, overriding file-based
// settings.
func New(filename string, developmentMode bool) (*Config, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported identifier "New" should have comment

Suggested change
func New(filename string, developmentMode bool) (*Config, error) {
// New ...
func New(filename string, developmentMode bool) (*Config, error) {

var cfg Config

fp, err := os.Open(filename)
Expand Down Expand Up @@ -62,9 +68,26 @@ func New(filename string) (*Config, error) {
cfg.PrometheusPort = defaultPrometheusPort
}

defaultLoggerConfig := logger.DefaultLogConfig()
if cfg.Log == nil {
cfg.Log = defaultLoggerConfig
}
if cfg.Log.Location == nil {
cfg.Log.Location = defaultLoggerConfig.Location
}

if developmentMode {
cfg.Log.Development = developmentMode
}

if err := logger.ValidateLogLevel(cfg.Log.Level); err != nil {
return nil, err
}

return &cfg, nil
}

// Print outputs the actual configuration.
func (c Config) Print() {
if c.StagingDir != "" {
log.Printf("Staging directory: %q", c.StagingDir)
Expand Down
Loading