-
Notifications
You must be signed in to change notification settings - Fork 937
/
Copy pathrestore.go
77 lines (60 loc) · 1.64 KB
/
restore.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
73
74
75
76
77
package main
import (
"github.com/spf13/cobra"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
cli "github.com/canonical/lxd/shared/cmd"
"github.com/canonical/lxd/shared/i18n"
)
type cmdRestore struct {
global *cmdGlobal
flagStateful bool
}
func (c *cmdRestore) command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("restore", i18n.G("[<remote>:]<instance> <snapshot>"))
cmd.Short = i18n.G("Restore instances from snapshots")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Restore instances from snapshots
If --stateful is passed, then the running state will be restored too.`))
cmd.Example = cli.FormatSection("", i18n.G(
`lxc snapshot u1 snap0
Create the snapshot.
lxc restore u1 snap0
Restore the snapshot.`))
cmd.RunE = c.run
cmd.Flags().BoolVar(&c.flagStateful, "stateful", false, i18n.G("Whether or not to restore the instance's running state from snapshot (if available)"))
return cmd
}
func (c *cmdRestore) run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 2, 2)
if exit {
return err
}
// Connect to LXD
remote, name, err := conf.ParseRemote(args[0])
if err != nil {
return err
}
d, err := conf.GetInstanceServer(remote)
if err != nil {
return err
}
// Setup the snapshot restore
snapname := args[1]
if !shared.IsSnapshot(snapname) {
snapname = name + "/" + snapname
}
req := api.InstancePut{
Restore: snapname,
Stateful: c.flagStateful,
}
// Restore the snapshot
op, err := d.UpdateInstance(name, req, "")
if err != nil {
return err
}
return op.Wait()
}