Skip to content

Commit

Permalink
add --from-backup flag to ark restore create & allow restore name
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kriss <steve@heptio.com>
  • Loading branch information
skriss committed Mar 5, 2018
1 parent f53d605 commit c281124
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Make sure that you install somewhere in your `$PATH`.
1. Run:

```
ark restore create nginx-backup
ark restore create --from-backup nginx-backup
```

1. Run:
Expand Down
4 changes: 2 additions & 2 deletions docs/cloud-common.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ After you set up the Ark server, try these examples:
1. Restore your lost resources:

```bash
ark restore create nginx-backup
ark restore create --from-backup nginx-backup
```

### Snapshot example (with PersistentVolumes)
Expand Down Expand Up @@ -67,7 +67,7 @@ After you set up the Ark server, try these examples:
1. Restore your lost resources:

```bash
ark restore create nginx-backup
ark restore create --from-backup nginx-backup
```

[0]: aws-config.md
Expand Down
4 changes: 2 additions & 2 deletions docs/use-cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If you periodically back up your cluster's resources, you are able to return to
4. Create a restore with your most recent Ark Backup:
```
ark restore create <SCHEDULE NAME>-<TIMESTAMP>
ark restore create --from-backup <SCHEDULE NAME>-<TIMESTAMP>
```
## Cluster migration
Expand All @@ -45,7 +45,7 @@ Heptio Ark can help you port your resources from one cluster to another, as long
4. *(Cluster 2)* Once you have confirmed that the right Backup (`<BACKUP-NAME>`) is now present, you can restore everything with:
```
ark restore create <BACKUP-NAME>
ark restore create --from-backup <BACKUP-NAME>
```
[0]: #disaster-recovery
Expand Down
28 changes: 22 additions & 6 deletions pkg/cmd/cli/restore/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ func NewCreateCommand(f client.Factory, use string) *cobra.Command {
o := NewCreateOptions()

c := &cobra.Command{
Use: use + " BACKUP",
Use: use + " [RESTORE_NAME] --from-backup BACKUP_NAME",
Short: "Create a restore",
Example: ` # create a restore named "restore-1" from backup "backup-1"
ark restore create restore-1 --from-backup backup-1
# create a restore with a default name ("backup-1-<timestamp>") from backup "backup-1"
ark restore create --from-backup backup-1`,
Run: func(c *cobra.Command, args []string) {
cmd.CheckError(o.Validate(c, args, f))
cmd.CheckError(o.Complete(args))
Expand All @@ -56,6 +61,7 @@ func NewCreateCommand(f client.Factory, use string) *cobra.Command {

type CreateOptions struct {
BackupName string
RestoreName string
RestoreVolumes flag.OptionalBool
Labels flag.Map
IncludeNamespaces flag.StringArray
Expand All @@ -80,6 +86,7 @@ func NewCreateOptions() *CreateOptions {
}

func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.BackupName, "from-backup", "", "backup to restore from")
flags.Var(&o.IncludeNamespaces, "include-namespaces", "namespaces to include in the restore (use '*' for all namespaces)")
flags.Var(&o.ExcludeNamespaces, "exclude-namespaces", "namespaces to exclude from the restore")
flags.Var(&o.NamespaceMappings, "namespace-mappings", "namespace mappings from name in the backup to desired restored name in the form src1:dst1,src2:dst2,...")
Expand All @@ -97,8 +104,12 @@ func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) {
}

func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Factory) error {
if len(args) != 1 {
return errors.New("you must specify only one argument, the backup's name")
if len(o.BackupName) == 0 {
return errors.New("--from-backup is required")
}

if len(args) > 1 {
return errors.New("you may specify at most one argument, the restore's name")
}

if err := output.ValidateFlags(c); err != nil {
Expand All @@ -111,7 +122,7 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
}
o.client = client

_, err = o.client.ArkV1().Backups(f.Namespace()).Get(args[0], metav1.GetOptions{})
_, err = o.client.ArkV1().Backups(f.Namespace()).Get(o.BackupName, metav1.GetOptions{})
if err != nil {
return err
}
Expand All @@ -120,7 +131,12 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
}

func (o *CreateOptions) Complete(args []string) error {
o.BackupName = args[0]
if len(args) == 1 {
o.RestoreName = args[0]
} else {
o.RestoreName = fmt.Sprintf("%s-%s", o.BackupName, time.Now().Format("20060102150405"))
}

return nil
}

Expand All @@ -133,7 +149,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
restore := &api.Restore{
ObjectMeta: metav1.ObjectMeta{
Namespace: f.Namespace(),
Name: fmt.Sprintf("%s-%s", o.BackupName, time.Now().Format("20060102150405")),
Name: o.RestoreName,
Labels: o.Labels.Data(),
},
Spec: api.RestoreSpec{
Expand Down

0 comments on commit c281124

Please sign in to comment.