-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
88 lines (82 loc) · 2.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const path = require('path');
let SftpClient = require('ssh2-sftp-client');
var SSH = require('ssh2').Client;
const mysqldump = require('mysqldump');
const fs = require('fs');
let date_ob = new Date();
var time = {
date: ("0" + date_ob.getDate()).slice(-2),
month: ("0" + (date_ob.getMonth() + 1)).slice(-2),
year: date_ob.getFullYear() - 2000,
hours: date_ob.getHours(),
minutes: date_ob.getMinutes()
}
var formattedTime = time.month + "-" + time.date + "-" + time.year + "-" + time.hours + "" + time.minutes;
const Database = {
host: '',
username: '',
password: '',
database: '',
dump: '/dbdump.sql'
}
const config = {
host: '',
username: '',
password: '',
port: 22,
src: '/var/www/html',
dst: '/backups/' + formattedTime
}
async function main() {
const client = new SftpClient('upload');
mysqldump({
connection: {
host: Database.host,
user: Database.username,
password: Database.password,
database: Database.database,
},
dumpToFile: config.src + Database.dump,
}).then((() => {
console.log('SQL dumped, proceeding..');
}))
try {
await client.connect(config);
client.on('upload', info => {
console.log(`Uploaded ${info.source}`);
});
let rslt = await client.uploadDir(config.src, config.dst);
return rslt;
} finally {
client.end();
}
}
main()
.then(msg => {
console.log(msg);
console.log('Deleting SQL file from webroot..');
fs.unlink(config.src + Database.dump, (err) => {
if (err) throw err;
console.log(config.src + Database.dump + ' has been deleted locally.')
})
console.log('Connecting to SSH Server..');
var conn = new SSH();
conn.on('ready', function () {
console.log('Connected to SSH server..');
console.log('Compressing backup file..')
conn.exec('cd /backups && tar -cf ' + config.dst + '.tar.gz ' + config.dst + ' && rm -rf ' + config.dst, function (err, stream) {
if (err) throw err;
stream.on('close', function (code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).on('data', function (data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function (data) {
console.log('STDERR: ' + data);
});
});
}).connect(config);
})
.catch(err => {
console.log(`main error: ${err.message}`);
});