Exemple #1
0
func (runner *Runner) runTasks(configs *[]backuper.TaskConfig) (results []backuper.PathResult) {

	results = make([]backuper.PathResult, 0, len(*configs))

	for _, config := range *configs {
		beginTime := time.Now()
		task, err := GetTask(&config)
		if err == nil {
			taskTmpFileName := task.TmpFileName()
			tmpFilePath := runner.tmpFilePath(taskTmpFileName)

			out, err := task.GenerateTmpFile(tmpFilePath)

			results = append(results,
				backuper.NewPathResult(
					err,
					taskTmpFileName,
					string(out),
					beginTime,
					time.Now(),
				))
		} else {
			log.Printf("task type: %s, no registered handler found", config.Type)
		}
	}
	return
}
Exemple #2
0
func (runner *Runner) Run() (err error, backupResult *backuper.BackupResult) {
	configs := &runner.agentConfig.Tasks

	backupResult = &backuper.BackupResult{
		BeginTime: time.Now(),
		Size:      -1,
		Status:    backuper.BackupErrorCrytical,
	}

	beginTime := time.Now()
	err = runner.prepareTmpDir()

	tmpDirPath := runner.tmpDirPath()
	backupResult.Prepare = backuper.NewPathResult(
		err,
		tmpDirPath,
		"",
		beginTime,
		time.Now(),
	)

	if err != nil {
		log.Println("ERR: prepare:", err.Error())
		return
	}

	defer func() {
		beginTime = time.Now()
		err = runner.CleanupTmpDir()
		backupResult.Cleanup = backuper.NewPathResult(
			err,
			tmpDirPath,
			"",
			beginTime,
			time.Now(),
		)

		cryticalErrors := []*backuper.PathResult{
			&backupResult.Prepare,
			&backupResult.Lock,
			&backupResult.Encrypt,
			// &backupResult.Upload,
		}

		backupResult.Status = backuper.BackupErrorNo

		hasCryticalError := false
		for _, br := range cryticalErrors {
			if !ResultSuccess(br) {
				hasCryticalError = true
				backupResult.Status = backuper.BackupErrorCrytical
				break
			}
		}

		if !hasCryticalError {
			numTaskErrors := 0
			for _, br := range backupResult.Backup {
				if !ResultSuccess(&br) {
					numTaskErrors++
					backupResult.Status = backuper.BackupErrorTask
				}
			}
			if numTaskErrors == len(backupResult.Backup) {
				backupResult.Status = backuper.BackupErrorTaskAll
			} else {
				if !ResultSuccess(&backupResult.Unlock) || !ResultSuccess(&backupResult.Cleanup) {
					backupResult.Status = backuper.BackupErrorCleanup
				}
			}
		}

		backupResult.EndTime = time.Now()
	}()

	beginTime = time.Now()
	pidFilePath := runner.pidFilePath()
	err = runner.lockPidFile(pidFilePath)
	backupResult.Lock = backuper.NewPathResult(
		err,
		pidFilePath,
		"",
		beginTime,
		time.Now(),
	)

	if err != nil {
		log.Println("ERR: lock:", err.Error())
		return
	}

	backupResult.Backup = runner.runTasks(configs)

	tmpFiles := make([]string, 0, len(*configs))
	for _, result := range backupResult.Backup {
		if ResultSuccess(&result) {
			tmpFiles = append(tmpFiles, result.Path)
		}
	}

	beginTime = time.Now()

	backupFilePath := runner.backupFilePath()

	output, err := runner.encryptTmpFiles(backupFilePath, tmpFiles)
	backupResult.Encrypt = backuper.NewPathResult(
		err,
		backupFilePath,
		string(output),
		beginTime,
		time.Now(),
	)

	if err != nil {
		log.Println("ERR: encrypt:", err.Error())
		return
	}

	fi, err := os.Stat(backupFilePath)
	if err == nil {
		backupResult.Size = fi.Size()
	}

	// dstPath := runner.formatDstPath("backup/%hostname%/%timestamp%")
	dstPath := hmutil.FormatDstPath(filepath.Join("backup", "%hostname%", "%timestamp%"), runner.timestamp)
	uploadResults, err := runner.uploadBackupFile(
		backupFilePath, runner.agentConfig.UploadMethod, dstPath,
	)
	// backupResult.Upload = backuper.NewPathResult(
	// 	err,
	// 	backupFilePath,
	// 	string(output),
	// 	beginTime,
	// 	time.Now(),
	// )

	backupResult.Upload = uploadResults
	// backuper.UploadResult{
	// 	Err: err.Error(), // next line fixes this
	// 	// Err: "",
	// 	Path: backupFilePath + ".tar.gz.encrypted",
	// 	Type: "S3",
	// 	Destination: "headmade",
	// 	BeginTime: beginTime,
	// 	EndTime: time.Now(),
	// }

	if err != nil {
		log.Println("ERR: upload:", err.Error())
		return
	}

	beginTime = time.Now()
	// TODO: remember to uncomment the unlock()!
	//err = runner.unlockPidFile()
	backupResult.Unlock = backuper.NewPathResult(
		err,
		pidFilePath,
		"",
		beginTime,
		time.Now(),
	)

	return
}