nextload/Makefile

49 lines
2.3 KiB
Makefile
Raw Permalink Normal View History

2024-07-16 09:55:55 +00:00
.PHONY: sync_db
# Define variables
REMOTE_USER=tma
REMOTE_SERVER=swarm-demo.autonomic.zone
REMOTE_PORT=222
REMOTE_CONTAINER_PATH=/data/db/mongodump
2024-07-16 11:01:41 +00:00
LOCAL_PATH=/tmp/mongodump
LOCAL_DB=test
2024-07-16 09:55:55 +00:00
LOCAL_CONTAINER_NAME=nextload-mongo-1
2024-07-16 13:34:24 +00:00
REMOTE_VOLUME_NAME = nextload-demo_autonomic_zone_payload_uploads
LOCAL_VOLUME_NAME = nextload_payload_uploads
2024-07-16 11:53:19 +00:00
ENV_FILE=.env
2024-07-16 09:55:55 +00:00
2024-07-16 11:53:19 +00:00
include $(ENV_FILE)
2024-07-16 09:55:55 +00:00
sync_db:
@echo "Step 1: Dump the database on production"
2024-07-16 11:01:41 +00:00
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 'rm -rf $(REMOTE_CONTAINER_PATH) && mongodump -u mongo -p "$$(cat /run/secrets/mongo_password)" -o $(REMOTE_CONTAINER_PATH) && ls -l $(REMOTE_CONTAINER_PATH)'
2024-07-16 09:55:55 +00:00
2024-07-16 11:01:41 +00:00
@echo "Step 2: Copy the dump from the remote MongoDB container to the local machine"
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)
2024-07-16 09:55:55 +00:00
2024-07-16 11:53:19 +00:00
@echo "Step 3: Copy the dump from local machine to MongoDB container"
docker cp $(LOCAL_PATH)/ $(LOCAL_CONTAINER_NAME):/tmp/mongodump
@echo "Step 4: Drop the existing local database and restore the dump"
2024-07-16 13:34:24 +00:00
docker exec -it $(LOCAL_CONTAINER_NAME) mongorestore --drop --username $(MONGO_USER) --password $(MONGO_PASSWORD) --authenticationDatabase admin --db $(LOCAL_DB) /tmp/mongodump/$(LOCAL_DB)
sync_media:
@echo "Step 1: Create a tar archive of the remote Docker volume"
rm -rf /tmp/$(REMOTE_VOLUME_NAME)
mkdir -p /tmp/$(REMOTE_VOLUME_NAME)
ssh -p $(REMOTE_PORT) $(REMOTE_USER)@$(REMOTE_SERVER) \
"docker run --rm -v $(REMOTE_VOLUME_NAME):/volume -v /tmp:/backup alpine tar -czf /backup/$(REMOTE_VOLUME_NAME).tar.gz -C /volume ."
@echo "Step 2: Copy the media from remote volume to local volume"
scp -P $(REMOTE_PORT) $(REMOTE_USER)@$(REMOTE_SERVER):/tmp/$(REMOTE_VOLUME_NAME).tar.gz /tmp/$(REMOTE_VOLUME_NAME).tar.gz
docker run --rm -v /tmp:/volume -v $(LOCAL_VOLUME_NAME):/backup alpine tar -xzf /volume/$(REMOTE_VOLUME_NAME).tar.gz -C /backup
@echo "Step 3: Cleanup temporary files"
rm /tmp/$(REMOTE_VOLUME_NAME).tar.gz
@echo "Media synchronization complete."