Skip to content

Commit

Permalink
add support for running backman as a CF task
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesClonk committed Feb 23, 2020
1 parent 8201b7a commit 8e21111
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ a backup-manager app for [Cloud Foundry](https://www.cloudfoundry.org/)
5. deploy the app
6. enjoy!

#### Using Cloud Foundry tasks

backman also supports running as a one-off task inside Cloud Foundry. Simply push the app as normal, stop it, and then run it via `cf run-task` with `./backman -backup <service_name>` as task command.

## Configuration

backman can be configured via JSON configuration, either with a file `config.json` in it's root directory, or by the environment variable `BACKMAN_CONFIG`.
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
DisableMetrics bool `json:"disable_metrics"`
S3 S3Config
Services map[string]ServiceConfig
Foreground bool
}

type S3Config struct {
Expand Down
48 changes: 48 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"flag"

"github.com/swisscom/backman/config"
"github.com/swisscom/backman/log"
"github.com/swisscom/backman/router"
"github.com/swisscom/backman/scheduler"
Expand All @@ -9,13 +12,58 @@ import (

//go:generate swagger generate spec
func main() {
log.Infoln("starting up backman ...")

// init services
service.Get()

// check if an immediate backup should run / non-background mode
if runBackupNow() {
return
}

// schedule backups
scheduler.RegisterBackups()

// serve API & UI
r := router.New()
log.Fatalf("%v", r.Start())
}

func runBackupNow() bool {
serviceToBackup := flag.String("backup", "", "service to backup now")
flag.Parse()

if len(*serviceToBackup) > 0 {
log.Infof("backup flag provided with value [%s], running backup now", *serviceToBackup)

// setting config to non-background mode, to avoid background goroutines during backups
config.Get().Foreground = true

// find service to backup
var found bool
for _, s := range service.Get().Services {
if s.Name == *serviceToBackup {
// running backup
log.Infof("running service backup for [%s/%s]", s.Label, s.Name)
if err := service.Get().Backup(s); err != nil {
log.Fatalf("service backup failed: %v", err)
}
found = true

// running S3 cleanup
if err := service.Get().RetentionCleanup(s); err != nil {
log.Errorf("could not cleanup S3 storage for service [%s]: %v", s.Name, err)
}
break
}
}
if !found {
log.Fatalf("could not find any service named [%s]", *serviceToBackup)
}

log.Infoln("backup successfully completed")
return true
}
return false
}
2 changes: 1 addition & 1 deletion manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ applications:

# ### push either as docker image
docker:
image: jamesclonk/backman:1.10.0 # choose version from https://hub.docker.com/r/jamesclonk/backman/tags, or 'latest'
image: jamesclonk/backman:1.11.0 # choose version from https://hub.docker.com/r/jamesclonk/backman/tags, or 'latest'
# ### or as buildpack/src
# buildpacks:
# - https://github.com/cloudfoundry/apt-buildpack
Expand Down
20 changes: 12 additions & 8 deletions service/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/swisscom/backman/config"
"github.com/swisscom/backman/log"
"github.com/swisscom/backman/service/elasticsearch"
"github.com/swisscom/backman/service/mongodb"
Expand Down Expand Up @@ -86,15 +87,18 @@ func (s *Service) Backup(service util.Service) error {
}
log.Infof("created and uploaded backup [%s] for service [%s]", filename, service.Name)

// cleanup files according to retention policy of service
go func() {
if err := s.RetentionCleanup(service); err != nil {
log.Errorf("could not cleanup S3 storage for service [%s]: %v", service.Name, err)
}
// only run background goroutines if not in non-background mode
if !config.Get().Foreground {
// cleanup files according to retention policy of service
go func() {
if err := s.RetentionCleanup(service); err != nil {
log.Errorf("could not cleanup S3 storage for service [%s]: %v", service.Name, err)
}

// update backup files state & metrics
_, _ = s.GetBackups(service.Label, service.Name)
}()
// update backup files state & metrics
_, _ = s.GetBackups(service.Label, service.Name)
}()
}
return err
}

Expand Down
22 changes: 13 additions & 9 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,22 @@ func (s *Service) parseServices() {
},
}
s.Services = append(s.Services, newService)

// init prometheus state metrics to 0
state.BackupInit(newService)
state.RestoreInit(newService)

// init backup files state & metrics
go func(label, name string) {
_, _ = s.GetBackups(label, name)
}(service.Label, service.Name)
}
}
}

// setup service metrics
for _, service := range s.Services {
// init prometheus state metrics to 0
state.BackupInit(service)
state.RestoreInit(service)

// init backup files state & metrics in background
go func(label, name string) {
_, _ = s.GetBackups(label, name)
}(service.Label, service.Name)
}

log.Debugf("services loaded: %+v", s.Services)
}

Expand Down

0 comments on commit 8e21111

Please sign in to comment.