Skip to content

Commit

Permalink
Backup db (#589)
Browse files Browse the repository at this point in the history
* backupdb2jira using axios

* use native fetch instead

* create Issue, store Backup in Issue. Added Error logging

* remove push trigger, add dispatch and weekly trigger

* put workflow to correct location

* add test trigger

* which dir

* update action v4

* delete PR exec of backup

* use correct format EJSON

* correct dependencies for backupdb script

* Test für Freitag morgen

---------

Co-authored-by: Silas Meilbeck <[email protected]>
  • Loading branch information
jonycoo and sMeilbeck authored Jun 26, 2024
1 parent 45f8668 commit 37ef540
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/backup2jira.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Backup and Upload

on:
workflow_dispatch:
schedule:
- cron: "0 0 * * 0" # Every Sunday at midnight (UTC)
- cron: "0 5 * * 5" # Every Friday at 5 AM (UTC)

jobs:
backup-and-upload:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
run: |
cd actions/backup2jira
npm install
- name: Run Backup and Upload
run: node actions/backup2jira/indexBackup.js
env:
DATABASE_URI: ${{ secrets.DATABASE_URI }}
JIRA_TOKEN: ${{ secrets.JIRA_TOKEN }}
142 changes: 142 additions & 0 deletions actions/backup2jira/indexBackup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require('dotenv').config();
const { MongoClient } = require("mongodb");
const { EJSON } = require('bson')

// MongoDB URI and database details
const mongoURI = process.env.DATABASE_URI;
const dbName = "Seed";
const collectionNames = ["Stories", "Repositories", "CustomBlocks", "User", "Workgroups"];

const jiraBaseUrl = "https://jira.adesso.de/rest/api/2";
const jiraProjKey = 'CUC'
const bearerToken = process.env.JIRA_TOKEN

async function connectToMongoDB() {
try {
const client = new MongoClient(mongoURI);
await client.connect();
console.log("Connected to MongoDB");
return client;
} catch (error) {
console.error("Error connecting to MongoDB:", error.message);
throw error;
}
}

async function queryMongoData(client) {
try {
const db = client.db(dbName);
const data = [];

for (const collectionName of collectionNames) {
const collection = db.collection(collectionName);
const collectionData = await collection.find().toArray();
const collectionBSON = collectionData.map(o => EJSON.stringify(o)); // Serialize each (individual)object
//console.log(collectionData)
const file = new File(collectionBSON, `${collectionName}.json`);
data.push(file);
}

console.log("Retrieved MongoDB data:", data);
return data;
} catch (error) {
console.error("Error querying MongoDB data:", error.message);
throw error;
}
}

async function createJiraIssue(projectKey) {
try {
const attachmentUrl = `${jiraBaseUrl}/issue`;

const today = new Date()

// Append issue details to formData
const issueData = { fields: {
project: { key: projectKey },
summary: `DB-Backup ${today.toLocaleDateString()}`,
description: `Backup of Database Collections for Project Seed-Test`,
// fixVersion: {id:1002},
labels: ['DB-Backup'],
issuetype: {
name: "Service Request"
}
}};

// Make the POST request
const response = await fetch(attachmentUrl, {
method: 'POST',
headers: {
"Authorization": `Bearer ${bearerToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(issueData)
});

if(response.ok){
const content = await response.json();
console.log("Issue created: ", content.key);
return content.key;
}
throw Error(`creating Issue no ok ${response.status}`)

} catch (error) {
console.error("Failed Creating Issue. Reason:", error.message);
}
}


/**
*
* @param {String} issueKey
* @param {Array} data
*/
async function sendToJiraIssue(issueKey, data) {
try {
const attachmentUrl = `${jiraBaseUrl}/issue/${issueKey}/attachments`;

const formData = new FormData();
// Create a FormData object and append the data
data.forEach(file => formData.append("file", file))


// Make the POST request
const response = await fetch(attachmentUrl, {
method: 'POST',
headers: {
"Authorization": `Bearer ${bearerToken}`, // Include your bearer token
"X-Atlassian-Token": "nocheck"
},
body: formData
}).then(async res => await res.json());
delete response?.author

if(response.errorMessages) throw Error(`add attachment no ok ${response.errorMessages}, Issue: ${issueKey}`);
return response;

} catch (error) {
console.error("Error adding attachment:", error.message);
}
}


async function main() {
let client;
try {
client = await connectToMongoDB();
const data = await queryMongoData(client);
const issueKey = await createJiraIssue(jiraProjKey);
const attachResult = await sendToJiraIssue(issueKey, data)
if(attachResult&&data) console.log("Successfully created DB Backup: ", issueKey);
throw Error("Something went wrong see earlier errors")
} catch (error) {
console.error("Error:", error.message);
} finally {
if (client) {
client.close();
console.log("Disconnected from MongoDB");
}
}
}

main();
7 changes: 7 additions & 0 deletions actions/backup2jira/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"bson": "^6.7.0",
"dotenv": "^16.4.5",
"mongodb": "^6.6.2"
}
}

0 comments on commit 37ef540

Please sign in to comment.