Infinity Forum
Christian Engineering Solutions (CES) is a Not for Profit Organization specializing in collaborative solutions development for the Church. In the Spirit of Jesus Christ, we are to spread the gospel throughout the Earth, taking care to be good examples of Christ Jesus by serving others. CES is designed to help the Church meet these goals in the most rigorous manner possible.
Our Technology is scripture based in its goals and foundations. Open-Source and Free, one may use our services to both learn and solve problems with the goals of helping others and growing closer to God.
var backup = require('mongodb-backup');
var encryptor = require('file-encryptor');
var fs = require('fs');
const { exec } = require('child_process');
const nodemailer = require('nodemailer');
require('dotenv').config({path: __dirname + '/../.env'})
exec('rm -rf sasame sasame.zip');
backup({
uri: process.env.MONGODB_CONNECTION_URL,
root: __dirname,
callback: function(){
var key = process.env.ENCRYPTION_KEY;
var filesProcessed = 0;
var numFiles = 0;
// Encrypt files
//within this dir is a dir for each collection
var dir = 'sasame/';
fs.readdir(dir, (err, dirs) => {
dirs.forEach(function(sub_dir){
fs.readdir((dir + sub_dir), (err, files) => {
numFiles += files.length;
files.forEach(file => {
//encrypt into .dat
encryptor.encryptFile((dir + sub_dir + '/' + file), (dir + sub_dir + '/' + file.split('.')[0]+'.dat'), key, function(err) {
//delete original
fs.unlink(dir + sub_dir + '/' + file);
++filesProcessed;
//run on last iteration
if(filesProcessed == numFiles){
//now we need to zip sasame/
var folderLocation = 'sasame';
var zipLocation = 'sasame.zip';
exec('zip -r '+zipLocation+' '+folderLocation, (error, stdout, stderr) => {
//now delete unzipped folder
exec('rm -rf ' + folderLocation);
//now email the zipped file
sendEmail('uriahsanders99@gmail.com', 'Sasame Backup', 'Attached.', __dirname + '/' + zipLocation);
console.log('Backup Complete.');
});
}
});
});
});
});
});
}
});
function sendEmail(to, subject, body, file){
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
var mailOptions = {
from: process.env.EMAIL_USERNAME,
to: to,
subject: subject,
text: body,
attachments: [
{
path: file
}
]
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
//okay we first need to get all json files from the server
//then we need to download everything from uploads
var dbUri = "mongodb://127.0.0.1:27017/sasame";
//example dbUri with username and password for the database test
// var dbUri = "mongodb://username:pwd@127.0.0.1:27017/test";
var basePath = "./backup";
var Backup = require("backup-mongodb");
new Backup(dbUri, basePath).backup();
var restore = require('mongodb-restore');
var encryptor = require('file-encryptor');
var fs = require('fs');
const { exec } = require('child_process');
require('dotenv').config({path: __dirname + '/../.env'})
var key = process.env.ENCRYPTION_KEY;
var numFiles = 0;
var filesProcessed = 0;
//first unzip
exec('unzip sasame.zip', (err, stdout, stderr) => {
// Decrypt files
//within this dir is a dir for each collection
var dir = 'sasame/';
fs.readdir(dir, (err, dirs) => {
dirs.forEach(function(sub_dir){
fs.readdir((dir + sub_dir), (err, files) => {
numFiles += files.length;
files.forEach(file => {
encryptor.decryptFile((dir + sub_dir + '/' + file), (dir + sub_dir + '/' + file.split('.')[0]+'.bson'), key, function(err) {
// Decryption complete.
console.log('Decrypted ' + file);
//delete original
fs.unlink(dir + sub_dir + '/' + file);
});
++filesProcessed;
//run on last iteration
if(filesProcessed == numFiles){
restore({
uri: process.env.MONGODB_CONNECTION_URL,
root: __dirname + '/sasame',
drop: true,
callback: function(){
console.log('Database Restored.');
//now backup the data again so it is always encrypted
//this also cleans the dir
exec('node backup.js');
}
});
}
});
});
});
});
});