Backup
All user data and documents are stored in a mongo database. The other components only store log files and configuration. Mongo provides an easy way to backup the information using the mongodump
command. This also includes any documents and images. You can use a Kubernetes cron job to automatically dump the database at regular intervals. An alternative is to use a shell script and Linux cron job as described below.
If you are just testing the installation or already have a procedure for mongo database backup you may skip this section.
Backup database
Creating a backup can be done by executing a kubernetes exec
command into the application that is deployed. A second command creates a tar
archive, which is then copied to the host system. This file can then be transferred to a backup server using ssh
.
These commands can be added to a file 'backup.sh'.
#!/bin/bash
kubectl exec -it mongo-0 -- mongodump --db idata --out /tmp
kubectl exec -it mongo-0 -- tar -cvf /tmp/idata.tar /tmp/idata
kubectl cp mongo-0:/tmp/idata.tar /tmp/idata.tar'
scp -P 4422 /tmp/idata.tar [USER]@[IP]:[DIRECTORY]
Save the file in a local directory and make sure it has execute rights. Then add the following command to the cron scheduler. In this case a backup is created every hour at 33 minutes past the hour.
chmod 777 backup.sh
crontab -e
33 * * * * /home/devops/bin/backup.sh > /home/devops/bin/backup.log
Restore database
To restore the database you copy the tar file to the deployed mongo instance on kubernetes and unpack and restore from the host system.
kubectl cp /tmp/idata.tar mongo-0:/tmp
kubectl exec -it mongo-0 -- tar -xvf /tmp/idata.tar /tmp/idata
kubectl exec -it mongo-0 -- mongorestore --db idata /tmp/idata