Skip to content

Commit

Permalink
get backup position
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielZhangQD committed Dec 19, 2019
1 parent eca87b9 commit c17f89d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
37 changes: 32 additions & 5 deletions cmd/backup-manager/app/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ func (bo *BackupOpts) backupDataToRemote(source, bucketURI string) error {
return nil
}

func (bo *BackupOpts) brBackupData(backup *v1alpha1.Backup) (string, error) {
func (bo *BackupOpts) brBackupData(backup *v1alpha1.Backup) (string, string, error) {
args, path, err := constructBROptions(backup)
if err != nil {
return "", err
return "", "", err
}
fullArgs := []string{
"backup",
Expand All @@ -139,10 +139,10 @@ func (bo *BackupOpts) brBackupData(backup *v1alpha1.Backup) (string, error) {
fullArgs = append(fullArgs, args...)
output, err := exec.Command("br", fullArgs...).CombinedOutput()
if err != nil {
return path, fmt.Errorf("cluster %s, execute br command %v failed, output: %s, err: %v", bo, args, string(output), err)
return path, "", fmt.Errorf("cluster %s, execute br command %v failed, output: %s, err: %v", bo, args, string(output), err)
}
glog.Infof("backup data for cluster %s successfully, output: %s", bo, string(output))
return path, nil
glog.Infof("Backup data for cluster %s successfully, output: %s", bo, string(output))
return path, string(output), nil
}

// cleanBRRemoteBackupData clean the backup data from remote
Expand Down Expand Up @@ -260,6 +260,33 @@ func archiveBackupData(backupDir, destFile string) error {
return nil
}

// getBRCommitTs get backup position from the log output of BR
// It really depends on the format in BR log,
// currently, it's in the format of [BackupTS=412992336769581057]
func getBRCommitTs(out string) (string, error) {
var commitTs string
for _, lineStr := range strings.Split(out, "\n") {
if !strings.Contains(lineStr, "BackupTS=") {
continue
}
lineStrSlice := strings.Split(lineStr, " ")
for _, s := range lineStrSlice {
if !strings.Contains(s, "BackupTS=") {
continue
}
kv := strings.Split(s, "=")
if len(kv) != 2 {
return commitTs, fmt.Errorf("get pos from %s failed", lineStr)
}
cs := strings.TrimSpace(kv[1])
commitTs = strings.TrimSuffix(cs, "]")
break
}

}
return commitTs, nil
}

// constructBROptions constructs options for BR and also return the remote path
func constructBROptions(backup *v1alpha1.Backup) ([]string, string, error) {
args, path, err := util.ConstructBRGlobalOptions(backup)
Expand Down
28 changes: 13 additions & 15 deletions cmd/backup-manager/app/backup/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (bm *BackupManager) performBRBackup(backup *v1alpha1.Backup) error {
return err
}

backupFullPath, err := bm.brBackupData(backup)
backupFullPath, out, err := bm.brBackupData(backup)
if err != nil {
glog.Errorf("backup cluster %s data to %s failed, err: %s", bm, bm.StorageType, err)
return bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Expand All @@ -132,27 +132,25 @@ func (bm *BackupManager) performBRBackup(backup *v1alpha1.Backup) error {
}
glog.Infof("Get size %d for backup files in %s of cluster %s success", size, backupFullPath, bm)

// BR does not provide CommitTS yet
// https://github.com/pingcap/br/issues/76
// commitTs, err := getBRCommitTsFromMetadata(backupFullPath)
// if err != nil {
// glog.Errorf("get cluster %s commitTs failed, err: %s", bm, err)
// return bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
// Type: v1alpha1.BackupFailed,
// Status: corev1.ConditionTrue,
// Reason: "GetCommitTsFailed",
// Message: err.Error(),
// })
// }
// glog.Infof("get cluster %s commitTs %s success", bm, commitTs)
commitTs, err := getBRCommitTs(out)
if err != nil {
glog.Errorf("get cluster %s commitTs failed, err: %s", bm, err)
return bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Reason: "GetCommitTsFailed",
Message: err.Error(),
})
}
glog.Infof("get cluster %s commitTs %s success", bm, commitTs)

finish := time.Now()

backup.Status.BackupPath = backupFullPath
backup.Status.TimeStarted = metav1.Time{Time: started}
backup.Status.TimeCompleted = metav1.Time{Time: finish}
backup.Status.BackupSize = size
backup.Status.CommitTs = ""
backup.Status.CommitTs = commitTs

return bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupComplete,
Expand Down
12 changes: 6 additions & 6 deletions cmd/backup-manager/app/util/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ func ConstructBRGlobalOptions(backup *v1alpha1.Backup) ([]string, string, error)
if config.Key != "" {
args = append(args, fmt.Sprintf("--key=%s", config.Key))
}
if config.LogFile != "" {
args = append(args, fmt.Sprintf("--log-file=%s", config.LogFile))
}
if config.LogLevel != "" {
args = append(args, fmt.Sprintf("--log-level=%s", config.LogLevel))
}
// Do not set log-file, backup-manager needs to get backup
// position from the output of BR with info log-level
// if config.LogFile != "" {
// args = append(args, fmt.Sprintf("--log-file=%s", config.LogFile))
// }
args = append(args, "--log-level=info")
if config.StatusAddr != "" {
args = append(args, fmt.Sprintf("--status-addr=%s", config.StatusAddr))
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,6 @@ type BRConfig struct {
Cert string `json:"cert,omitempty"`
// Key is the private key path for TLS connection
Key string `json:"key,omitempty"`
// LogFile is the log file path. If not set, logs will output to stdout
LogFile string `json:"log-file,omitempty"`
// LogLevel is the log level, default to "info"
LogLevel string `json:"log-level,omitempty"`
// StatusAddr is the HTTP listening address for the status report service. Set to empty string to disable
StatusAddr string `json:"status-addr,omitempty"`
// Concurrency is the size of thread pool on each node that execute the backup task
Expand Down

0 comments on commit c17f89d

Please sign in to comment.