forked from pkallberg/cx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backups-download.go
72 lines (58 loc) · 1.54 KB
/
backups-download.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
70
71
72
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/cloud66/cli"
)
func runDownloadBackup(c *cli.Context) {
if len(c.Args()) == 0 {
//cmd.printUsage()
os.Exit(2)
}
stack := mustStack(c)
backupId, err := strconv.Atoi(c.Args()[0])
if err != nil {
//cmd.printUsage()
os.Exit(2)
}
segmentIndeces, err := client.GetBackupSegmentIndeces(stack.Uid, backupId)
must(err)
if len(segmentIndeces) < 1 {
printFatal("Cannot find file segments associated with this backup")
}
flagDownloadDir := c.String("directory")
mainDir := filepath.Join(homePath(), "cx_backups")
if flagDownloadDir != "" {
mainDir = flagDownloadDir
}
// create a download tmp folder
dir := filepath.Join(mainDir, "tmp", c.Args()[0])
err = os.MkdirAll(dir, 0777)
must(err)
var files = []string{}
for _, segmentIndex := range segmentIndeces {
segment, err := client.GetBackupSegment(stack.Uid, backupId, segmentIndex.Extension)
must(err)
fmt.Printf("Downloading %s to %s\n", segmentIndex.Filename, dir)
// this should be moved to go routines
toFile := filepath.Join(dir, segmentIndex.Filename)
err = downloadFile(segment.Url, toFile)
must(err)
files = append(files, toFile)
}
toFile := filepath.Join(mainDir, "backup_"+c.Args()[0]+".tar")
fmt.Printf("Concatenating files to %s\n", toFile)
err = appendFiles(files, toFile)
if err != nil {
printFatal("Error during concatenation: " + err.Error())
return
}
// remove the temp
if !debugMode {
os.RemoveAll(dir)
fmt.Printf("Deleting %s\n", dir)
}
fmt.Println("Done")
}