This is the continuation from my previous post where I had explained how to run spring boot app inside the docker container as daemon which is using MongoDB as storage and the [/data/db] volume was mounted as docker container volume.
This post will provide the steps to take the backup of volume container and restore it back.
Step 1: Take the backup via tar command provided below.
docker run --volumes-from mongodata -v $(pwd):/backup busybox tar cvf /backup/backup.tar /data/db kup.tar /data/db tar: removing leading '/' from member names data/db/ data/db/local.ns data/db/TEST_DB.ns data/db/mongod.lock data/db/journal/ data/db/TEST_DB.0 data/db/local.0
- This is how it works first the volume from mongodata [/data/db] is mounted .
- The current working directory [$(pwd)] of host is mounted as volume with path [/backup] directory inside container.
- The tar command is executed inside the container on /data/db directory and the backup.tar is copied to [/backup]
- Execute the ls -l command from the current directory of the host you will notice the backup.tar file.
Step 2: Restore the backup via tar command provide below.
docker run --volumes-from mongodata -v $(pwd):/backup busybox tar xvf /backup/backup.tar
- This is how it works first the volume from mongodata [/data/db] is mounted .
- The current working directory [$(pwd)] of host is mounted as volume with path [/backup] directory inside container.
- The tar command is executed inside the container on /backup/backup.tar file and the it contents are extracted to [/data/db] directory (this is mounted via volume container which is mongodata).
Step 3 : List the contents
docker run --rm --volumes-from mongodata busybox ls -lh /data/db
- Notice the –rm option.This command will execute and spawn the temporary docker container mount the volume list the [/data/db] directory .
- After the command is executed the temporary docker container will be automatically removed.
Copy existing folder from host to docker volume container.
First of all the credit must be given to the person who has this covered already on his blog here. but I thought it is worth mentioning it again here because we are covering the topic of backup and restore.
Step 1. Create volume only docker container named as testdata.
docker run -v /test/data --name testdata busybox true
Step 2. Copy the folder named as sample from current host directory to volume container.
tar -c sample/ | docker run -i --rm -w /test/data --volumes-from testdata busybox tar -xv
- The tar command pipes the content of sample folder into the docker volume container directory [/test/data].
- You can use the ls command like we did earlier change the mongodata to testdata and [/data/db] to [/test/data]
- All the contents from sample directory copied will be listed.
Please feel free to notify if you encounter any issues.I am always learning :).
Thank You!!!