Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alobato
Last active April 8, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alobato/163a2620d944c76d402a7acc1e6e4627 to your computer and use it in GitHub Desktop.
Save alobato/163a2620d944c76d402a7acc1e6e4627 to your computer and use it in GitHub Desktop.
db_backup
#!/usr/bin/env node
const fs = require('fs');
const AWS = require('aws-sdk');
const { program } = require('commander');
const execa = require('execa');
program
.option('-d, --db-name <name>', 'Database')
.option('-p, --db-password <pass>', 'Database password')
.option('-k, --key <value>', 'AWS Access Key Id')
.option('-s, --secret <value>', 'AWS Secret Access Key')
.option('-r, --region <value>', 'AWS Region')
.option('-b, --bucket <value>', 'AWS S3 Bucket');
program.parse(process.argv);
AWS.config.update({ accessKeyId: program.key, secretAccessKey: program.secret, region: program.region });
const today = new Date().toISOString().slice(0, 19).replace(/:/g, '-');
(async () => {
await execa('/usr/bin/mysqldump', ['--user=root', `--password=${program.dbPassword}`, '--default-character-set=utf8', `--result-file=/home/deployer/backups/${program.dbName}_${today}`, program.dbName]);
await execa('/usr/bin/gzip', [`/home/deployer/backups/${program.dbName}_${today}`]);
const s3 = new AWS.S3();
const stored = await s3.upload({ Bucket: program.bucket, Key: `${program.dbName}_${today}.gz`, Body: fs.createReadStream(`/home/deployer/backups/${program.dbName}_${today}.gz`) }).promise();
})();
{
"name": "db_backup",
"version": "1.0.0",
"description": "test",
"bin": "./index.js",
"dependencies": {
"commander": "5.1.0",
"execa": "4.0.3",
"aws-sdk": "2.715.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment