Compare commits

..

2 Commits

Author SHA1 Message Date
Florian Hartwich a0e07b4322 Add backup cronjob 2017-07-15 12:03:38 +02:00
Florian Hartwich 909767fecf Optimize backup script 2017-07-15 11:26:33 +02:00
4 changed files with 43 additions and 6 deletions

View File

@ -2,6 +2,7 @@
const cron = require('cron'); const cron = require('cron');
const async = require('async'); const async = require('async');
const {exec} = require('child_process');
const UserModel = require('../models/user'); const UserModel = require('../models/user');
const signatureTool = require('../signature-tool/signature-tool'); const signatureTool = require('../signature-tool/signature-tool');
@ -41,10 +42,28 @@ const createAllSignatures = () => {
}; };
const createBackup = () => {
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job started - CREATE BACKUP');
exec(__dirname + '/../../backup/backup.sh', (error, stdout, stderr) => {
if (error) {
console.log(error);
}
console.log('\x1b[32m%s\x1b[0m',stderr);
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job finished - CREATE BACKUP');
})
};
// Execute daily @ 02:30 AM // Execute daily @ 02:30 AM
const cronJob = cron.job('00 30 02 * * *', createAllSignatures); const cronJobSignature = cron.job('00 30 02 * * *', createAllSignatures);
// Execute daily @ 04:00 AM
const cronJobBackup = cron.job('00 00 04 * * *', createBackup);
module.exports = { module.exports = {
cronJob: cronJob, cronJobSignature: cronJobSignature,
cronJobBackup: cronJobBackup,
createAllSignatures: createAllSignatures createAllSignatures: createAllSignatures
}; };

View File

@ -8,7 +8,7 @@ const express = require('express');
const multer = require('multer'); const multer = require('multer');
const storage = multer.memoryStorage(); const storage = multer.memoryStorage();
const upload = multer({storage: storage}); const upload = multer({storage: storage});
const logger = require('debug')('cc:squads'); const logger = require('debug')('cc:wars');
// HTTP status codes by name // HTTP status codes by name
const codes = require('./http-codes'); const codes = require('./http-codes');

View File

@ -15,9 +15,10 @@ const urls = require('./config/api-url');
const restAPIchecks = require('./middleware/request-checks.js'); const restAPIchecks = require('./middleware/request-checks.js');
const errorResponseWare = require('./middleware/error-response'); const errorResponseWare = require('./middleware/error-response');
const apiAuthenticationMiddleware = require('./middleware/auth-middleware'); const apiAuthenticationMiddleware = require('./middleware/auth-middleware');
const checkSql = require('./middleware/permission-check').checkSql;
const checkAdmin = require('./middleware/permission-check').checkAdmin; const checkAdmin = require('./middleware/permission-check').checkAdmin;
const signatureCronJob = require('./cron-job/update-signatures').cronJob; const signatureCronJob = require('./cron-job/update-signatures').cronJobSignature;
const backupCronJob = require('./cron-job/update-signatures').cronJobBackup;
// router modules // router modules
const authenticateRouter = require('./routes/authenticate'); const authenticateRouter = require('./routes/authenticate');
@ -110,6 +111,7 @@ if (process.env.NODE_ENV !== config.test.env) {
else { else {
debug('Listening on port ' + config.port); debug('Listening on port ' + config.port);
signatureCronJob.start(); signatureCronJob.start();
backupCronJob.start();
} }
}); });
} else { } else {

View File

@ -1,4 +1,17 @@
#!/bin/bash #!/usr/bin/env bash
# execute script in its location folder
cd $(dirname $0)
deleteOldest() {
line_num=0
while IFS= read -r -d $'\0'; do
if [ $line_num == 1 ]; then
$(rm -rf ${REPLY})
fi
((line_num++))
done < <(find -maxdepth 1 -type d -print0 | sort -z)
}
# array of available collection names # array of available collection names
col=(app_user awarding decoration rank squad user promotion player war) col=(app_user awarding decoration rank squad user promotion player war)
@ -10,6 +23,9 @@ do
then then
mongoexport --db cc --collection $i --out $(date '+%Y-%m-%d')/collections/${i}.json; mongoexport --db cc --collection $i --out $(date '+%Y-%m-%d')/collections/${i}.json;
cp -R ../api/resource $(date '+%Y-%m-%d')/ cp -R ../api/resource $(date '+%Y-%m-%d')/
if [ $(ls -l | grep -c ^d) -gt 5 ]; then
deleteOldest
fi
else else
mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json
rm -rf ../api/resource rm -rf ../api/resource