Example #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)
		}
	}
}
Example #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))
	}
}
Example #3
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)
		}
	}
}
Example #4
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)
			}
		}
	}
}