Skip to content

Commit

Permalink
Backup and restore PVC (#676)
Browse files Browse the repository at this point in the history
- [x] Merge #674 
- [x] What should be host-name?
- [ ] Set backup/restore session status
  • Loading branch information
Dipta Das authored and tamalsaha committed Mar 20, 2019
1 parent 7bdf075 commit 30993c0
Show file tree
Hide file tree
Showing 14 changed files with 551 additions and 1 deletion.
33 changes: 33 additions & 0 deletions docs/examples/pvc/backup.yaml
@@ -0,0 +1,33 @@
apiVersion: stash.appscode.com/v1beta1
kind: BackupConfiguration
metadata:
name: pvc-backup-config
namespace: demo
spec:
task:
name: pvc-backup-task
repository:
name: hello-repo
schedule: "0 * * * *"
target:
ref:
apiVersion: v1
kind: PersistentVolumeClaim
name: test-pvc-source
mountPath: /etc/target
directories:
- /etc/target/dir-01
- /etc/target/dir-02
retentionPolicy:
keepLast: 5
prune: true

---
apiVersion: stash.appscode.com/v1beta1
kind: BackupSession
metadata:
name: pvc-backup-01
namespace: demo
spec:
backupConfiguration:
name: pvc-backup-config
20 changes: 20 additions & 0 deletions docs/examples/pvc/check.yaml
@@ -0,0 +1,20 @@
kind: Pod
apiVersion: v1
metadata:
name: test-check-dest
namespace: demo
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh","-c"]
args:
- 'set -x; ls /etc/target'
volumeMounts:
- mountPath: /etc/target
name: test-pvc-dest
restartPolicy: Never
volumes:
- name: test-pvc-dest
persistentVolumeClaim:
claimName: test-pvc-dest
69 changes: 69 additions & 0 deletions docs/examples/pvc/functions.yaml
@@ -0,0 +1,69 @@
apiVersion: stash.appscode.com/v1beta1
kind: Function
metadata:
name: pvc-backup
spec:
image: diptadas/stash:pvc
args:
- backup-pvc
- --provider=${REPOSITORY_PROVIDER}
- --bucket=${REPOSITORY_BUCKET=""}
- --endpoint=${REPOSITORY_ENDPOINT=""}
- --path=${REPOSITORY_PREFIX=""}
- --secret-dir=/etc/repository/secret # specified
- --scratch-dir=/tmp/restic/scratch # specified
- --enable-cache=false # specified
- --hostname=${HOSTNAME}
- --backup-dirs=${TARGET_DIRECTORIES}
- --retention-keep-last=${RETENTION_KEEP_LAST=0}
- --retention-keep-hourly=${RETENTION_KEEP_HOURLY=0}
- --retention-keep-daily=${RETENTION_KEEP_DAILY=0}
- --retention-keep-weekly=${RETENTION_KEEP_WEEKLY=0}
- --retention-keep-monthly=${RETENTION_KEEP_MONTHLY=0}
- --retention-keep-yearly=${RETENTION_KEEP_YEARLY=0}
- --retention-keep-tags=${RETENTION_KEEP_TAGS=""}
- --retention-prune=${RETENTION_PRUNE=false}
- --retention-dry-run=${RETENTION_DRY_RUN=false}
- --output-dir=${outputDir}
#- --metrics-enabled
#- --metrics-pushgateway-url
#- --metrics-dir
#- --metrics-labels
volumeMounts:
- name: ${scratchDirVolume}
mountPath: /tmp/restic/scratch
- name: ${targetVolume}
mountPath: ${TARGET_MOUNT_PATH}
- name: ${secretVolume}
mountPath: /etc/repository/secret
---
apiVersion: stash.appscode.com/v1beta1
kind: Function
metadata:
name: pvc-restore
spec:
image: diptadas/stash:pvc
args:
- restore-pvc
- --provider=${REPOSITORY_PROVIDER}
- --bucket=${REPOSITORY_BUCKET=""}
- --endpoint=${REPOSITORY_ENDPOINT=""}
- --path=${REPOSITORY_PREFIX=""}
- --secret-dir=/etc/repository/secret # specified
- --scratch-dir=/tmp/restic/scratch # specified
- --enable-cache=false # specified
- --hostname=${HOSTNAME}
- --restore-dirs=${RESTORE_DIRECTORIES}
- --snapshots=${RESTORE_SNAPSHOTS=""}
- --output-dir=${outputDir}
#- --metrics-enabled
#- --metrics-pushgateway-url
#- --metrics-dir
#- --metrics-labels
volumeMounts:
- name: ${scratchDirVolume}
mountPath: /tmp/restic/scratch
- name: ${targetVolume}
mountPath: ${TARGET_MOUNT_PATH}
- name: ${secretVolume}
mountPath: /etc/repository/secret
37 changes: 37 additions & 0 deletions docs/examples/pvc/pvc.yaml
@@ -0,0 +1,37 @@
kind: PersistentVolume
apiVersion: v1
metadata:
name: test-pv-volume
labels:
type: local
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/tmp/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-pvc-source
namespace: demo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 256M
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-pvc-dest
namespace: demo
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 256M
11 changes: 11 additions & 0 deletions docs/examples/pvc/repo.yaml
@@ -0,0 +1,11 @@
apiVersion: stash.appscode.com/v1alpha1
kind: Repository
metadata:
name: hello-repo
namespace: demo
spec:
backend:
gcs:
bucket: hello-bucket
prefix: hello-prefix
storageSecretName: gcs-secret
22 changes: 22 additions & 0 deletions docs/examples/pvc/restore.yaml
@@ -0,0 +1,22 @@
apiVersion: stash.appscode.com/v1beta1
kind: RestoreSession
metadata:
name: pvc-restore-01
namespace: demo
spec:
task:
name: pvc-restore-task
repository:
name: hello-repo
target:
ref:
apiVersion: v1
kind: PersistentVolumeClaim
name: test-pvc-dest
mountPath: /etc/target
rules:
- hosts:
- host-0
paths:
- /etc/target/dir-01
- /etc/target/dir-02
51 changes: 51 additions & 0 deletions docs/examples/pvc/tasks.yaml
@@ -0,0 +1,51 @@
apiVersion: stash.appscode.com/v1beta1
kind: Task
metadata:
name: pvc-backup-task
spec:
steps:
- name: pvc-backup
params:
- name: outputDir
value: /etc/backup # specified
- name: scratchDirVolume
value: scratch-volume # specified
- name: targetVolume
value: target-volume # specified
- name: secretVolume
value: secret-volume # specified
volumes:
- name: target-volume
persistentVolumeClaim:
claimName: ${TARGET_NAME}
- name: secret-volume
secret:
secretName: ${REPOSITORY_SECRET_NAME}
- name: scratch-volume
emptyDir: {}
---
apiVersion: stash.appscode.com/v1beta1
kind: Task
metadata:
name: pvc-restore-task
spec:
steps:
- name: pvc-restore
params:
- name: outputDir
value: /etc/restore # specified
- name: scratchDirVolume
value: scratch-volume # specified
- name: targetVolume
value: target-volume # specified
- name: secretVolume
value: secret-volume # specified
volumes:
- name: target-volume
persistentVolumeClaim:
claimName: ${TARGET_NAME}
- name: secret-volume
secret:
secretName: ${REPOSITORY_SECRET_NAME}
- name: scratch-volume
emptyDir: {}
20 changes: 20 additions & 0 deletions docs/examples/pvc/write.yaml
@@ -0,0 +1,20 @@
kind: Pod
apiVersion: v1
metadata:
name: test-write-soruce
namespace: demo
spec:
containers:
- name: busybox
image: busybox
command: ["/bin/sh","-c"]
args:
- 'set -x; rm -r /etc/target/*; mkdir /etc/target/dir-01; mkdir /etc/target/dir-02; ls /etc/target'
volumeMounts:
- mountPath: /etc/target
name: test-pvc-source
restartPolicy: Never
volumes:
- name: test-pvc-source
persistentVolumeClaim:
claimName: test-pvc-source
110 changes: 110 additions & 0 deletions docs/guides/backup_restore_pvc.md
@@ -0,0 +1,110 @@
---
title: Backup and Restore PVC | Stash
description: Backup and Restore PVC using Stash
menu:
product_stash_0.8.3:
identifier: backup_restore_pvc
name: Backup and Restore PVC
parent: guides
weight: 15
product_name: stash
menu_name: product_stash_0.8.3
section_menu_id: guides
---

> New to Stash? Please start [here](/docs/concepts/README.md).
# Backup and Restore PVC

In this tutorial we will backup some directories of a persistent volume and restore it in another persistent volume.

## Before You Begin

At first, you need to have a Kubernetes cluster, and the `kubectl` command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using [Minikube](https://github.com/kubernetes/minikube). Then install `Stash` in your cluster following the steps [here](/docs/setup/install.md).

To keep things isolated, we are going to use a separate namespace called `demo` throughout this tutorial.

```console
$ kubectl create ns demo
namespace/demo created
```

>Note: YAML files used in this tutorial are stored in [/docs/examples/pvc](/docs/examples/pvc) directory of [appscode/stash](https://github.com/appscode/stash) repository.
## Create Functions and Tasks

```bash
$ kubectl apply -f /docs/examples/pvc/functions.yaml; kubectl apply -f /docs/examples/pvc/tasks.yaml
function.stash.appscode.com/pvc-backup created
task.stash.appscode.com/pvc-backup-task created
function.stash.appscode.com/pvc-restore created
task.stash.appscode.com/pvc-restore-task created
```

## Create Repository

```bash
$ kubectl create secret generic gcs-secret -n demo --from-file=./RESTIC_PASSWORD --from-file=./GOOGLE_PROJECT_ID --from-file=./GOOGLE_SERVICE_ACCOUNT_JSON_KEY
secret/gcs-secret created
```

```bash
$ kubectl apply -f /docs/examples/pvc/repo.yaml
repository.stash.appscode.com/hello-repo created
```

## Create PVC

Create Persistent Volume and two separate PVC for source and destination.

```bash
$ kubectl apply -f /docs/examples/pvc/pvc.yaml
persistentvolume/test-pv-volume created
persistentvolumeclaim/test-pvc-source created
persistentvolumeclaim/test-pvc-dest created
```

## Write to Source PVC

```bash
$ kubectl apply -f /docs/examples/pvc/write.yaml
pod/test-write-soruce created
```

## Backup Source PVC

```bash
$ kubectl apply -f /docs/examples/pvc/backup.yaml
backupconfiguration.stash.appscode.com/pvc-backup-config created
backupsession.stash.appscode.com/pvc-backup-01 created
```

## Restore to Destination PVC

```bash
$ kubectl apply -f /docs/examples/pvc/restore.yaml
restoresession.stash.appscode.com/pvc-restore-01 created
```

## Check Destination PVC

```bash
$ kubectl apply -f /docs/examples/pvc/check.yaml
pod/test-check-dest created
```

```bash
$ kc logs -n demo -f test-check-dest
+ ls /etc/target
dir-01
dir-02
```

## Cleanup

To cleanup the Kubernetes resources created by this tutorial, run:

```console
$ kubectl delete -f /docs/examples/pvc
$ kubectl delete namespace demo
```
2 changes: 1 addition & 1 deletion hack/docker/setup.sh
Expand Up @@ -14,7 +14,7 @@ source "$REPO_ROOT/hack/libbuild/common/public_image.sh"

APPSCODE_ENV=${APPSCODE_ENV:-dev}
IMG=stash
RESTIC_VER=${RESTIC_VER:-0.8.3}
RESTIC_VER=${RESTIC_VER:-0.9.4}
RESTIC_BRANCH=${RESTIC_BRANCH:-stash-0.4.2}

DIST=$REPO_ROOT/dist
Expand Down

0 comments on commit 30993c0

Please sign in to comment.