github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/app/backup.go (about) 1 package app 2 3 import ( 4 "fmt" 5 6 "github.com/Sirupsen/logrus" 7 "github.com/codegangsta/cli" 8 "github.com/rancher/longhorn/sync" 9 ) 10 11 func BackupCmd() cli.Command { 12 return cli.Command{ 13 Name: "backups", 14 ShortName: "backup", 15 Subcommands: []cli.Command{ 16 BackupCreateCmd(), 17 BackupRestoreCmd(), 18 BackupRmCmd(), 19 BackupInspectCmd(), 20 }, 21 } 22 } 23 24 func BackupCreateCmd() cli.Command { 25 return cli.Command{ 26 Name: "create", 27 Usage: "create a backup in objectstore: create <snapshot> --dest <dest>", 28 Flags: []cli.Flag{ 29 cli.StringFlag{ 30 Name: "dest", 31 Usage: "destination of backup if driver supports, would be url like s3://bucket@region/path/ or vfs:///path/", 32 }, 33 }, 34 Action: func(c *cli.Context) { 35 if err := createBackup(c); err != nil { 36 logrus.Fatalf("Error running create backup command: %v", err) 37 } 38 }, 39 } 40 } 41 42 func BackupRmCmd() cli.Command { 43 return cli.Command{ 44 Name: "rm", 45 Usage: "remove a backup in objectstore: rm <backup>", 46 Action: func(c *cli.Context) { 47 if err := rmBackup(c); err != nil { 48 logrus.Fatalf("Error running rm backup command: %v", err) 49 } 50 }, 51 } 52 } 53 54 func BackupRestoreCmd() cli.Command { 55 return cli.Command{ 56 Name: "restore", 57 Usage: "restore a backup to current volume: restore <backup>", 58 Action: func(c *cli.Context) { 59 if err := restoreBackup(c); err != nil { 60 logrus.Fatalf("Error running restore backup command: %v", err) 61 } 62 }, 63 } 64 } 65 66 func BackupInspectCmd() cli.Command { 67 return cli.Command{ 68 Name: "inspect", 69 Usage: "inspect a backup: inspect <backup>", 70 Action: func(c *cli.Context) { 71 if err := inspectBackup(c); err != nil { 72 logrus.Fatalf("Error running inspect backup command: %v", err) 73 } 74 }, 75 } 76 } 77 78 func createBackup(c *cli.Context) error { 79 url := c.GlobalString("url") 80 task := sync.NewTask(url) 81 82 dest := c.String("dest") 83 if dest == "" { 84 return fmt.Errorf("Missing required parameter --dest") 85 } 86 87 snapshot := c.Args().First() 88 if snapshot == "" { 89 return fmt.Errorf("Missing required parameter snapshot") 90 } 91 92 backup, err := task.CreateBackup(snapshot, dest) 93 if err != nil { 94 return err 95 } 96 fmt.Println(backup) 97 98 return nil 99 } 100 101 func rmBackup(c *cli.Context) error { 102 url := c.GlobalString("url") 103 task := sync.NewTask(url) 104 105 backup := c.Args().First() 106 if backup == "" { 107 return fmt.Errorf("Missing required parameter backup") 108 } 109 110 if err := task.RmBackup(backup); err != nil { 111 return err 112 } 113 114 return nil 115 } 116 117 func restoreBackup(c *cli.Context) error { 118 url := c.GlobalString("url") 119 task := sync.NewTask(url) 120 121 backup := c.Args().First() 122 if backup == "" { 123 return fmt.Errorf("Missing required parameter backup") 124 } 125 126 if err := task.RestoreBackup(backup); err != nil { 127 return err 128 } 129 130 return nil 131 } 132 133 func inspectBackup(c *cli.Context) error { 134 url := c.GlobalString("url") 135 task := sync.NewTask(url) 136 137 backup := c.Args().First() 138 if backup == "" { 139 return fmt.Errorf("Missing required parameter backup") 140 } 141 142 output, err := task.InspectBackup(backup) 143 if err != nil { 144 return err 145 } 146 fmt.Println(output) 147 148 return nil 149 }