Copy mongodump directly from container

This commit is contained in:
tobias 2024-07-16 13:01:41 +02:00
parent 8fa28a31ce
commit e93b5e653a

View File

@ -5,25 +5,26 @@ REMOTE_USER=tma
REMOTE_SERVER=swarm-demo.autonomic.zone REMOTE_SERVER=swarm-demo.autonomic.zone
REMOTE_PORT=222 REMOTE_PORT=222
REMOTE_CONTAINER_PATH=/data/db/mongodump REMOTE_CONTAINER_PATH=/data/db/mongodump
REMOTE_PATH=/tmp/mongodump LOCAL_PATH=/tmp/mongodump
LOCAL_PATH=local_path/mongodump LOCAL_DB=test
LOCAL_DB=your_database
LOCAL_CONTAINER_NAME=nextload-mongo-1 LOCAL_CONTAINER_NAME=nextload-mongo-1
ENV_FILE=.env
include $(ENV_FILE) MONGO_USER=mongo
MONGO_PASSWORD=mongo
MONGO_HOST=mongo
sync_db: sync_db:
@echo "Step 1: Dump the database on production" @echo "Step 1: Dump the database on production"
docker --context $(REMOTE_SERVER) exec -it $$(docker --context $(REMOTE_SERVER) ps -q -f name=nextload-demo_autonomic_zone_mongo) \ docker --context swarm-demo.autonomic.zone exec -it $$(docker --context swarm-demo.autonomic.zone ps -q -f name=nextload-demo_autonomic_zone_mongo) \
bash -c 'mongodump -u mongo -p "$$(cat /run/secrets/mongo_password)" -o $(REMOTE_CONTAINER_PATH)' bash -c 'rm -rf $(REMOTE_CONTAINER_PATH) && mongodump -u mongo -p "$$(cat /run/secrets/mongo_password)" -o $(REMOTE_CONTAINER_PATH) && ls -l $(REMOTE_CONTAINER_PATH)'
@echo "Step 2: Copy the dump from container to remote server" @echo "Step 2: Copy the dump from the remote MongoDB container to the local machine"
docker --context $(REMOTE_SERVER) cp $$(docker --context $(REMOTE_SERVER) ps -q -f name=nextload-demo_autonomic_zone_mongo):$(REMOTE_CONTAINER_PATH) $(REMOTE_PATH) rm -rf $(LOCAL_PATH)
mkdir -p $(LOCAL_PATH) # Create the target directory if it doesn't exist
ssh -p $(REMOTE_PORT) $(REMOTE_USER)@$(REMOTE_SERVER) \
"docker exec $$(docker --context swarm-demo.autonomic.zone ps -q -f name=nextload-demo_autonomic_zone_mongo) \
tar -cC $(REMOTE_CONTAINER_PATH) ." | tar -xC $(LOCAL_PATH)
@echo "Step 3: Transfer the dump to local using rsync" @echo "Step 3: Drop the existing local database and restore the dump"
rsync -avz -e "ssh -p $(REMOTE_PORT)" $(REMOTE_USER)@$(REMOTE_SERVER):$(REMOTE_PATH) $(LOCAL_PATH) docker exec -it $(LOCAL_CONTAINER_NAME) mongosh --username $(MONGO_USER) --password $(MONGO_PASSWORD) $(LOCAL_DB) --eval "db.dropDatabase()"
docker exec -it $(LOCAL_CONTAINER_NAME) mongorestore --drop --username $(MONGO_USER) --password $(MONGO_PASSWORD) --authenticationDatabase admin $(LOCAL_PATH)
@echo "Step 4: Drop the existing local database and restore the dump"
docker exec -it $(LOCAL_CONTAINER_NAME) mongo $(LOCAL_DB) --eval "db.dropDatabase()"
docker exec -it $(LOCAL_CONTAINER_NAME) mongorestore --drop -u $(MONGO_USER) -p $(MONGO_PASSWORD) $(LOCAL_PATH)