Exemplo n.º 1
0
func checkAction(c *cli.Context) {
	conf, err := config.New()
	if err != nil {
		log.Fatal(err)
	}
	if !conf.Local {
		client, err := client.NewClient(BackendAddr())
		if err != nil {
			log.Fatal(err)
		}
		var agentConfig *backuper.AgentConfig
		err = client.GetConfig(&agentConfig)
		if err != nil {
			log.Fatal(err)
		}
		conf.Write(agentConfig)
		scheduler, err := schedule.New()
		if err != nil {
			log.Fatal(err)
		}
		if err := scheduler.UpdateBackup(agentConfig.Period); err != nil {
			log.Println(err)
		}

		if conf.Agent.StartNow {
			log.Println("StartNow")
			backupAction(c)
			conf.Agent.StartNow = !conf.Agent.StartNow
			conf.Write(agentConfig)
		}
	}
}
Exemplo n.º 2
0
func listAction(c *cli.Context) {
	conf, err := config.New()
	if err != nil {
		log.Fatal(err)
	}

	if !conf.Local {
		cl, err := client.NewClient(BackendAddr())
		if err != nil {
			log.Fatal(err)
		}

		var blist []backuper.BackupResult
		if err = cl.GetBackupsList(&blist); err != nil {
			log.Fatal(err)
		}

		tail, err := strconv.Atoi(c.Args()[0])
		if err != nil {
			log.Fatal(err)
		}

		r, err := json.MarshalIndent(blist[:tail], "", "  ")
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(string(r))
	}
}
Exemplo n.º 3
0
func NewClient(backendAddr string) (*Client, error) {
	conf, err := config.New()
	if err != nil {
		return nil, err
	}
	return &Client{backendAddr, conf.Client.Token}, err
}
Exemplo n.º 4
0
func providerCommand(name string, providerConfig config.Provider) {
	conf, err := config.New()
	if err != nil {
		log.Fatal(err)
	}
	if conf.Secret == nil {
		conf.Secret = config.Providers{}
	}
	// conf.Secret[name] = providerConfig
	if conf.Secret[name] == nil {
		conf.Secret[name] = config.Provider{}
	}
	for k, v := range providerConfig {
		conf.Secret[name][k] = v
	}
	conf.Write(conf.Secret)
}
Exemplo n.º 5
0
func initAction(c *cli.Context) {
	checkUID(c.Command.Name)
	scheduler, err := schedule.New()
	if err != nil {
		log.Fatal(err)
	}
	if err := scheduler.UpdateCheck(); err != nil {
		log.Fatal(err)
	}
	if c.Args().First() == "local" {
		// conf := config.Config{Local: true}
		conf, _ := config.New()
		conf.Local = true
		conf.Write(true)
	} else {
		if err := initServer(c); err != nil {
			log.Fatal(err)
		}
	}
	fmt.Println("Success! This server is ready to backup.")
}
Exemplo n.º 6
0
func backupAction(c *cli.Context) {
	conf, err := config.New()
	if err != nil {
		log.Fatal("This server is not ready to backup. Please exec 'backuper init'")
	}
	agent, err := agent.Get(conf.Agent, &conf.Secret)
	if err != nil {
		log.Fatal(err)
	}
	lastErr, result := agent.Backup()
	if lastErr != nil {
		// do smth clever or useful, or both
	}

	if !conf.Local {
		client, err := client.NewClient(BackendAddr())
		if err != nil {
			log.Fatal(err)
		}
		if err := client.Backup(result); err != nil {
			log.Printf("Backup notification Error: %s", err)
		}
	}
}
Exemplo n.º 7
0
func retrieveAction(c *cli.Context) {
	conf, err := config.New()
	if err != nil {
		log.Fatal(err)
	}

	if !conf.Local {
		cl, err := client.NewClient(BackendAddr())
		if err != nil {
			log.Fatal(err)
		}

		var backup backuper.BackupResult
		id, err := strconv.Atoi(c.Args()[0])
		if err != nil {
			log.Fatal(err)
		}

		if err = cl.GetBackup(&backup, id); err != nil {
			log.Fatal(err)
		}

		fmt.Println(backup.Upload)

		_, name := filepath.Split(backup.Upload.Path)
		name = strings.Split(name, ".encrypted")[0]
		switch backup.Upload.Type {
		case "ssh":
			addr := strings.Split(backup.Upload.Destination, "@")
			login, host := addr[0], addr[1]
			ssh := hmutil.SSHDownloader(
				22,
				conf.Secret["SSH"]["id_rsa"],
				host,
				login,
				backup.Upload.Path,
				conf.Secret["SSH"]["download_path"],
			)
			result, err := hmutil.SSHExec(ssh)

			if err != nil {
				log.Fatal(err)
			}

			decoded_result, err := hmutil.Decode(&result, []byte(conf.Secret["encryption"]["pass"]))

			if err != nil {
				log.Fatal(err)
			}

			if err = hmutil.WriteToFile(conf.Secret["SSH"]["download_path"]+name, decoded_result); err != nil {
				log.Fatal(err)
			}
		case "ftp":
			addr := strings.Split(backup.Upload.Destination, "@")
			login, host := addr[0], addr[1]
			result, err := hmutil.FTPDownload(21, host, login, conf.Secret["FTP"]["passw"], backup.Upload.Path)

			if err != nil {
				log.Fatal(err)
			}

			decoded_result, err := hmutil.Decode(&result, []byte(conf.Secret["encryption"]["pass"]))

			if err != nil {
				log.Fatal(err)
			}

			if err = hmutil.WriteToFile(conf.Secret["FTP"]["download_path"]+name, decoded_result); err != nil {
				log.Fatal(err)
			}
		case "S3":
			result, err := hmutil.DownloadFromS3(
				s3gof3r.Keys{
					AccessKey: conf.Secret["AWS"]["AWS_ACCESS_KEY_ID"],
					SecretKey: conf.Secret["AWS"]["AWS_SECRET_ACCESS_KEY"],
				},
				backup.Upload.Destination,
				backup.Upload.Path,
			)

			if err != nil {
				log.Fatal(err)
			}

			decoded_result, err := hmutil.Decode(&result, []byte(conf.Secret["encryption"]["pass"]))

			if err != nil {
				log.Fatal(err)
			}

			if err = hmutil.WriteToFile(conf.Secret["AWS"]["download_path"]+name, decoded_result); err != nil {
				log.Fatal(err)
			}
		}
	}
}