From 9c7ddc662abfae97c03846620bf63ac1d743aaf0 Mon Sep 17 00:00:00 2001 From: GokulGopa Date: Fri, 26 Apr 2024 11:52:10 +0400 Subject: [PATCH] updates for backup and restore for upgrades --- backup-restore/README.md | 33 +++++ backup-restore/backup-4-20-4.sh | 182 ++++++++++++++++++++++++++ backup-restore/backup-mongo-5-0-15.sh | 182 ++++++++++++++++++++++++++ backup-restore/restore-4-20-4.sh | 62 +++++++++ backup-restore/restore-5-0-15.sh | 62 +++++++++ 5 files changed, 521 insertions(+) create mode 100644 backup-restore/backup-4-20-4.sh create mode 100644 backup-restore/backup-mongo-5-0-15.sh create mode 100644 backup-restore/restore-4-20-4.sh create mode 100644 backup-restore/restore-5-0-15.sh diff --git a/backup-restore/README.md b/backup-restore/README.md index 29972fa..44d9726 100644 --- a/backup-restore/README.md +++ b/backup-restore/README.md @@ -8,6 +8,16 @@ This folder contains two essential scripts to help manage MongoDB backups effici 2. **restore.sh**: Utilizes `mongorestore` to restore all MongoDB databases. The script requires one argument: the path where all the dump files are stored. +3. **backup-mongo-5-0-15.sh**: Similar to backup-mongo.sh but this script is used after upgrading Mongodb to 5.0.15 but while running the script, from now on mention the path as well to store the backup. + +4. **restore-5-0-15.sh**: Similar to restore.sh but this script is used after upgrading Mongodb to 5.0.15 + +5. **backup-4-20-4.sh**: Like the backup scripts previously discussed, this script also specifies the path for storing backups. However, it is specifically designed to back up the additional databases, Policies-nirmata and TimeSeries-Metrics-nirmata, following the upgrade to Nirmata 4.20.4. + +6. **restore-4-20-4.sh**: Similar to the restore scripts mentioned but this is used to restore the additional databases *Policies-nirmata* and *TimeSeries-Metrics-nirmata* after upgrading Nirmata to 4.20.4 + + + ## Usage ### Backup @@ -15,8 +25,31 @@ To back up the databases, use the following command: ```sh ./backup-mongo.sh ``` + +To back up the databases (Mongodb 5.0.15) and mention the path as well to store the backup, use the following command: +```sh +./backup-mongo-5-0-15.sh +``` + +To back up the databases (Nirmata 4.20.4) and mention the path as well to store the backup, use the following command: +```sh +./backup-4-20-4.sh +``` + + + ### Restore To restore the databases, use the following command with the appropriate path: ```sh ./restore.sh ``` + +To restore the databases (Mongodb 5.0.15), use the following command with the appropriate path: +```sh +./restore-4-20-4.sh +``` + +To restore the databases (Nirmata 4.20.4), use the following command with the appropriate path: +```sh +./restore-5-0-15.sh +``` \ No newline at end of file diff --git a/backup-restore/backup-4-20-4.sh b/backup-restore/backup-4-20-4.sh new file mode 100644 index 0000000..3011cfc --- /dev/null +++ b/backup-restore/backup-4-20-4.sh @@ -0,0 +1,182 @@ +#!/bin/bash + +#set -x + + +showcollectionscount() { + +#unix_timestamp=$(($(date +%s%N)/1000000)) + +cat << EOF > commands_${1}.js +use $1 +var timestamp = $unix_timestamp; +db.getCollectionNames().forEach(function(col) { +var size = db[col].find({"createdOn":{\$lt:timestamp}}).count(); +print(col+":"+size); +}) +EOF + + +} + + +getdb_version() { + +cat << EOF > dbVersion_${1}.js +use $1 +db.DBVERSION.find() +EOF + +} + +drop_db() { + +cat << EOF > dropdb_${1}.js +use $1 +db.dropDatabase() +EOF + +} + +## main +#input location of backup +backup_location=$1 + +# List all mongo pods +mongos="mongodb-0 mongodb-1 mongodb-2" + +for mongo in $mongos +do + # Adjust the command below with authentication details if necessary + cur_mongo=$(kubectl -n nirmata exec $mongo -c mongodb -- mongo --quiet --eval "printjson(rs.isMaster())" 2>&1) + + if echo "$cur_mongo" | grep -q '"ismaster" : true'; then + echo "$mongo is master" + mongo_master=$mongo + break # Assuming you only need one master, exit loop after finding it + fi +done + +if [ -n "$mongo_master" ]; then + echo "The primary MongoDB replica is: $mongo_master" +else + echo "No primary MongoDB replica found." + exit 1 # It seems this exit was intended to be here to halt the script if no master is found. +fi + +MONGO_MASTER=$mongo_master + +NIRMATA_SERVICES="Activity-nirmata Availability-cluster-hc-nirmata Availability-config-env-nirmata Availability-env-app-nirmata Catalog-nirmata Cluster-nirmata Config-nirmata Environments-nirmata Policies-nirmata TimeSeries-Metrics-nirmata TimeSeries-nirmata Users-nirmata" +# NIRMATA_SERVICES="Activity-nirmata" + +NIRMATA_HOST_BACKUP_FOLDER=/tmp/backup-nirmata +NIRMATA_POD_BACKUP_FOLDER=/tmp/nirmata-backups + +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- mkdir -p $NIRMATA_POD_BACKUP_FOLDER +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- rm -rf $NIRMATA_POD_BACKUP_FOLDER/* +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- mkdir -p $NIRMATA_POD_BACKUP_FOLDER/logs +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- touch $NIRMATA_POD_BACKUP_FOLDER/logs/backup-status.log + +# convert current date into unix time. +unix_timestamp=$(($(date +%s%N)/1000000)) + +mkdir -p $NIRMATA_HOST_BACKUP_FOLDER/$(date +%m-%d-%y)/$(date +"%H-%M") + +BACKUP_DIR="$NIRMATA_HOST_BACKUP_FOLDER/$(date +%m-%d-%y)/$(date +"%H-%M")" + +#echo $unix_timestamp + +for nsvc in $NIRMATA_SERVICES +do + echo + echo "-------------------------------------------------------" + echo "Backing up $nsvc db using mongodump" + echo "-------------------------------------------------------" + echo + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- touch $NIRMATA_POD_BACKUP_FOLDER/logs/${nsvc}_backup.log + sleep 2 + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongodump --gzip --db=$nsvc --archive=$NIRMATA_POD_BACKUP_FOLDER/$nsvc.gz 2>&1 | tee -a $NIRMATA_POD_BACKUP_FOLDER/logs/${nsvc}_backup.log" + if [[ $? != 0 ]]; then + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "echo \"Could not backup $nsvc database on $(date)\" | tee -a $NIRMATA_POD_BACKUP_FOLDER/logs/backup-status.log" + else + echo + file1="" + file2="" + file3="" + file4="" + + file1="$BACKUP_DIR/${nsvc}_objcount.txt" + file2="$BACKUP_DIR/${nsvc}-test_objcount.txt" + file3="$BACKUP_DIR/${nsvc}_dbVersion.txt" + file4="$BACKUP_DIR/${nsvc}-test_dbVersion.txt" + + showcollectionscount $nsvc + kubectl -n nirmata cp commands_${nsvc}.js $MONGO_MASTER:/tmp/ -c mongodb + + getdb_version $nsvc + kubectl -n nirmata cp dbVersion_${nsvc}.js $MONGO_MASTER:/tmp/ -c mongodb + + echo + echo "-------------------------------------------------------------------------" + echo "Displaying collection count for ${nsvc} database " + echo "-------------------------------------------------------------------------" + echo + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/commands_${nsvc}.js" | grep -v "switched to db ${nsvc}" > $file1 + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/dbVersion_${nsvc}.js" | grep -v "switched to db ${nsvc}" > $file3 + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongorestore --drop --gzip --archive=$NIRMATA_POD_BACKUP_FOLDER/$nsvc.gz --nsFrom "${nsvc}.*" --nsTo "${nsvc}-test.*" --noIndexRestore --nsInclude "${nsvc}.*"" + + showcollectionscount ${nsvc}-test + kubectl -n nirmata cp commands_${nsvc}-test.js $MONGO_MASTER:/tmp/ + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/commands_${nsvc}-test.js" | grep -v "switched to db ${nsvc}-test" > $file2 + #echo + #echo "-------------------------------------------------------------------------" + #echo "Displaying collection count for ${nsvc}-test database " + #echo "-------------------------------------------------------------------------" + #echo + + + getdb_version ${nsvc}-test + kubectl -n nirmata cp dbVersion_${nsvc}-test.js $MONGO_MASTER:/tmp/ -c mongodb + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/dbVersion_${nsvc}-test.js" | grep -v "switched to db ${nsvc}-test" > $file4 + + drop_db ${nsvc}-test + kubectl -n nirmata cp dropdb_${nsvc}-test.js $MONGO_MASTER:/tmp/ -c mongodb + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/dropdb_${nsvc}-test.js" + + + diff_output="$(diff --brief "$file1" "$file2")" + + # check if there's any output from the diff command + if [ -n "$diff_output" ]; then + echo "There is a difference between the files. The backup file could be corrupted as the file count between the ${nsvc} and the ${nsvc}-test databases does not match before and after " >> $BACKUP_DIR/dbvrsn_objcnt.log + fi + + diff_output2="$(diff --brief "$file3" "$file4")" + + # check if there's any output from the diff command + if [ -n "$diff_output2" ]; then + echo "The dbVersion does not seem to match between the the ${nsvc} and the ${nsvc}-test databases" >> $BACKUP_DIR/dbvrsn_objcnt.log + fi + + + + fi +done + +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "cd /tmp; tar cvf nirmata-backups.tar nirmata-backups/" + +kubectl -n nirmata cp $MONGO_MASTER:/tmp/nirmata-backups.tar -c mongodb $BACKUP_DIR/nirmata-backups.tar + +tar -xvf $BACKUP_DIR/nirmata-backups.tar -C $BACKUP_DIR + +# echo "nirmata-backups.tar extracted to: $BACKUP_DIR/nirmata-backups at $(date)" > nirmata_backup_directory_path_details.txt + +kubectl -n nirmata cp $MONGO_MASTER:$NIRMATA_POD_BACKUP_FOLDER/logs/backup-status.log -c mongodb $BACKUP_DIR/backup-status.log + +mv *.js /tmp + + +# Copy over the contents. +cp -r $NIRMATA_HOST_BACKUP_FOLDER/* "$backup_location" +# Remove the source directory after successful copy. +rm -rf $NIRMATA_HOST_BACKUP_FOLDER \ No newline at end of file diff --git a/backup-restore/backup-mongo-5-0-15.sh b/backup-restore/backup-mongo-5-0-15.sh new file mode 100644 index 0000000..ae4a693 --- /dev/null +++ b/backup-restore/backup-mongo-5-0-15.sh @@ -0,0 +1,182 @@ +#!/bin/bash + +#set -x + + +showcollectionscount() { + +#unix_timestamp=$(($(date +%s%N)/1000000)) + +cat << EOF > commands_${1}.js +use $1 +var timestamp = $unix_timestamp; +db.getCollectionNames().forEach(function(col) { +var size = db[col].find({"createdOn":{\$lt:timestamp}}).count(); +print(col+":"+size); +}) +EOF + + +} + + +getdb_version() { + +cat << EOF > dbVersion_${1}.js +use $1 +db.DBVERSION.find() +EOF + +} + +drop_db() { + +cat << EOF > dropdb_${1}.js +use $1 +db.dropDatabase() +EOF + +} + +## main +#input location of backup +backup_location=$1 + +# List all mongo pods +mongos="mongodb-0 mongodb-1 mongodb-2" + +for mongo in $mongos +do + # Adjust the command below with authentication details if necessary + cur_mongo=$(kubectl -n nirmata exec $mongo -c mongodb -- mongo --quiet --eval "printjson(rs.isMaster())" 2>&1) + + if echo "$cur_mongo" | grep -q '"ismaster" : true'; then + echo "$mongo is master" + mongo_master=$mongo + break # Assuming you only need one master, exit loop after finding it + fi +done + +if [ -n "$mongo_master" ]; then + echo "The primary MongoDB replica is: $mongo_master" +else + echo "No primary MongoDB replica found." + exit 1 # It seems this exit was intended to be here to halt the script if no master is found. +fi + +MONGO_MASTER=$mongo_master + +NIRMATA_SERVICES="Activity-nirmata Availability-cluster-hc-nirmata Availability-config-env-nirmata Availability-env-app-nirmata Catalog-nirmata Cluster-nirmata Config-nirmata Environments-nirmata Users-nirmata TimeSeries-nirmata" +# NIRMATA_SERVICES="Activity-nirmata" + +NIRMATA_HOST_BACKUP_FOLDER=/tmp/backup-nirmata +NIRMATA_POD_BACKUP_FOLDER=/tmp/nirmata-backups + +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- mkdir -p $NIRMATA_POD_BACKUP_FOLDER +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- rm -rf $NIRMATA_POD_BACKUP_FOLDER/* +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- mkdir -p $NIRMATA_POD_BACKUP_FOLDER/logs +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- touch $NIRMATA_POD_BACKUP_FOLDER/logs/backup-status.log + +# convert current date into unix time. +unix_timestamp=$(($(date +%s%N)/1000000)) + +mkdir -p $NIRMATA_HOST_BACKUP_FOLDER/$(date +%m-%d-%y)/$(date +"%H-%M") + +BACKUP_DIR="$NIRMATA_HOST_BACKUP_FOLDER/$(date +%m-%d-%y)/$(date +"%H-%M")" + +#echo $unix_timestamp + +for nsvc in $NIRMATA_SERVICES +do + echo + echo "-------------------------------------------------------" + echo "Backing up $nsvc db using mongodump" + echo "-------------------------------------------------------" + echo + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- touch $NIRMATA_POD_BACKUP_FOLDER/logs/${nsvc}_backup.log + sleep 2 + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongodump --gzip --db=$nsvc --archive=$NIRMATA_POD_BACKUP_FOLDER/$nsvc.gz 2>&1 | tee -a $NIRMATA_POD_BACKUP_FOLDER/logs/${nsvc}_backup.log" + if [[ $? != 0 ]]; then + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "echo \"Could not backup $nsvc database on $(date)\" | tee -a $NIRMATA_POD_BACKUP_FOLDER/logs/backup-status.log" + else + echo + file1="" + file2="" + file3="" + file4="" + + file1="$BACKUP_DIR/${nsvc}_objcount.txt" + file2="$BACKUP_DIR/${nsvc}-test_objcount.txt" + file3="$BACKUP_DIR/${nsvc}_dbVersion.txt" + file4="$BACKUP_DIR/${nsvc}-test_dbVersion.txt" + + showcollectionscount $nsvc + kubectl -n nirmata cp commands_${nsvc}.js $MONGO_MASTER:/tmp/ -c mongodb + + getdb_version $nsvc + kubectl -n nirmata cp dbVersion_${nsvc}.js $MONGO_MASTER:/tmp/ -c mongodb + + echo + echo "-------------------------------------------------------------------------" + echo "Displaying collection count for ${nsvc} database " + echo "-------------------------------------------------------------------------" + echo + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/commands_${nsvc}.js" | grep -v "switched to db ${nsvc}" > $file1 + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/dbVersion_${nsvc}.js" | grep -v "switched to db ${nsvc}" > $file3 + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongorestore --drop --gzip --archive=$NIRMATA_POD_BACKUP_FOLDER/$nsvc.gz --nsFrom "${nsvc}.*" --nsTo "${nsvc}-test.*" --noIndexRestore --nsInclude "${nsvc}.*"" + + showcollectionscount ${nsvc}-test + kubectl -n nirmata cp commands_${nsvc}-test.js $MONGO_MASTER:/tmp/ + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/commands_${nsvc}-test.js" | grep -v "switched to db ${nsvc}-test" > $file2 + #echo + #echo "-------------------------------------------------------------------------" + #echo "Displaying collection count for ${nsvc}-test database " + #echo "-------------------------------------------------------------------------" + #echo + + + getdb_version ${nsvc}-test + kubectl -n nirmata cp dbVersion_${nsvc}-test.js $MONGO_MASTER:/tmp/ -c mongodb + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/dbVersion_${nsvc}-test.js" | grep -v "switched to db ${nsvc}-test" > $file4 + + drop_db ${nsvc}-test + kubectl -n nirmata cp dropdb_${nsvc}-test.js $MONGO_MASTER:/tmp/ -c mongodb + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongo --quiet < /tmp/dropdb_${nsvc}-test.js" + + + diff_output="$(diff --brief "$file1" "$file2")" + + # check if there's any output from the diff command + if [ -n "$diff_output" ]; then + echo "There is a difference between the files. The backup file could be corrupted as the file count between the ${nsvc} and the ${nsvc}-test databases does not match before and after " >> $BACKUP_DIR/dbvrsn_objcnt.log + fi + + diff_output2="$(diff --brief "$file3" "$file4")" + + # check if there's any output from the diff command + if [ -n "$diff_output2" ]; then + echo "The dbVersion does not seem to match between the the ${nsvc} and the ${nsvc}-test databases" >> $BACKUP_DIR/dbvrsn_objcnt.log + fi + + + + fi +done + +kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "cd /tmp; tar cvf nirmata-backups.tar nirmata-backups/" + +kubectl -n nirmata cp $MONGO_MASTER:/tmp/nirmata-backups.tar -c mongodb $BACKUP_DIR/nirmata-backups.tar + +tar -xvf $BACKUP_DIR/nirmata-backups.tar -C $BACKUP_DIR + +# echo "nirmata-backups.tar extracted to: $BACKUP_DIR/nirmata-backups at $(date)" > nirmata_backup_directory_path_details.txt + +kubectl -n nirmata cp $MONGO_MASTER:$NIRMATA_POD_BACKUP_FOLDER/logs/backup-status.log -c mongodb $BACKUP_DIR/backup-status.log + +mv *.js /tmp + + +# Copy over the contents. +cp -r $NIRMATA_HOST_BACKUP_FOLDER/* "$backup_location" +# Remove the source directory after successful copy. +rm -rf $NIRMATA_HOST_BACKUP_FOLDER \ No newline at end of file diff --git a/backup-restore/restore-4-20-4.sh b/backup-restore/restore-4-20-4.sh new file mode 100644 index 0000000..79fc199 --- /dev/null +++ b/backup-restore/restore-4-20-4.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +## main + +if kubectl get pods -n nirmata --no-headers | egrep -v 'mongo|zk|kafka' 1> /dev/null; then + echo -e "\nPlease scale down non shared services before performing the restore\n" + exit 1 +fi + +if [[ $# != 1 ]]; then + echo -e "\nUsage: $0 \n" + exit 1 +fi + +# List all mongo pods +mongos="mongodb-0 mongodb-1 mongodb-2" + +for mongo in $mongos +do + cur_mongo=$(kubectl -n nirmata exec $mongo -c mongodb -- sh -c 'echo "db.serverStatus()" |mongo' 2>&1|grep '"ismaster"') + if [[ $cur_mongo =~ "true" ]];then + echo "$mongo is master" + mongo_master=$mongo + fi +done + +MONGO_MASTER=$mongo_master + +if [[ -z $MONGO_MASTER ]]; then + echo "Unable to find the mongo master. Please check the mongo cluster. Exiting!" + exit 1 +fi + +rm -f /tmp/restore-status.txt +touch /tmp/restore-status.txt + +backupfolder=$1 + +# Get the list of all databases +mongodbs="Activity-nirmata Availability-cluster-hc-nirmata Availability-config-env-nirmata Availability-env-app-nirmata Catalog-nirmata Cluster-nirmata Config-nirmata Environments-nirmata Policies-nirmata TimeSeries-Metrics-nirmata TimeSeries-nirmata Users-nirmata" + +# For each database +for db in $mongodbs; do + + # Copy the backup file to the MongoDB pod + kubectl -n nirmata cp $backupfolder/${db}.gz $MONGO_MASTER:/tmp/${db}.gz -c mongodb + + # Connect to the MongoDB pod and restore the database + + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongorestore --drop --gzip --db=${db} --archive=/tmp/${db}.gz --noIndexRestore -v" + + # Check the status of the restore + if [ $? -eq 0 ]; then + echo "Database ${db} restored successfully" | tee -a /tmp/restore-status.txt + else + echo "Database ${db} restore failed" | tee -a /tmp/restore-status.txt + fi + + # Delete the backup file + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "rm -f /tmp/${db}.gz" + +done \ No newline at end of file diff --git a/backup-restore/restore-5-0-15.sh b/backup-restore/restore-5-0-15.sh new file mode 100644 index 0000000..3ba1920 --- /dev/null +++ b/backup-restore/restore-5-0-15.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +## main + +if kubectl get pods -n nirmata --no-headers | egrep -v 'mongo|zk|kafka' 1> /dev/null; then + echo -e "\nPlease scale down non shared services before performing the restore\n" + exit 1 +fi + +if [[ $# != 1 ]]; then + echo -e "\nUsage: $0 \n" + exit 1 +fi + +# List all mongo pods +mongos="mongodb-0 mongodb-1 mongodb-2" + +for mongo in $mongos +do + cur_mongo=$(kubectl -n nirmata exec $mongo -c mongodb -- sh -c 'echo "db.serverStatus()" |mongo' 2>&1|grep '"ismaster"') + if [[ $cur_mongo =~ "true" ]];then + echo "$mongo is master" + mongo_master=$mongo + fi +done + +MONGO_MASTER=$mongo_master + +if [[ -z $MONGO_MASTER ]]; then + echo "Unable to find the mongo master. Please check the mongo cluster. Exiting!" + exit 1 +fi + +rm -f /tmp/restore-status.txt +touch /tmp/restore-status.txt + +backupfolder=$1 + +# Get the list of all databases +mongodbs="Activity-nirmata Availability-cluster-hc-nirmata Availability-env-app-nirmata Catalog-nirmata Cluster-nirmata Config-nirmata Environments-nirmata Users-nirmata TimeSeries-nirmata" + +# For each database +for db in $mongodbs; do + + # Copy the backup file to the MongoDB pod + kubectl -n nirmata cp $backupfolder/${db}.gz $MONGO_MASTER:/tmp/${db}.gz -c mongodb + + # Connect to the MongoDB pod and restore the database + + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "mongorestore --drop --gzip --db=${db} --archive=/tmp/${db}.gz --noIndexRestore -v" + + # Check the status of the restore + if [ $? -eq 0 ]; then + echo "Database ${db} restored successfully" | tee -a /tmp/restore-status.txt + else + echo "Database ${db} restore failed" | tee -a /tmp/restore-status.txt + fi + + # Delete the backup file + kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "rm -f /tmp/${db}.gz" + +done \ No newline at end of file