- Create a Lamda function with nodejs as a codebase.
- Use API Gateway to trigger lamda from external
- create a S3 bucket to store database backup from RDS
const AWS = require('aws-sdk');
const childProcess = require('child_process');
var moment = require("moment");
Go to aws console, and click on IAM user and create a API key for your account.
AWS.config.accessKeyId = "xxxxx";
AWS.config.secretAccessKey = "xxxxx";
var rds_host = "xxxxx";
var username = "xxxxx";
var password = "xxxxx";
var databaseName = "xxxxx";
Create a bucket on S3 First
s3.putObject({
Bucket: "xxxxxx",
Key: key,
Body: buffer
}, function (err, resp) {
if (err) {
console.log(err)
return
}
console.log("Database Upload Done");
});
If you dont want to use LAMDA then simple, Change this below code.
exports.DatabaseDump = ((event, context, callback) => {
var dumpCommand = `mysqldump -h${rds_host} -u${username} -p${password} ${databaseName}`;
childProcess.exec(dumpCommand, (error, stdout, stderr)=> {
var bufferData = Buffer.from(stdout, 'utf8');
uploadToS3(createKey(), bufferData);
callback(null, bufferData);
});
});
To
DatabaseDump = ((event, context, callback) => {
var dumpCommand = `mysqldump -h${rds_host} -u${username} -p${password} ${databaseName}`;
childProcess.exec(dumpCommand, (error, stdout, stderr)=> {
var bufferData = Buffer.from(stdout, 'utf8');
uploadToS3(createKey(), bufferData);
callback(null, bufferData);
});
});
DatabaseDump();