You are looking at the documentation of a prior release. To read the documentation of the latest release, please
visit here.
New to KubeDB? Please start here.
MongoDB Backup & Restore Overview
KubeDB can also uses KubeStash to backup and restore databases. KubeStash by AppsCode is a cloud native data backup and recovery solution for Kubernetes workloads. KubeStash utilizes restic to securely backup stateful applications to any cloud or on-prem storage backends (for example, S3, GCS, Azure Blob storage, Minio, NetApp, Dell EMC etc.).
How Backup Works
The following diagram shows how KubeStash takes backup of a MongoDB database. Open the image in a new tab to see the enlarged version.
The backup process consists of the following steps:
At first, a user creates a secret with access credentials of the backend where the backed up data will be stored.
Then, she creates a
BackupStoragecrd that specifies the backend information along with the secret that holds the credentials to access the backend.Then, she creates a
BackupConfigurationcrd targeting the crd of the desiredMongoDBdatabase. TheBackupConfigurationobject also specifies theSessionsto use to backup the database.KubeStash operator watches for
BackupConfigurationcrd.Once KubeStash operator finds a
BackupConfigurationcrd, it creates a CronJob with the schedule specified inBackupConfigurationobject to trigger backup periodically.On the next scheduled slot, the CronJob triggers a backup by creating a
BackupSessioncrd.KubeStash operator also watches for
BackupSessioncrd.When it finds a
BackupSessionobject, It creates aSnapshotfor holding backup information.KubeStash resolves the respective
AddonandFunctionand prepares a Job definition to backup.Then, it creates the Job to backup the targeted database.
The backup Job reads necessary information to connect with the database from the
AppBindingcrd of the targatedMongoDBdatabase. It also reads backend information and access credentials fromBackupStoragecrd and Storage Secret respectively throughBackendsection ofBackupConfigurationcrdThen, the Job dumps the targeted database and uploads the output to the backend. KubeStash pipes the output of dump command to uploading process. Hence, backup Job does not require a large volume to hold the entire dump output.
Finally, when the backup is complete, the Job sends Prometheus metrics to the Pushgateway running inside KubeStash operator pod. It also updates the
BackupSessionandSnapshotstatus to reflect the backup procedure.
Backup Different MongoDB Configurations
This section will show you how backup works for different MongoDB configurations.
Standalone MongoDB
For a standalone MongoDB database, the backup job directly dumps the database using mongodump and pipe the output to the backup process.
MongoDB ReplicaSet Cluster
For MongoDB ReplicaSet cluster, KubeStash takes backup from one of the secondary replicas. The backup process consists of the following steps:
- Identify a secondary replica.
- Lock the secondary replica.
- Backup the secondary replica.
- Unlock the secondary replica.
MongoDB Sharded Cluster
For MongoDB sharded cluster, KubeStash takes backup of the individual shards as well as the config server. KubeStash takes backup from a secondary replica of the shards and the config server. If there is no secondary replica then KubeStash will take backup from the primary replica. The backup process consists of the following steps:
- Disable balancer.
- Lock config server.
- Identify a secondary replica for each shard.
- Lock the secondary replica.
- Run backup on the secondary replica.
- Unlock the secondary replica.
- Unlock config server.
- Enable balancer.
How Restore Process Works
The following diagram shows how KubeStash restores backed up data into a MongoDB database. Open the image in a new tab to see the enlarged version.
The restore process consists of the following steps:
At first, a user creates a
RestoreSessioncrd that specifies the target database where the backed-up data will be restored, addon information (including restore tasks), the target snapshot to be restored, the Repository containing that snapshot, and other additional settings.KubeStash operator watches for
RestoreSessionobject.When it finds a
RestoreSessioncustom resource, it resolves the respectiveAddonandFunctionand prepares a restore Job definition.Then, it creates the Job to restore the target.
The Job reads necessary information to connect with the database from respective
AppBindingcrd. It also reads backend information and access credentials fromRepositorycrd and Storage Secret respectively.Then, the job downloads the backed up data from the backend and injects into the desired database. KubeStash pipes the downloaded data to the respective database tool to inject into the database. Hence, restore job does not require a large volume to download entire backup data inside it.
Finally, when the restore process is complete, the Job sends Prometheus metrics to the Pushgateway and update the
RestoreSessionstatus to reflect restore completion.
Restoring Different MongoDB Configurations
This section will show you restore process works for different MongoDB configurations.
Standalone MongoDB
For a standalone MongoDB database, the restore job downloads the backed up data from the backend and pipe the downloaded data to mongorestore command which inserts the data into the desired MongoDB database.
MongoDB ReplicaSet Cluster
For MongoDB ReplicaSet cluster, KubeStash identifies the primary replica and restore into it.
MongoDB Sharded Cluster
For MongoDB sharded cluster, KubeStash identifies the primary replica of each shard as well as the config server and restore respective backed up data into them.






























