From 7e39c0829fb13332ce766898d483cfcc38ed92e7 Mon Sep 17 00:00:00 2001 From: suvajit Date: Sat, 18 May 2024 11:07:25 +0530 Subject: [PATCH] Initial commit --- .github/workflows/backup-tool.yml | 27 +++++++++++++++++++++++++++ .gitignore | 2 ++ Dockerfile | 12 ++++++++++++ README.md | 1 + backup.sh | 23 +++++++++++++++++++++++ docker-compose.yml | 19 +++++++++++++++++++ 6 files changed, 84 insertions(+) create mode 100644 .github/workflows/backup-tool.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 backup.sh create mode 100644 docker-compose.yml diff --git a/.github/workflows/backup-tool.yml b/.github/workflows/backup-tool.yml new file mode 100644 index 0000000..05bb463 --- /dev/null +++ b/.github/workflows/backup-tool.yml @@ -0,0 +1,27 @@ +name: backup-tool + +on: + workflow_dispatch: + +jobs: + build: + name: backup-tool + runs-on: buildjet-4vcpu-ubuntu-2204-arm + strategy: + matrix: + collection: [PlayerLinks, ClanStores] + + steps: + - name: Init Backup Tool + run: docker compose up + env: + MONGO_COLLECTION_NAME: ${{ matrix.collection }} + S3_BUCKET: ${{ secrets.S3_BUCKET }} + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }} + MONGO_HOST: ${{ secrets.MONGO_HOST }} + MONGO_PORT: ${{ secrets.MONGO_PORT }} + MONGO_USERNAME: ${{ secrets.MONGO_USERNAME }} + MONGO_PASSWORD: ${{ secrets.MONGO_PASSWORD }} + MONGO_DB_NAME: ${{ secrets.MONGO_DB_NAME }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0da6a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +backup/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c5c8a7c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM mongo:latest + +RUN apt-get update && \ + apt-get install -y python3-pip && \ + pip3 install awscli && \ + apt-get clean + +COPY backup.sh /usr/local/bin/backup.sh + +RUN chmod +x /usr/local/bin/backup.sh + +ENTRYPOINT ["/usr/local/bin/backup.sh"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..53dbfce --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# backup-tool \ No newline at end of file diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..138d701 --- /dev/null +++ b/backup.sh @@ -0,0 +1,23 @@ +#!/bin/bash +set -e + +DATE=$(date +%Y-%m-%d-%H-%M-%S) +BACKUP_DIR="backup" + +echo "Starting mongodump..." + +mongodump --host $MONGO_HOST \ + --port $MONGO_PORT \ + --username $MONGO_USERNAME \ + --password $MONGO_PASSWORD \ + --authenticationDatabase admin \ + --db $MONGO_DB_NAME \ + --collection $MONGO_COLLECTION_NAME \ + --out ./$BACKUP_DIR + +tar -czf "$BACKUP_DIR-$DATE.tar.gz" -c "$BACKUP_DIR" + +echo "Uploading backup to S3..." +aws s3 cp "./$BACKUP_DIR-$DATE.tar.gz" "s3://$S3_BUCKET/$MONGO_COLLECTION_NAME-$DATE.tar.gz" + +echo "Backup and upload completed successfully." diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c01128b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3.8' + +services: + backup-tool: + build: . + environment: + - S3_BUCKET=${S3_BUCKET} + - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} + - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} + - AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} + - MONGO_HOST=${MONGO_HOST} + - MONGO_PORT=${MONGO_PORT} + - MONGO_USERNAME=${MONGO_USERNAME} + - MONGO_PASSWORD=${MONGO_PASSWORD} + - MONGO_DB_NAME=${MONGO_DB_NAME} + - MONGO_COLLECTION_NAME=${MONGO_COLLECTION_NAME} + volumes: + - ./backup.sh:/usr/local/bin/backup.sh + - ./backup:/backup