zfs-localpv/pkg/builder/restorebuilder/build.go
Pawan Prakash Sharma e40026c98a
feat(zfspv): adding backup and restore support (#162)
This commit adds support for Backup and Restore controller, which will be watching for
the events. The velero plugin will create a Backup CR to create a backup
with the remote location information, the controller will send the data
to that remote location.

In the same way, the velero plugin will create a Restore CR to restore the
volume from the the remote location and the restore controller will restore
the data.

Steps to use velero plugin for ZFS-LocalPV are :

1. install velero

2. add openebs plugin

velero plugin add openebs/velero-plugin:latest

3. Create the volumesnapshot location :

for full backup :-

```yaml
apiVersion: velero.io/v1
kind: VolumeSnapshotLocation
metadata:
  name: default
  namespace: velero
spec:
  provider: openebs.io/zfspv-blockstore
  config:
    bucket: velero
    prefix: zfs
    namespace: openebs
    provider: aws
    region: minio
    s3ForcePathStyle: "true"
    s3Url: http://minio.velero.svc:9000
```

for incremental backup :-

```yaml
apiVersion: velero.io/v1
kind: VolumeSnapshotLocation
metadata:
  name: default
  namespace: velero
spec:
  provider: openebs.io/zfspv-blockstore
  config:
    bucket: velero
    prefix: zfs
    backup: incremental
    namespace: openebs
    provider: aws
    region: minio
    s3ForcePathStyle: "true"
    s3Url: http://minio.velero.svc:9000
```

4. Create backup

velero backup create my-backup --snapshot-volumes --include-namespaces=velero-ns --volume-snapshot-locations=aws-cloud-default --storage-location=default

5. Create Schedule

velero create schedule newschedule  --schedule="*/1 * * * *" --snapshot-volumes --include-namespaces=velero-ns --volume-snapshot-locations=aws-local-default --storage-location=default

6. Restore from backup

velero restore create --from-backup my-backup --restore-volumes=true --namespace-mappings velero-ns:ns1



Signed-off-by: Pawan <pawan@mayadata.io>
2020-09-08 13:44:39 +05:30

169 lines
3.6 KiB
Go

/*
Copyright 2020 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package restorebuilder
import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
"github.com/openebs/zfs-localpv/pkg/common/errors"
)
// Builder is the builder object for ZFSRestore
type Builder struct {
rstr *ZFSRestore
errs []error
}
// NewBuilder returns new instance of Builder
func NewBuilder() *Builder {
return &Builder{
rstr: &ZFSRestore{
Object: &apis.ZFSRestore{},
},
}
}
// BuildFrom returns new instance of Builder
// from the provided api instance
func BuildFrom(rstr *apis.ZFSRestore) *Builder {
if rstr == nil {
b := NewBuilder()
b.errs = append(
b.errs,
errors.New("failed to build rstr object: nil rstr"),
)
return b
}
return &Builder{
rstr: &ZFSRestore{
Object: rstr,
},
}
}
// WithNamespace sets the namespace of ZFSRestore
func (b *Builder) WithNamespace(namespace string) *Builder {
if namespace == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi rstr object: missing namespace",
),
)
return b
}
b.rstr.Object.Namespace = namespace
return b
}
// WithName sets the name of ZFSRestore
func (b *Builder) WithName(name string) *Builder {
if name == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi rstr object: missing name",
),
)
return b
}
b.rstr.Object.Name = name
return b
}
// WithVolume sets the name of ZFSRestore
func (b *Builder) WithVolume(name string) *Builder {
if name == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi rstr object: missing volume name",
),
)
return b
}
b.rstr.Object.Spec.VolumeName = name
return b
}
// WithNode sets the node id for ZFSRestore
func (b *Builder) WithNode(node string) *Builder {
if node == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi rstr object: missing node name",
),
)
return b
}
b.rstr.Object.Spec.OwnerNodeID = node
return b
}
// WithStatus sets the status for ZFSRestore
func (b *Builder) WithStatus(status apis.ZFSRestoreStatus) *Builder {
b.rstr.Object.Status = status
return b
}
// WithRemote sets the node id for ZFSRestore
func (b *Builder) WithRemote(server string) *Builder {
if server == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi rstr object: missing node name",
),
)
return b
}
b.rstr.Object.Spec.RestoreSrc = server
return b
}
// WithLabels merges existing labels if any
// with the ones that are provided here
func (b *Builder) WithLabels(labels map[string]string) *Builder {
if len(labels) == 0 {
return b
}
if b.rstr.Object.Labels == nil {
b.rstr.Object.Labels = map[string]string{}
}
for key, value := range labels {
b.rstr.Object.Labels[key] = value
}
return b
}
// WithFinalizer merge existing finalizers if any
// with the ones that are provided here
func (b *Builder) WithFinalizer(finalizer []string) *Builder {
b.rstr.Object.Finalizers = append(b.rstr.Object.Finalizers, finalizer...)
return b
}
// Build returns ZFSRestore API object
func (b *Builder) Build() (*apis.ZFSRestore, error) {
if len(b.errs) > 0 {
return nil, errors.Errorf("%+v", b.errs)
}
return b.rstr.Object, nil
}