Last active
April 6, 2021 16:10
-
-
Save alobato/9516e3a1ad8d27cf9d7b20efa8c899eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// npx https://gist.github.com/alobato/9516e3a1ad8d27cf9d7b20efa8c899eb -s IP -p /Users/user/.ssh/id_rsa | |
const fs = require('fs'); | |
// const AWS = require('aws-sdk'); | |
const { program } = require('commander'); | |
// const execa = require('execa'); | |
const { Client } = require('ssh2'); | |
const { readFileSync } = require('fs'); | |
program | |
.option('-s, --server <host>', 'Host') | |
.option('-p, --privkey <privkey>', 'Private Key') | |
.option('-c, --command <command>', 'Command') | |
// .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, 10); | |
(async () => { | |
const host = program.server; | |
const command = program.command; | |
const privkey = readFileSync(program.privkey); | |
const conn = new Client(); | |
conn.on('ready', () => { | |
console.log('Client :: ready'); | |
conn.exec(command, (err, stream) => { | |
if (err) throw err; | |
stream.on('close', (code, signal) => { | |
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal); | |
conn.end(); | |
}).on('data', (data) => { | |
console.log('STDOUT: ' + data); | |
}).stderr.on('data', (data) => { | |
console.log('STDERR: ' + data); | |
}); | |
}); | |
}).connect({ | |
host: host, | |
port: 22, | |
username: 'deployer', | |
privateKey: privkey | |
}); | |
// await execa('/usr/bin/mysqldump', ['--user=root', `--password=${program.dbPassword}`, '--default-character-set=utf8', `--result-file=/home/deployer/backups/backup_${program.dbName}_${today}`, program.dbName]); | |
// await execa('/usr/bin/gzip', [`/home/deployer/backups/backup_${program.dbName}_${today}`]); | |
// const s3 = new AWS.S3(); | |
// const stored = await s3.upload({ Bucket: program.bucket, Key: `backup_${program.dbName}_${today}.gz`, Body: fs.createReadStream(`/home/deployer/backups/backup_${program.dbName}_${today}.gz`) }).promise(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "db_backup", | |
"version": "1.0.0", | |
"description": "test", | |
"bin": "./index.js", | |
"dependencies": { | |
"commander": "5.1.0", | |
"ssh2": "0.8.9" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment