diff --git a/cmd/commands.go b/cmd/commands.go index 8f35be6..33741a7 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -92,6 +92,10 @@ func (c *CLI) RegisterCommands() { cmd.Command("detach", "detach ISO from a virtual machine (server will hard reboot)", serversDetachISO) cmd.Command("status", "show status of ISO attached to a virtual machine", serversStatusISO) }) + cmd.Command("restore", "restore from backup/snapshot", func(cmd *cli.Cmd) { + cmd.Command("backup", "restore from backup (any data already on the server will be lost)", serversRestoreBackup) + cmd.Command("snapshot", "restore from snapshot (any data already on the server will be lost)", serversRestoreSnapshot) + }) cmd.Command("delete", "delete a virtual machine", serversDelete) cmd.Command("bandwidth", "list bandwidth used by a virtual machine", serversBandwidth) cmd.Command("list", "list all active or pending virtual machines on current account", serversList) diff --git a/cmd/commands_servers.go b/cmd/commands_servers.go index f800330..8338437 100644 --- a/cmd/commands_servers.go +++ b/cmd/commands_servers.go @@ -219,6 +219,32 @@ func serversStatusISO(cmd *cli.Cmd) { } } +func serversRestoreBackup(cmd *cli.Cmd) { + cmd.Spec = "SUBID -b" + id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see )") + backupID := cmd.StringOpt("b backup", "", "BACKUPID of backups") + + cmd.Action = func() { + if err := GetClient().RestoreBackup(*id, *backupID); err != nil { + log.Fatal(err) + } + fmt.Printf("Virtual machine restored, server will be reboot: %v\n", *id) + } +} + +func serversRestoreSnapshot(cmd *cli.Cmd) { + cmd.Spec = "SUBID -s" + id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see )") + snapshotID := cmd.StringOpt("s snapshot", "", "SNAPSHOTID of snapshots") + + cmd.Action = func() { + if err := GetClient().RestoreSnapshot(*id, *snapshotID); err != nil { + log.Fatal(err) + } + fmt.Printf("Virtual machine restored, server will be reboot: %v\n", *id) + } +} + func serversListOS(cmd *cli.Cmd) { id := cmd.StringArg("SUBID", "", "SUBID of virtual machine (see )") cmd.Action = func() { diff --git a/lib/servers.go b/lib/servers.go index 418198a..eee1f1b 100644 --- a/lib/servers.go +++ b/lib/servers.go @@ -475,6 +475,32 @@ func (c *Client) GetISOStatusofServer(id string) (isoStatus ISOStatus, err error return isoStatus, nil } +// RestoreBackup restore the specified backup to the virtual machine +func (c *Client) RestoreBackup(id, backupID string) error { + values := url.Values{ + "SUBID": {id}, + "BACKUPID": {backupID}, + } + + if err := c.post(`server/restore_backup`, values, nil); err != nil { + return err + } + return nil +} + +// RestoreSnapshot restore the specified snapshot to the virtual machine +func (c *Client) RestoreSnapshot(id, snapshotID string) error { + values := url.Values{ + "SUBID": {id}, + "SNAPSHOTID": {snapshotID}, + } + + if err := c.post(`server/restore_snapshot`, values, nil); err != nil { + return err + } + return nil +} + // DeleteServer deletes an existing virtual machine func (c *Client) DeleteServer(id string) error { values := url.Values{