Skip to content

Commit

Permalink
Merge pull request #6 from knackroot-technolabs-llp/cloud-backup
Browse files Browse the repository at this point in the history
Cloud backup
  • Loading branch information
argonmining authored Dec 23, 2024
2 parents c70e610 + ec50f07 commit 38c99b7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ Optionally, you can add a backup process to the DB. Check the ./backup folder.
You can build the suggested image via `docker build -t katpool-backup:0.4 .` and uncomment its part in the docker-compose.yml file.
We recommend to transfer the database dump files to other location as additional protection.

For cloud backup get google credentials file from google cloud console
- Add that json file to backup folder as "google-credentials.json"
- Configure the email address to access the dump file in config as "backupEmailAddress"
Then execute the below commads:
```bash
cd backup/
bun run cloudBackup.ts fileName.sql
```

## How to install locally using bun (not recommended)
To install dependencies:

Expand Down
50 changes: 50 additions & 0 deletions backup/cloudBackup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createReadStream, access, constants } from 'fs';
import path from 'path';
import { google } from 'googleapis';
import config from '../config/config.json'
import googleCredentials from './google-credentials.json';

const SCOPES = ["https://www.googleapis.com/auth/drive.file"];
const fileNameArgs = process.argv.slice(2);

async function authorize() {
const jwtClient = new google.auth.JWT(
googleCredentials.client_email,
undefined,
googleCredentials.private_key,
SCOPES
);
await jwtClient.authorize();
return jwtClient;
}

async function uploadFile(authClient: any) {
const drive = google.drive({ version: "v3", auth: authClient });

for(let i = 0; i < fileNameArgs.length; i++) {
access(fileNameArgs[i], constants.F_OK, async (err) => {
if (err) {
console.log(`The file ${fileNameArgs[i]} does not exist in the current directory.`);
} else {
const file = await drive.files.create({
media: {
body: createReadStream(fileNameArgs[i]),
},
fields: "id",
requestBody: {
name: path.basename(fileNameArgs[i]),
},
});
console.log("File Uploaded :", file.data.id);
const backupEmailAddress = config.backupEmailAddress
await drive.permissions.create({ fileId: file.data.id!, requestBody: { type: 'user', role: 'writer', emailAddress: backupEmailAddress } })
}
});
}
}

(async function main() {
const authClient = await authorize();
await uploadFile(authClient);
})()

3 changes: 2 additions & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
},
"treasury": {
"fee": 5
}
},
"backupEmailAddress": ""
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"denque": "^2.1.0",
"dotenv": "^16.4.5",
"fs": "^0.0.1-security",
"googleapis": "^144.0.0",
"json-bigint": "^1.0.0",
"lmdb": "^3.0.11",
"node-cron": "^3.0.3",
Expand Down

0 comments on commit 38c99b7

Please sign in to comment.