Skip to content

Commit

Permalink
update backup-restore
Browse files Browse the repository at this point in the history
  • Loading branch information
gokulgopa committed Apr 11, 2024
1 parent 066ea93 commit 44e8157
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions backup-restore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ This folder contains two essential scripts to help manage MongoDB backups effici
### Backup
To back up the databases, use the following command:
```sh
./backup-mongo.sh
./backup-mongo.sh <path_to_dump_files>
```
### Restore
To restore the databases, use the following command with the appropriate path:
```sh
./restore.sh <path_to_dump_files>
```
```
25 changes: 21 additions & 4 deletions backup-restore/backup-mongo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,31 @@ 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
cur_mongo=$(kubectl -n nirmata exec $mongo -c mongodb -- sh -c 'echo "db.serverStatus()" |mongo' 2>&1|grep '"ismaster"')
if [[ $cur_mongo =~ "true" ]];then
# 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"
Expand Down Expand Up @@ -158,8 +169,14 @@ kubectl -n nirmata cp $MONGO_MASTER:/tmp/nirmata-backups.tar -c mongodb $BACKUP_

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
# 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
2 changes: 1 addition & 1 deletion backup-restore/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ for db in $mongodbs; do
# Delete the backup file
kubectl -n nirmata exec $MONGO_MASTER -c mongodb -- sh -c "rm -f /tmp/${db}.gz"

done
done

0 comments on commit 44e8157

Please sign in to comment.