Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dropboxautoupdate.js
var fs = require('fs')
var request = require('request')
var key = ' INSERT YOUR API KEY HERE! '
function save (filename, userCode, file){
return new Promise((resolve, reject)=>{
var content = fs.readFileSync(filename);
options = {
method: "POST",
url: 'https://content.dropboxapi.com/2/files/upload',
headers: {
"Content-Type": "application/octet-stream",
"Authorization": "Bearer " + key,
"Dropbox-API-Arg": "{\"path\": \"/api-save/"+userCode+"/"+file+"\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false}",
},
body:content
};
request(options,function(err, res,body){
if(err) {
// console.log(err);
reject(err)
} else {
resolve(res)
}
})
})
}
function saveFile (filename, content){
return new Promise((resolve, reject)=>{
options = {
method: "POST",
url: 'https://content.dropboxapi.com/2/files/upload',
headers: {
"Content-Type": "application/octet-stream",
"Authorization": "Bearer " + key,
"Dropbox-API-Arg": "{\"path\": \""+filename+"\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false}",
},
body:content
};
request(options,function(err, res,body){
if(err) {
reject(err)
} else {
resolve(res)
}
})
})
}
function readFile(file) {
var options = {
method: "POST",
url: 'https://content.dropboxapi.com/2/files/download',
headers: {
"Authorization": "Bearer " + key,
'Dropbox-API-Arg': '{"path":"'+file+'"}'
}
}
return new Promise((resolve, reject)=>{
request(options,function(err, res,body){
if(err) {
reject(err)
} else {
resolve(body)
}
})
})
}
function listFiles(path) {
var fetch = require('isomorphic-fetch'); // or another library of choice.
var Dropbox = require('dropbox').Dropbox;
var dbx = new Dropbox({ accessToken: key, fetch: fetch });
return new Promise((resolve, reject)=>{
dbx.filesListFolder({path: path})
.then(function(response) {
resolve(response);
})
.catch(function(error) {
reject(error);
});
})
}
module.exports = {save, readFile, saveFile, listFiles}
//? Variables declaration
var fs = require('fs')
var homedir = require('os').homedir();
var c_directory = homedir.split("\\")[0]+'\\'
var sale2folder = c_directory+'sale2-data'
var dropboxdatefile = sale2folder+'\\dropboxlastdate.txt'
var moment = require("moment")
var mysqldmp = require('mysqldump')
var dropbox = require('./dropbox')
//? Database Configs
var dbpass = ''
var dbhost = 'localhost'
var dbuser = 'root'
var dbdatabase = 'sale4'
var index = 'Empresa 1'
var waittime = 10000
//! Time to wait for create the backup
var hourstowait = 3
main()
function main() {
//? Se não existe o arquivo
if(! fs.existsSync(dropboxdatefile)) {
fs.writeFileSync(dropboxdatefile,'')
}
//? getFile content
var lasttime = readFileContent()
//? If file not exists creates it.
if(lasttime == '') {
writeFileCurrentTimestramp()
}
//? If file exists... verify if timestramp is valid, if it's not create it.
if(!moment(lasttime, "DD-MM-YYYY hh:mm:ss", true).isValid()) {
writeFileCurrentTimestramp()
}
//? Now the timestramp is valid. -> So... Just verify the time has been passed.
var isPassed = moment(currentTimeStramp(),'DD-MM-YYYY hh:mm:ss') > (moment(lasttime,'DD-MM-YYYY hh:mm:ss').add(hourstowait, 'hours'))
//! Backup in dropbox - Proceed
if(isPassed) {
var fname = moment().format('DD..MM..YYYY..hh.mm.ss')+'.sql.gz'
dbbackup(fname)
//? Replace time to wait more time
writeFileCurrentTimestramp()
dropbox.save(sale2folder+'\\backups\\'+fname, index ,fname)
console.log('Passed');
} else {
console.log('Wait more time...');
}
//!!!!! Wait time to repeat main again. !!!!!
setTimeout(main, waittime);
}
async function dbbackup(fname) {
var dbConfig = {
user: 'root',
password: '',
table: 'sale4'
}
//? If folder backup not exists create it.
createdbfolder()
try {
await mysqldmp({
connection: {
host: dbhost,
user: dbuser,
password: dbpass,
database: dbdatabase
},
dumpToFile: sale2folder+'\\backups\\'+fname,
compressFile: true
})
} catch (error) {
console.log('Catch error inside save into file:\n',error);
}
}
function currentTimeStramp() {
return moment().format('DD-MM-YYYY hh:mm:ss');
}
function readFileContent() {
return fs.readFileSync(dropboxdatefile).toString()
}
function writeFileCurrentTimestramp() {
return fs.writeFileSync(dropboxdatefile,currentTimeStramp())
}
function createdbfolder() {
if(!fs.existsSync(sale2folder+'\\backups')) {
fs.mkdirSync(sale2folder+'\\backups')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment