|
| 1 | +package backup |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/gravitational/trace" |
| 9 | + "github.com/solidDoWant/backup-tool/pkg/cleanup" |
| 10 | + "github.com/solidDoWant/backup-tool/pkg/contexts" |
| 11 | + "github.com/solidDoWant/backup-tool/pkg/disasterrecovery/actions/remote" |
| 12 | + "github.com/solidDoWant/backup-tool/pkg/grpc/clients" |
| 13 | + "github.com/solidDoWant/backup-tool/pkg/kubecluster" |
| 14 | + "github.com/solidDoWant/backup-tool/pkg/kubecluster/composite/backuptoolinstance" |
| 15 | + "github.com/solidDoWant/backup-tool/pkg/kubecluster/composite/clonepvc" |
| 16 | + "github.com/solidDoWant/backup-tool/pkg/kubecluster/helpers" |
| 17 | + "github.com/solidDoWant/backup-tool/pkg/kubecluster/primatives/core" |
| 18 | + corev1 "k8s.io/api/core/v1" |
| 19 | +) |
| 20 | + |
| 21 | +type FilesBackupOptions struct { |
| 22 | + CleanupTimeout helpers.MaxWaitTime `yaml:"cleanupTimeout,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +// FilesBackupInterface is a RemoteStage action that captures a live data-directory PVC into the DR |
| 26 | +// volume. The source PVC is in use, so it cannot be read directly; the action snapshots/clones it for a |
| 27 | +// consistent point-in-time view, syncs the clone's contents into a subdirectory of the DR volume, and |
| 28 | +// tears the clone down afterwards (so it is a CleanupAction). A volume snapshot exists only at the moment |
| 29 | +// it is taken and cannot be reconstructed for an arbitrary instant, so as a remote.PreConsistencyPointAction |
| 30 | +// it takes the clone before the consistency point is fixed and pins the point to the clone's creation time; |
| 31 | +// the other captures then align to that filesystem freeze (a database clone recovers forward to it). |
| 32 | +type FilesBackupInterface interface { |
| 33 | + remote.CleanupAction |
| 34 | + remote.PreConsistencyPointAction |
| 35 | + Configure(kubeClusterClient kubecluster.ClientInterface, namespace, sourcePVCName, drVolName, backupDirRelPath string, opts FilesBackupOptions) error |
| 36 | +} |
| 37 | + |
| 38 | +type configureState struct { |
| 39 | + uid string // Unique identifier to prevent accidental collisions between multiple instances |
| 40 | + isConfigured bool |
| 41 | + kubeClusterClient kubecluster.ClientInterface |
| 42 | + namespace string |
| 43 | + sourcePVCName string |
| 44 | + drVolName string |
| 45 | + backupDirRelPath string |
| 46 | + opts FilesBackupOptions |
| 47 | +} |
| 48 | + |
| 49 | +func (cs *configureState) Configure(kubeClusterClient kubecluster.ClientInterface, namespace, sourcePVCName, drVolName, backupDirRelPath string, opts FilesBackupOptions) error { |
| 50 | + if cs.isConfigured { |
| 51 | + return trace.Errorf("attempted to configure multiple times") |
| 52 | + } |
| 53 | + |
| 54 | + cs.uid = uuid.NewString() |
| 55 | + cs.kubeClusterClient = kubeClusterClient |
| 56 | + cs.namespace = namespace |
| 57 | + cs.sourcePVCName = sourcePVCName |
| 58 | + cs.drVolName = drVolName |
| 59 | + cs.backupDirRelPath = backupDirRelPath |
| 60 | + cs.opts = opts |
| 61 | + |
| 62 | + cs.isConfigured = true |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (cs *configureState) ctxLogWith(ctx *contexts.Context) *contexts.LoggerContext { |
| 67 | + return ctx.Log.With("sourcePVC", cs.sourcePVCName, "uid", cs.uid) |
| 68 | +} |
| 69 | + |
| 70 | +type validateState struct { |
| 71 | + configureState |
| 72 | + isValidated bool |
| 73 | +} |
| 74 | + |
| 75 | +func (vs *validateState) Validate(ctx *contexts.Context) (err error) { |
| 76 | + vs.ctxLogWith(ctx).Info("Validating configuration for files backup") |
| 77 | + defer ctx.Log.Info("Completed files backup configuration validation", ctx.Stopwatch.Keyval(), contexts.ErrorKeyvals(&err)) |
| 78 | + |
| 79 | + if !vs.isConfigured { |
| 80 | + return trace.Errorf("attempted to validate without configuring") |
| 81 | + } |
| 82 | + |
| 83 | + if _, err := vs.kubeClusterClient.Core().GetPVC(ctx.Child(), vs.namespace, vs.sourcePVCName); err != nil { |
| 84 | + return trace.Wrap(err, "failed to get source data PVC %q", vs.sourcePVCName) |
| 85 | + } |
| 86 | + |
| 87 | + if _, err := vs.kubeClusterClient.Core().GetPVC(ctx.Child(), vs.namespace, vs.drVolName); err != nil { |
| 88 | + return trace.Wrap(err, "failed to get DR PVC %q", vs.drVolName) |
| 89 | + } |
| 90 | + |
| 91 | + vs.isValidated = true |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// cloneState holds the PVC clone taken before the consistency point is established. |
| 96 | +type cloneState struct { |
| 97 | + validateState |
| 98 | + clonedPVC *corev1.PersistentVolumeClaim |
| 99 | + isCloned bool |
| 100 | +} |
| 101 | + |
| 102 | +// BeforeConsistencyPoint clones the source data PVC and returns the clone's creation time, pinning the |
| 103 | +// event's consistency point to the moment the filesystem was frozen so the database recovers forward to |
| 104 | +// exactly that state. Implements remote.PreConsistencyPointAction. |
| 105 | +func (cs *cloneState) BeforeConsistencyPoint(ctx *contexts.Context) (_ time.Time, err error) { |
| 106 | + cs.ctxLogWith(ctx).Info("Cloning data directory for files backup") |
| 107 | + defer ctx.Log.Info("Files backup clone complete", ctx.Stopwatch.Keyval(), contexts.ErrorKeyvals(&err)) |
| 108 | + |
| 109 | + if !cs.isValidated { |
| 110 | + return time.Time{}, trace.Errorf("attempted to clone without validating") |
| 111 | + } |
| 112 | + |
| 113 | + if cs.isCloned { |
| 114 | + return time.Time{}, trace.Errorf("attempted to clone multiple times") |
| 115 | + } |
| 116 | + |
| 117 | + // ForceBind is required because the snapshot is deleted once the clone exists, so the clone must bind |
| 118 | + // immediately. |
| 119 | + clonedPVC, err := cs.kubeClusterClient.ClonePVC(ctx.Child(), cs.namespace, cs.sourcePVCName, clonepvc.ClonePVCOptions{ |
| 120 | + DestPvcNamePrefix: cs.drVolName, |
| 121 | + ForceBind: true, |
| 122 | + CleanupTimeout: cs.opts.CleanupTimeout, |
| 123 | + }) |
| 124 | + if err != nil { |
| 125 | + return time.Time{}, trace.Wrap(err, "failed to clone source data PVC %q", cs.sourcePVCName) |
| 126 | + } |
| 127 | + cs.clonedPVC = clonedPVC |
| 128 | + cs.isCloned = true |
| 129 | + |
| 130 | + return clonedPVC.CreationTimestamp.Time, nil |
| 131 | +} |
| 132 | + |
| 133 | +type setupStateMountPaths struct { |
| 134 | + drVolume string |
| 135 | + data string |
| 136 | +} |
| 137 | + |
| 138 | +type setupState struct { |
| 139 | + cloneState |
| 140 | + mountPaths setupStateMountPaths |
| 141 | + isSetup bool |
| 142 | +} |
| 143 | + |
| 144 | +func (ss *setupState) Setup(ctx *contexts.Context, btiOpts *backuptoolinstance.CreateBackupToolInstanceOptions) (err error) { |
| 145 | + ss.ctxLogWith(ctx).Info("Setting up for files backup") |
| 146 | + defer ctx.Log.Info("Files backup setup complete", ctx.Stopwatch.Keyval(), contexts.ErrorKeyvals(&err)) |
| 147 | + |
| 148 | + if !ss.isCloned { |
| 149 | + return trace.Errorf("attempted to setup without cloning") |
| 150 | + } |
| 151 | + |
| 152 | + if ss.isSetup { |
| 153 | + return trace.Errorf("attempted to setup multiple times") |
| 154 | + } |
| 155 | + |
| 156 | + baseMountPath := filepath.Join("/mnt", "filesbackup", ss.uid) |
| 157 | + ss.mountPaths = setupStateMountPaths{ |
| 158 | + drVolume: filepath.Join(baseMountPath, "dr"), |
| 159 | + data: filepath.Join(baseMountPath, "data"), |
| 160 | + } |
| 161 | + |
| 162 | + btiOpts.Volumes = append(btiOpts.Volumes, |
| 163 | + core.NewSingleContainerPVC(ss.drVolName, ss.mountPaths.drVolume), |
| 164 | + core.NewSingleContainerPVC(ss.clonedPVC.Name, ss.mountPaths.data), |
| 165 | + ) |
| 166 | + |
| 167 | + ss.isSetup = true |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +// Cleanup tears down the cloned PVC. It tolerates partial state (e.g. the action never reached Setup |
| 172 | +// because another action failed first), deleting nothing in that case. |
| 173 | +func (ss *setupState) Cleanup(ctx *contexts.Context) error { |
| 174 | + if ss.clonedPVC == nil { |
| 175 | + return nil |
| 176 | + } |
| 177 | + |
| 178 | + err := cleanup.To(func(ctx *contexts.Context) error { |
| 179 | + return ss.kubeClusterClient.Core().DeletePVC(ctx, ss.namespace, ss.clonedPVC.Name) |
| 180 | + }).WithErrMessage("failed to cleanup cloned data PVC %q", helpers.FullName(ss.clonedPVC)). |
| 181 | + WithParentCtx(ctx).WithTimeout(ss.opts.CleanupTimeout.MaxWait(time.Minute)). |
| 182 | + RunError() |
| 183 | + return trace.Wrap(err, "failed to cleanup files backup resources") |
| 184 | +} |
| 185 | + |
| 186 | +type executeState struct { |
| 187 | + setupState |
| 188 | +} |
| 189 | + |
| 190 | +func (es *executeState) Execute(ctx *contexts.Context, backupToolClient clients.ClientInterface) (err error) { |
| 191 | + es.ctxLogWith(ctx).Info("Executing files backup") |
| 192 | + defer ctx.Log.Info("Files backup complete", ctx.Stopwatch.Keyval(), contexts.ErrorKeyvals(&err)) |
| 193 | + |
| 194 | + if !es.isSetup { |
| 195 | + return trace.Errorf("attempted to execute without setting up") |
| 196 | + } |
| 197 | + |
| 198 | + drDataPath := filepath.Join(es.mountPaths.drVolume, es.backupDirRelPath) |
| 199 | + err = backupToolClient.Files().SyncFiles(ctx.Child(), es.mountPaths.data, drDataPath) |
| 200 | + return trace.Wrap(err, "failed to sync data directory files at %q to the disaster recovery volume at %q", es.mountPaths.data, drDataPath) |
| 201 | +} |
| 202 | + |
| 203 | +type FilesBackup struct { |
| 204 | + executeState |
| 205 | +} |
| 206 | + |
| 207 | +func NewFilesBackup() FilesBackupInterface { |
| 208 | + return &FilesBackup{} |
| 209 | +} |
0 commit comments