Skip to content

Commit

Permalink
feat(data): add db backup to aws s3
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed May 16, 2024
1 parent b4e635d commit 925368a
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ src/.eslintrc.js

test/coverage

animation
animation

dump

output.json
147 changes: 147 additions & 0 deletions cron/backupDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import fs from "fs";
import path from "path";
import CronJob from "node-cron";
import { exec } from "child_process";
import AWS from "aws-sdk";
import { stringToDate, empty, listFiles } from "../utils/helperFunctions.js";

// Auto backup function
// Only back up the database if there was a new deployment
export const dbAutoBackUp = async () => {
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});

const __dirname = path.resolve(path.dirname(""));
const backupDirPath = path.join(__dirname, "database-backup");
const backupName = process.env.BACKUP_NAME;
const bucket = process.env.BACKUP_BUCKET_NAME;
const dumpPath = path.resolve(__dirname, backupName);
const s3 = new AWS.S3();

const dbOptions = {
user: process.env.DBUSER,
pass: process.env.DBPASSWORD,
host: process.env.DBHOST,
port: process.env.DBPORT,
database: process.env.DBNAME,
uri: process.env.MONGODB_URL || "mongodb://localhost:27017/portflio",
autoBackup: true,
removeOldBackup: false,
keepLastDaysBackup: 2,
autoBackupPath: backupDirPath,
};

// check for auto backup is enabled or disabled
if (dbOptions.autoBackup) {
let date = new Date();
let beforeDate, oldBackupDir, oldBackupPath;

// Current date
let currentDate = stringToDate(date);
let newBackupDir =
currentDate.getFullYear() +
"-" +
(currentDate.getMonth() + 1) +
"-" +
currentDate.getDate();

// New backup path for current backup process
let newBackupPath = dbOptions.autoBackupPath + "-mongodump-" + newBackupDir;
// check for remove old backup after keeping # of days given in configuration
if (dbOptions.removeOldBackup) {
beforeDate = _.clone(currentDate);
// Substract number of days to keep backup and remove old backup
beforeDate.setDate(beforeDate.getDate() - dbOptions.keepLastDaysBackup);
oldBackupDir =
beforeDate.getFullYear() +
"-" +
(beforeDate.getMonth() + 1) +
"-" +
beforeDate.getDate();
// old backup(after keeping # of days)
oldBackupPath = dbOptions.autoBackupPath + "mongodump-" + oldBackupDir;
}

let cmd =
"mongodump --uri=" +
dbOptions.uri +
` --gzip --archive=${dumpPath} ` +
" --out " +
newBackupPath;

try {
await exec(cmd, (error, stdout, stderr) => {
console.log({ error, stdout, stderr });
if (empty(error)) {
// check for remove old backup after keeping # of days given in configuration.
if (dbOptions.removeOldBackup) {
if (fs.existsSync(oldBackupPath)) {
exec("rm -rf " + oldBackupPath, (err) => {});
}
}
}
});

const backupFiles = [
"articles.bson",
"articles.metadata.json",
"categories.bson",
"categories.metadata.json",
"comments.bson",
"comments.metadata.json",
"payments.bson",
"payments.metadata.json",
"products.bson",
"products.metadata.json",
"sessions.bson",
"sessions.metadata.json",
"users.bson",
"users.metadata.json",
];
// const readStream = fs.createReadStream(__dirname + "/dump/portflio/");
// const params = {
// Bucket: bucket,
// Key: backupName,
// Body: readStream,
// };
// await s3.putObject(params).promise();

backupFiles.forEach(async (file) => {
console.log(file);
const readStream = fs.createReadStream(
__dirname + "/dump/portflio/" + file
);
const params = {
Bucket: bucket,
Key: `${backupName}-${file}`,
Body: readStream,
};
await s3.putObject(params).promise();
});


console.log("Successful backup!");
console.log("Backup path: ", __dirname + "/dump");
} catch (error) {
console.log(`Backup failed: ${error}`);
}
}
};

// AutoBackUp every week (at 00:00 on Sunday)
export const initBackUpDBJob = () => {
const backUpJobFunction = CronJob.schedule(
"0 0 * * 0",
async () => {
dbAutoBackUp();
},
{
scheduled: true,
timezone: "America/Chicago",
}
);

backUpJobFunction.start();
};
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@use-effect/did-update": "^0.0.3",
"@welldone-software/why-did-you-render": "^7.0.1",
"aos": "^2.3.4",
"aws-sdk": "^2.1621.0",
"axios": "^0.21.4",
"bcrypt": "^5.0.1",
"body-parser": "^1.19.0",
Expand Down

0 comments on commit 925368a

Please sign in to comment.