New to Stash? Please start here.
An entire backup or restore process needs an ordered execution of one or more steps. A Function represents a step of a backup or restore process. A Task is a Kubernetes CustomResourceDefinition(CRD) which specifies a sequence of functions along with their parameters in a Kubernetes native way.
When you install Stash, some Tasks will be pre-installed for supported targets like databases, etc. However, you can create your own Task to customize or extend the backup/restore process. Stash will execute these steps in the order you have specified.
Like any official Kubernetes resource, a Task has TypeMeta, ObjectMeta and Spec sections. However, unlike other Kubernetes resources, it does not have a Status section.
A sample Task object to backup a PostgreSQL database is shown below:
apiVersion: stash.appscode.com/v1beta1
kind: Task
metadata:
name: postgres-backup-11.2
spec:
steps:
- name: postgres-backup-11.2
params:
- name: outputDir # specifies where to write output file
value: /tmp/output
- name: secretVolume # specifies where backend secret has been mounted
value: secret-volume
- name: update-status
params:
- name: outputDir # specifies where previous step wrote output file. it will read that file and update status of respective resources accordingly.
value: /tmp/output
volumes:
- name: secret-volume
secret:
secretName: ${REPOSITORY_SECRET_NAME}
This Task uses two functions to backup a PostgreSQL database. The first step uses postgres-backup-11.2 function that dumps PostgreSQL database and uploads the dumped file. The second step uses update-status function which updates the status of the BackupSession and Repository crd for respective backup.
Here, we are going to describe the various sections of a Task crd.
SpecA Task object has the following fields in the spec section:
spec.steps section specifies a list of functions and their parameters in the order they should be executed. You can also templatize this section using the variables that Stash can resolve itself. Stash will resolve all the variables and create a pod definition with a container specification for each Function specified in steps section.
Each step consists of the following fields:
name specifies the name of the Function that will be executed at this step.params specifies an optional list of variables names and their values that Stash should use to resolve the respective Function. If you use a variable in a Function specification whose value Stash cannot provide, you can pass the value of that variable using this params section. You have to specify the following fields for a variable:
name of the variable.In the above example Task, we have used outputDir variable in postgres-backup-11.2 function that Stash can’t resolve automatically. So, we have passed the value using the params section in the Task object.
Stash executes the
Functionsin the order they appear inspec.stepssection. All the functions except the last one will be used to createinit-containerspecification and the last function will be used to createcontainerspecification for respective backup job. This guarantees an ordered execution of the steps.
spec.volumes specifies a list of volumes that should be mounted in the respective job created for this Task. In the sample we have shown above, we need to mount storage secret for the backup job. So, we have added the secret volume in spec.volumes section. Note that, we have used REPOSITORY_SECRET_NAME variable as secret name. This variable will be resolved by Stash from Repository specification.
You might be wondering why we have introduced Function and Task crd. We have designed Function-Task model for the following reasons:
Customizability: Function and Task enables you to customize backup/recovery process. For example, currently we use mysqldump in mysql-backup Function to backup MySQL database. You can build a custom Function using Percona’s xtrabackup tool instead of mysqldump. Then you can write a Task with this custom Function and use it to backup your target MySQL database.
You can also customize backup/restore process by executing hooks before or after the backup/restore process. For example, if you want to execute some logic to prepare your apps for backup or you want to send an email notification after each backup, you just need to add Function with your custom logic and respective Task to execute them.
Extensibility: Currently, Stash supports backup of MySQL, MongoDB and PostgreSQL databases. You can easily backup the databases that are not officially supported by Stash. You just need to create a Function and a Task for your desired database.
Re-usability: Function's are self-sufficient and independent of Stash. So, you can reuse them in any application that uses Function-Task model.