Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automated MySQL backups to S3 with node.js
'use strict';
var mysqlBackup = require('./mysql-backup');
var schedule = require('node-schedule');
schedule.scheduleJob({ hour: 22, minute: 0 }, mysqlBackup);
'use strict';
var spawn = require('child_process').spawn;
var s3Upload = require('s3-stream-upload');
var config = require('../config');
var Promise = require('bluebird');
var moment = require('moment');
var mysqlBackup = function() {
var upload = s3Upload({
accessKeyId: config.aws.accessKey,
secretAccessKey: config.aws.secretKey,
Bucket: config.aws.buckets.backup.name,
region: config.aws.buckets.backup.region
});
var s3 = upload({ Key: 'mysql-backup-' + moment().format('YYYY-MM-DD-HH-mm-ss') + '.sql' });
var mysqldump = spawn('mysqldump', [
'-u', config.db.connection.user,
'-p' + config.db.connection.password,
config.db.connection.database
]);
return new Promise(function(resolve, reject) {
mysqldump
.stdout
.pipe(s3)
.on('finish', function() {
resolve();
})
.on('error', function(err) {
reject(err);
});
});
};
module.exports = mysqlBackup;
@akhilendharreddy

This comment has been minimized.

Copy link

@akhilendharreddy akhilendharreddy commented Feb 14, 2019

i am getting error like: Unhandled rejection TypeError: Cannot read property 'Bucket' of undefined
at new Uploader (/Users/akhilendhar/Desktop/node_modules/s3-stream-upload/lib/uploader.js:32:15)
at S3UploadStream (/Users/akhilendhar/Desktop/node_modules/s3-stream-upload/index.js:19:18)
at /Users/akhilendhar/Desktop/access.js:12:18
at Promise._execute (/Users/akhilendhar/Desktop/node_modules/bluebird/js/release/debuggability.js:313:9)
at Promise._resolveFromExecutor (/Users/akhilendhar/Desktop/node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (/Users/akhilendhar/Desktop/node_modules/bluebird/js/release/promise.js:79:10)
at mysqlBackup (/Users/akhilendhar/Desktop/access.js:11:11)
at Object. (/Users/akhilendhar/Desktop/access.js:8:2)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment