-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for localfs provider (#34)
- Loading branch information
Showing
12 changed files
with
553 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package localfs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/utils" | ||
"github.com/metal-stack/backup-restore-sidecar/pkg/constants" | ||
) | ||
|
||
type LocalFS struct { | ||
datadir string | ||
log *slog.Logger | ||
} | ||
|
||
func New(log *slog.Logger, datadir string) *LocalFS { | ||
return &LocalFS{ | ||
datadir: datadir, | ||
log: log, | ||
} | ||
} | ||
|
||
// Check if Datadir is empty | ||
func (l *LocalFS) Check(ctx context.Context) (bool, error) { | ||
empty, err := utils.IsEmpty(l.datadir) | ||
if err != nil { | ||
return false, err | ||
} | ||
if empty { | ||
l.log.Info("data directory is empty") | ||
return true, err | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
// put Datadir into constants.BackupDir directory | ||
func (l *LocalFS) Backup(ctx context.Context) error { | ||
if err := os.RemoveAll(constants.BackupDir); err != nil { | ||
return fmt.Errorf("could not clean backup directory: %w", err) | ||
} | ||
|
||
if err := os.MkdirAll(constants.BackupDir, 0777); err != nil { | ||
return fmt.Errorf("could not create backup directory: %w", err) | ||
} | ||
|
||
if err := utils.CopyFS(constants.BackupDir, os.DirFS(l.datadir)); err != nil { | ||
return fmt.Errorf("could not copy contents: %w", err) | ||
} | ||
|
||
l.log.Debug("Sucessfully took backup of localfs") | ||
return nil | ||
} | ||
|
||
// get data from constants.RestoreDir | ||
func (l *LocalFS) Recover(ctx context.Context) error { | ||
if err := utils.RemoveContents(l.datadir); err != nil { | ||
return fmt.Errorf("Could not cleanup Datadir: %w", err) | ||
} | ||
|
||
if err := utils.CopyFS(l.datadir, os.DirFS(constants.RestoreDir)); err != nil { | ||
return fmt.Errorf("could not copy contents: %w", err) | ||
} | ||
|
||
l.log.Debug("Successfully restored localfs") | ||
return nil | ||
} | ||
|
||
func (l *LocalFS) Probe(ctx context.Context) error { | ||
//Nothing to do, not a real Database | ||
return nil | ||
} | ||
|
||
func (_ *LocalFS) Upgrade(ctx context.Context) error { | ||
// Nothing to do here | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# THESE EXAMPLES ARE GENERATED! | ||
# Use them as a template for your deployment, but do not commit manual changes to these files. | ||
--- | ||
apiVersion: apps/v1 | ||
kind: StatefulSet | ||
metadata: | ||
creationTimestamp: null | ||
labels: | ||
app: localfs | ||
name: localfs | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: localfs | ||
serviceName: localfs | ||
template: | ||
metadata: | ||
creationTimestamp: null | ||
labels: | ||
app: localfs | ||
spec: | ||
containers: | ||
- command: | ||
- backup-restore-sidecar | ||
- wait | ||
image: alpine:3.19 | ||
name: localfs | ||
resources: {} | ||
volumeMounts: | ||
- mountPath: /data | ||
name: data | ||
- mountPath: /usr/local/bin/backup-restore-sidecar | ||
name: bin-provision | ||
subPath: backup-restore-sidecar | ||
- mountPath: /etc/backup-restore-sidecar | ||
name: backup-restore-sidecar-config | ||
- command: | ||
- backup-restore-sidecar | ||
- start | ||
- --log-level=debug | ||
image: alpine:3.19 | ||
name: backup-restore-sidecar | ||
ports: | ||
- containerPort: 8000 | ||
name: grpc | ||
resources: {} | ||
volumeMounts: | ||
- mountPath: /backup | ||
name: backup | ||
- mountPath: /data | ||
name: data | ||
- mountPath: /etc/backup-restore-sidecar | ||
name: backup-restore-sidecar-config | ||
- mountPath: /usr/local/bin/backup-restore-sidecar | ||
name: bin-provision | ||
subPath: backup-restore-sidecar | ||
initContainers: | ||
- command: | ||
- cp | ||
- /backup-restore-sidecar | ||
- /bin-provision | ||
image: ghcr.io/metal-stack/backup-restore-sidecar:latest | ||
imagePullPolicy: IfNotPresent | ||
name: backup-restore-sidecar-provider | ||
resources: {} | ||
volumeMounts: | ||
- mountPath: /bin-provision | ||
name: bin-provision | ||
volumes: | ||
- name: data | ||
persistentVolumeClaim: | ||
claimName: data | ||
- name: backup | ||
persistentVolumeClaim: | ||
claimName: backup | ||
- configMap: | ||
name: backup-restore-sidecar-config-localfs | ||
name: backup-restore-sidecar-config | ||
- emptyDir: {} | ||
name: bin-provision | ||
updateStrategy: {} | ||
volumeClaimTemplates: | ||
- metadata: | ||
creationTimestamp: null | ||
name: data | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
resources: | ||
requests: | ||
storage: 1Gi | ||
status: {} | ||
- metadata: | ||
creationTimestamp: null | ||
name: backup | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
resources: | ||
requests: | ||
storage: 1Gi | ||
status: {} | ||
status: | ||
availableReplicas: 0 | ||
replicas: 0 | ||
--- | ||
apiVersion: v1 | ||
data: | ||
config.yaml: | | ||
--- | ||
bind-addr: 0.0.0.0 | ||
db: localfs | ||
db-data-directory: /data/ | ||
backup-provider: local | ||
backup-cron-schedule: "*/1 * * * *" | ||
object-prefix: localfs-test | ||
redis-addr: localhost:6379 | ||
post-exec-cmds: | ||
- tail -f /etc/hosts | ||
kind: ConfigMap | ||
metadata: | ||
creationTimestamp: null | ||
name: backup-restore-sidecar-config-localfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build integration | ||
|
||
package integration_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/metal-stack/backup-restore-sidecar/pkg/generate/examples/examples" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Localfs_Restore(t *testing.T) { | ||
restoreFlow(t, &flowSpec{ | ||
databaseType: examples.Localfs, | ||
sts: examples.LocalfsSts, | ||
backingResources: examples.LocalfsBackingResources, | ||
addTestData: addLocalfsTestData, | ||
verifyTestData: verifyLocalfsTestData, | ||
}) | ||
} | ||
|
||
func addLocalfsTestData(t *testing.T, ctx context.Context) { | ||
namespace := namespaceName(t) | ||
_, err := execCommand(ctx, "localfs-0", namespace, "backup-restore-sidecar", []string{"sh", "-c", "echo 'I am precious' > /data/test.txt"}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func verifyLocalfsTestData(t *testing.T, ctx context.Context) { | ||
namespace := namespaceName(t) | ||
resp, err := execCommand(ctx, "localfs-0", namespace, "backup-restore-sidecar", []string{"cat", "/data/test.txt"}) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "I am precious", resp) | ||
} |
Oops, something went wrong.