forked from a2call/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
69 lines (65 loc) · 2.45 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package db
import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/daticahealth/cli/commands/services"
"github.com/daticahealth/cli/lib/jobs"
"github.com/daticahealth/cli/models"
)
func CmdBackup(databaseName string, skipPoll bool, id IDb, is services.IServices, ij jobs.IJobs) error {
service, err := is.RetrieveByLabel(databaseName)
if err != nil {
return err
}
if service == nil {
return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"datica services list\" command.", databaseName)
}
job, err := id.Backup(service)
if err != nil {
return err
}
logrus.Printf("Backup started (job ID = %s)", job.ID)
isSnapshotBackup := job.IsSnapshotBackup != nil && *job.IsSnapshotBackup
if !skipPoll {
// all because logrus treats print, println, and printf the same
logrus.StandardLogger().Out.Write([]byte("Polling until backup finishes."))
if isSnapshotBackup {
logrus.StandardLogger().Out.Write([]byte(fmt.Sprintf("\nThis is a snapshot backup, it may be a while before this backup shows up in the \"datica db list %s\" command.", databaseName)))
err = ij.WaitToAppear(job.ID, service.ID)
if err != nil {
return err
}
}
status, err := ij.PollTillFinished(job.ID, service.ID)
if err != nil {
return err
}
job.Status = status
logrus.Printf("\nEnded in status '%s'", job.Status)
err = id.DumpLogs("backup", job, service)
if err != nil {
return err
}
if job.Status != "finished" {
return fmt.Errorf("Job finished with invalid status %s", job.Status)
}
} else if isSnapshotBackup {
logrus.Printf("This is a snapshot backup, it may be a while before this backup shows up in the \"datica db list %s\" command.", databaseName)
}
logrus.Printf("You can download your backup with the \"datica db download %s %s ./output_file_path\" command", databaseName, job.ID)
return nil
}
// Backup creates a new backup
func (d *SDb) Backup(service *models.Service) (*models.Job, error) {
headers := d.Settings.HTTPManager.GetHeaders(d.Settings.SessionToken, d.Settings.Version, d.Settings.Pod, d.Settings.UsersID)
resp, statusCode, err := d.Settings.HTTPManager.Post(nil, fmt.Sprintf("%s%s/environments/%s/services/%s/backup", d.Settings.PaasHost, d.Settings.PaasHostVersion, d.Settings.EnvironmentID, service.ID), headers)
if err != nil {
return nil, err
}
var job models.Job
err = d.Settings.HTTPManager.ConvertResp(resp, statusCode, &job)
if err != nil {
return nil, err
}
return &job, nil
}