Guest User

Untitled

a guest
Jan 25th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. const path = require('path');
  2.  
  3. const Ssh = require('node-ssh');
  4. const moment = require('moment');
  5. const tar = require('tar');
  6.  
  7. async function downloadSlowQueryLog(sshInfo, sshPassword, remoteSlowQueryLogDir, slowQueryLogFileName, localSlowQueryLogFilePath) {
  8. const ssh = new Ssh();
  9.  
  10. const remoteSlowQueryLogFilePath = remoteSlowQueryLogDir + '/' + slowQueryLogFileName;
  11.  
  12. // Connect to remote server
  13. await ssh.connect(sshInfo);
  14.  
  15. // Compress log file
  16. await ssh.execCommand('sudo tar zcf ' + slowQueryLogFileName + '.tgz ' + slowQueryLogFileName, {stdin: sshPassword + '\n', cwd: remoteSlowQueryLogDir, options: {pty: true}});
  17.  
  18. // Download log file
  19. await ssh.getFile(localSlowQueryLogFilePath + '.tgz', remoteSlowQueryLogFilePath + '.tgz');
  20.  
  21. // Remove compressed log file from server
  22. await ssh.execCommand('sudo rm ' + slowQueryLogFileName + '.tgz', {stdin: sshPassword + '\n', cwd: remoteSlowQueryLogDir, options: {pty: true}});
  23.  
  24. // Disconect from server
  25. await ssh.dispose();
  26. }
  27.  
  28. async function main() {
  29.  
  30. const sshPassword = 'SSH_PASSWORD';
  31. const sshInfo = {
  32. host: 'SSH_HOST',
  33. port: SSH_PORT,
  34. username: 'SSH_USER',
  35. password: sshPassword
  36. };
  37.  
  38. const ts = moment().format('YYYYMMDDHHmmss');
  39.  
  40. const slowQueryLogFileName = 'postgresql-Wed.log';
  41. const remoteSlowQueryLogDir = '/path/to/log/dir';
  42. const localSlowQueryLogDir = 'C:/path/to/local/log/dir';
  43. const localSlowQueryFileName = ts + '_' + slowQueryLogFileName;
  44. const localSlowQueryLogFilePath = path.join(localSlowQueryLogDir, localSlowQueryFileName);
  45.  
  46. await downloadSlowQueryLog(sshInfo, sshPassword, remoteSlowQueryLogDir, slowQueryLogFileName, localSlowQueryLogFilePath);
  47. console.log('slow query log download completed.');
  48.  
  49. // Decompress downloaded log file
  50. await tar.x({
  51. file: localSlowQueryLogFilePath + '.tgz',
  52. cwd: localSlowQueryLogDir
  53. });
  54.  
  55. console.log('untar completed.');
  56. }
  57.  
  58. main();
Add Comment
Please, Sign In to add comment