database-backup-dropbox
Advanced tools
Comparing version
{ | ||
"name": "database-backup-dropbox", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Backups database and copies file to DropBox", | ||
@@ -5,0 +5,0 @@ "main": "./src/app.js", |
@@ -11,2 +11,3 @@ "use strict"; | ||
const readChunk = require("read-chunk"); | ||
const zlib = require("zlib"); | ||
const dropbox_gateway_1 = require("./dropbox-gateway"); | ||
@@ -31,34 +32,62 @@ const argv = require('yargs') | ||
co(function* () { | ||
const dropBoxGateway = new dropbox_gateway_1.DropBoxGateway(); | ||
const fileName = `${fileNamePrefix}_${moment().format('MM-DD-YYYY-HH-mm-ss')}.Bak`; | ||
const sqlConfig = { | ||
user: databaseUser, | ||
password: databasePassword, | ||
server: databaseHost, | ||
database: databaseName | ||
}; | ||
const pool = yield sql.connect(sqlConfig); | ||
const sqlResult = yield pool.request().query(`BACKUP DATABASE ${databaseName} TO DISK = '${path.join(filePath, fileName)}'`); | ||
pool.close(); | ||
const fileSize = fs.statSync(path.join(filePath, fileName)).size; | ||
const chunkSize = 100000; | ||
const sessionId = yield dropBoxGateway.startSession(accessToken); | ||
for (let i = 0; i < fileSize; i = i + chunkSize) { | ||
const buffer = readChunk.sync(path.join(filePath, fileName), i, chunkSize); | ||
for (let j = 0; j < 5; j++) { | ||
try { | ||
yield dropBoxGateway.appendSession(accessToken, sessionId, i, buffer); | ||
break; | ||
} | ||
catch (err) { | ||
winston.error(err); | ||
yield delay(1000); | ||
} | ||
} | ||
winston.info(`${i} / ${fileSize}`); | ||
} | ||
yield dropBoxGateway.endSession(accessToken, sessionId, `/${fileName}`, fileSize); | ||
const compressedFileName = `${path.join(filePath, fileName)}.gz`; | ||
yield backupDatabase(databaseHost, databaseUser, databasePassword, databaseName, path.join(filePath, fileName)); | ||
yield compressFile(path.join(filePath, fileName), path.join(filePath, compressedFileName)); | ||
yield uploadFile(path.join(filePath, compressedFileName), accessToken); | ||
}).catch((err) => { | ||
winston.error(err); | ||
}); | ||
function backupDatabase(host, user, password, database, filename) { | ||
return co(function* () { | ||
const sqlConfig = { | ||
user: user, | ||
password: password, | ||
server: host, | ||
database: database | ||
}; | ||
const pool = yield sql.connect(sqlConfig); | ||
const sqlResult = yield pool.request().query(`BACKUP DATABASE ${database} TO DISK = '${filename}'`); | ||
pool.close(); | ||
return; | ||
}); | ||
} | ||
function compressFile(inputFilename, outputFilename) { | ||
return new Promise((resolve, reject) => { | ||
const gzip = zlib.createGzip(); | ||
const inputFile = fs.createReadStream(inputFilename); | ||
const outputFile = fs.createWriteStream(outputFilename); | ||
inputFile.pipe(gzip).pipe(outputFile); | ||
outputFile.on('finish', () => { | ||
inputFile.close(); | ||
inputFile.destroy(); | ||
outputFile.close(); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
function uploadFile(filename, token) { | ||
return co(function* () { | ||
const dropBoxGateway = new dropbox_gateway_1.DropBoxGateway(); | ||
const fileSize = fs.statSync(filename).size; | ||
const chunkSize = 100000; | ||
const sessionId = yield dropBoxGateway.startSession(accessToken); | ||
for (let i = 0; i < fileSize; i = i + chunkSize) { | ||
const buffer = readChunk.sync(filename, i, chunkSize); | ||
for (let j = 0; j < 5; j++) { | ||
try { | ||
yield dropBoxGateway.appendSession(token, sessionId, i, buffer); | ||
break; | ||
} | ||
catch (err) { | ||
winston.error(err); | ||
yield delay(1000); | ||
} | ||
} | ||
winston.info(`${i} / ${fileSize}`); | ||
} | ||
yield dropBoxGateway.endSession(accessToken, sessionId, `/${path.basename(filename)}`, fileSize); | ||
return; | ||
}); | ||
} | ||
//# sourceMappingURL=app.js.map |
101
src/app.ts
@@ -10,2 +10,3 @@ // Imports | ||
import * as readChunk from 'read-chunk'; | ||
import * as zlib from 'zlib'; | ||
@@ -32,4 +33,2 @@ // Import gateways | ||
const databasePassword = argv.databasePassword; | ||
// 'FZCdx-RSopAAAAAAAAAAiakLpQQc01oxOYEL0WDE_imLZZvN0jx1lbl2M6tSEJDa' | ||
const accessToken = argv.accessToken; | ||
@@ -39,44 +38,82 @@ | ||
const dropBoxGateway = new DropBoxGateway(); | ||
const fileName = `${fileNamePrefix}_${moment().format('MM-DD-YYYY-HH-mm-ss')}.Bak`; | ||
const compressedFileName = `${path.join(filePath, fileName)}.gz`; | ||
const sqlConfig = { | ||
user: databaseUser, | ||
password: databasePassword, | ||
server: databaseHost, | ||
database: databaseName | ||
}; | ||
yield backupDatabase(databaseHost, databaseUser, databasePassword, databaseName, path.join(filePath, fileName)); | ||
const pool = yield sql.connect(sqlConfig); | ||
const sqlResult = yield pool.request().query(`BACKUP DATABASE ${databaseName} TO DISK = '${path.join(filePath, fileName)}'`); | ||
pool.close(); | ||
yield compressFile(path.join(filePath, fileName), path.join(filePath, compressedFileName)); | ||
yield uploadFile(path.join(filePath, compressedFileName), accessToken); | ||
const fileSize = fs.statSync(path.join(filePath, fileName)).size; | ||
}).catch((err) => { | ||
winston.error(err); | ||
}); | ||
const chunkSize = 100000; | ||
function backupDatabase(host: string, user: string, password: string, database: string, filename: string): Promise<void> { | ||
return co(function* () { | ||
const sqlConfig = { | ||
user: user, | ||
password: password, | ||
server: host, | ||
database: database | ||
}; | ||
const sessionId = yield dropBoxGateway.startSession(accessToken); | ||
const pool = yield sql.connect(sqlConfig); | ||
const sqlResult = yield pool.request().query(`BACKUP DATABASE ${database} TO DISK = '${filename}'`); | ||
pool.close(); | ||
for (let i = 0; i < fileSize; i = i + chunkSize) { | ||
const buffer = readChunk.sync(path.join(filePath, fileName), i, chunkSize); | ||
return; | ||
}); | ||
} | ||
for (let j = 0; j < 5; j++) { | ||
try { | ||
yield dropBoxGateway.appendSession(accessToken, sessionId, i, buffer); | ||
break; | ||
} catch (err) { | ||
winston.error(err); | ||
yield delay(1000); | ||
function compressFile(inputFilename: string, outputFilename: string): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const gzip = zlib.createGzip(); | ||
const inputFile = fs.createReadStream(inputFilename); | ||
const outputFile = fs.createWriteStream(outputFilename); | ||
inputFile.pipe(gzip).pipe(outputFile); | ||
outputFile.on('finish', () => { | ||
inputFile.close(); | ||
inputFile.destroy(); | ||
outputFile.close(); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
function uploadFile(filename: string, token: string): Promise<void> { | ||
return co(function* () { | ||
const dropBoxGateway = new DropBoxGateway(); | ||
const fileSize = fs.statSync(filename).size; | ||
const chunkSize = 100000; | ||
const sessionId = yield dropBoxGateway.startSession(accessToken); | ||
for (let i = 0; i < fileSize; i = i + chunkSize) { | ||
const buffer = readChunk.sync(filename, i, chunkSize); | ||
for (let j = 0; j < 5; j++) { | ||
try { | ||
yield dropBoxGateway.appendSession(token, sessionId, i, buffer); | ||
break; | ||
} catch (err) { | ||
winston.error(err); | ||
yield delay(1000); | ||
} | ||
} | ||
winston.info(`${i} / ${fileSize}`); | ||
} | ||
winston.info(`${i} / ${fileSize}`); | ||
} | ||
yield dropBoxGateway.endSession(accessToken, sessionId, `/${path.basename(filename)}`, fileSize); | ||
yield dropBoxGateway.endSession(accessToken, sessionId, `/${fileName}`, fileSize); | ||
}).catch((err) => { | ||
winston.error(err); | ||
}); | ||
return; | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
20424
20.23%334
20.58%