github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/operator_snapshot_restore.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/hashicorp/nomad/api" 9 "github.com/posener/complete" 10 ) 11 12 type OperatorSnapshotRestoreCommand struct { 13 Meta 14 } 15 16 func (c *OperatorSnapshotRestoreCommand) Help() string { 17 helpText := ` 18 Usage: nomad operator snapshot restore [options] <file> 19 20 Restores an atomic, point-in-time snapshot of the state of the Nomad servers 21 which includes jobs, nodes, allocations, periodic jobs, and ACLs. 22 23 Restores involve a potentially dangerous low-level Raft operation that is not 24 designed to handle server failures during a restore. This command is primarily 25 intended to be used when recovering from a disaster, restoring into a fresh 26 cluster of Nomad servers. 27 28 If ACLs are enabled, a management token must be supplied in order to perform 29 snapshot operations. 30 31 To restore a snapshot from the file "backup.snap": 32 33 $ nomad operator snapshot restore backup.snap 34 35 General Options: 36 37 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) 38 return strings.TrimSpace(helpText) 39 } 40 41 func (c *OperatorSnapshotRestoreCommand) AutocompleteFlags() complete.Flags { 42 return c.Meta.AutocompleteFlags(FlagSetClient) 43 } 44 45 func (c *OperatorSnapshotRestoreCommand) AutocompleteArgs() complete.Predictor { 46 return complete.PredictNothing 47 } 48 49 func (c *OperatorSnapshotRestoreCommand) Synopsis() string { 50 return "Restore snapshot of Nomad server state" 51 } 52 53 func (c *OperatorSnapshotRestoreCommand) Name() string { return "operator snapshot restore" } 54 55 func (c *OperatorSnapshotRestoreCommand) Run(args []string) int { 56 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 57 flags.Usage = func() { c.Ui.Output(c.Help()) } 58 59 if err := flags.Parse(args); err != nil { 60 c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err)) 61 return 1 62 } 63 64 // Check for misuse 65 args = flags.Args() 66 if len(args) != 1 { 67 c.Ui.Error("This command takes one argument: <filename>") 68 c.Ui.Error(commandErrorText(c)) 69 return 1 70 } 71 72 snap, err := os.Open(args[0]) 73 if err != nil { 74 c.Ui.Error(fmt.Sprintf("Error opening snapshot file: %q", err)) 75 return 1 76 } 77 defer snap.Close() 78 79 // Set up a client. 80 client, err := c.Meta.Client() 81 if err != nil { 82 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 83 return 1 84 } 85 86 // Call snapshot restore API with backup file. 87 _, err = client.Operator().SnapshotRestore(snap, &api.WriteOptions{}) 88 if err != nil { 89 c.Ui.Error(fmt.Sprintf("Failed to get restore snapshot: %v", err)) 90 return 1 91 } 92 93 c.Ui.Output("Snapshot Restored") 94 return 0 95 }