nextload/Makefile
2024-07-16 11:55:55 +02:00

30 lines
1.2 KiB
Makefile

.PHONY: sync_db
# Define variables
REMOTE_USER=tma
REMOTE_SERVER=swarm-demo.autonomic.zone
REMOTE_PORT=222
REMOTE_CONTAINER_PATH=/data/db/mongodump
REMOTE_PATH=/tmp/mongodump
LOCAL_PATH=local_path/mongodump
LOCAL_DB=your_database
LOCAL_CONTAINER_NAME=nextload-mongo-1
ENV_FILE=.env
include $(ENV_FILE)
sync_db:
@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) \
bash -c 'mongodump -u mongo -p "$$(cat /run/secrets/mongo_password)" -o $(REMOTE_CONTAINER_PATH)'
@echo "Step 2: Copy the dump from container to remote server"
docker --context $(REMOTE_SERVER) cp $$(docker --context $(REMOTE_SERVER) ps -q -f name=nextload-demo_autonomic_zone_mongo):$(REMOTE_CONTAINER_PATH) $(REMOTE_PATH)
@echo "Step 3: Transfer the dump to local using rsync"
rsync -avz -e "ssh -p $(REMOTE_PORT)" $(REMOTE_USER)@$(REMOTE_SERVER):$(REMOTE_PATH) $(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)