Skip to content

Commit

Permalink
feat: add aws backups (#3622)
Browse files Browse the repository at this point in the history
* feat: add aws backups

* chore: add aws client package
  • Loading branch information
krtk6160 authored Nov 29, 2023
1 parent 071763c commit 573d0bb
Show file tree
Hide file tree
Showing 5 changed files with 1,092 additions and 8 deletions.
1 change: 1 addition & 0 deletions core/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@apollo/server": "^4.9.5",
"@aws-sdk/client-s3": "^3.461.0",
"@google-cloud/storage": "^7.6.0",
"@grpc/grpc-js": "^1.9.11",
"@grpc/proto-loader": "^0.7.10",
Expand Down
42 changes: 41 additions & 1 deletion core/api/src/app/admin/backup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Storage } from "@google-cloud/storage"
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3"

import axios from "axios"

Expand All @@ -10,6 +11,8 @@ import {
NEXTCLOUD_PASSWORD,
NEXTCLOUD_URL,
NEXTCLOUD_USER,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
} from "@/config"
import { ErrorLevel } from "@/domain/shared"
import {
Expand All @@ -30,7 +33,8 @@ export const uploadBackup =
!(
DROPBOX_ACCESS_TOKEN ||
GCS_APPLICATION_CREDENTIALS_PATH ||
(NEXTCLOUD_URL && NEXTCLOUD_USER && NEXTCLOUD_PASSWORD)
(NEXTCLOUD_URL && NEXTCLOUD_USER && NEXTCLOUD_PASSWORD) ||
(AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY)
)
) {
const err = new Error(
Expand Down Expand Up @@ -111,6 +115,42 @@ export const uploadBackup =
)
}

if (!(AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY)) {
addAttributesToCurrentSpan({ ["uploadBackup.destination.aws"]: "false" })
} else {
addAttributesToCurrentSpan({ ["uploadBackup.destination.aws"]: "true" })
asyncRunInSpan(
"app.admin.backup.uploadBackup.aws",
{
attributes: {
[SemanticAttributes.CODE_FUNCTION]: "uploadBackup.aws",
[SemanticAttributes.CODE_NAMESPACE]: "app.admin.backup",
},
},
async () => {
const client = new S3Client({})
const command = new PutObjectCommand({
Bucket: LND_SCB_BACKUP_BUCKET_NAME,
Key: `lnd_scb/${filename}`,
Body: backup,
})
try {
client.send(command)
logger.info({ backup }, "Static channel backup to AWS successful.")
addEventToCurrentSpan("Static channel backup to AWS successful.")
} catch (error) {
const fallbackMsg = "Static channel backup to AWS failed."
logger.error({ error }, fallbackMsg)
recordExceptionInCurrentSpan({
error: error,
level: ErrorLevel.Warn,
fallbackMsg,
})
}
},
)
}

if (!(NEXTCLOUD_URL && NEXTCLOUD_USER && NEXTCLOUD_PASSWORD)) {
addAttributesToCurrentSpan({ ["uploadBackup.destination.nextcloud"]: "false" })
} else {
Expand Down
4 changes: 4 additions & 0 deletions core/api/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export const env = createEnv({
NEXTCLOUD_URL: z.string().min(1).optional(),
NEXTCLOUD_USER: z.string().min(1).optional(),
NEXTCLOUD_PASSWORD: z.string().min(1).optional(),
AWS_ACCESS_KEY_ID: z.string().min(1).optional(),
AWS_SECRET_ACCESS_KEY: z.string().min(1).optional(),

MATTERMOST_WEBHOOK_URL: z.string().min(1).optional(),

Expand Down Expand Up @@ -218,6 +220,8 @@ export const env = createEnv({
NEXTCLOUD_URL: process.env.NEXTCLOUD_URL,
NEXTCLOUD_USER: process.env.NEXTCLOUD_USER,
NEXTCLOUD_PASSWORD: process.env.NEXTCLOUD_PASSWORD,
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,

MATTERMOST_WEBHOOK_URL: process.env.MATTERMOST_WEBHOOK_URL,

Expand Down
2 changes: 2 additions & 0 deletions core/api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ export const PRICE_PORT = env.PRICE_PORT
export const PRICE_HISTORY_HOST = env.PRICE_HISTORY_HOST
export const PRICE_HISTORY_PORT = env.PRICE_HISTORY_PORT
export const DROPBOX_ACCESS_TOKEN = env.DROPBOX_ACCESS_TOKEN
export const AWS_ACCESS_KEY_ID = env.AWS_ACCESS_KEY_ID
export const AWS_SECRET_ACCESS_KEY = env.AWS_SECRET_ACCESS_KEY
export const GCS_APPLICATION_CREDENTIALS_PATH = env.GCS_APPLICATION_CREDENTIALS_PATH
export const NEXTCLOUD_URL = env.NEXTCLOUD_URL
export const NEXTCLOUD_USER = env.NEXTCLOUD_USER
Expand Down
Loading

0 comments on commit 573d0bb

Please sign in to comment.