From 64c809cdac660ba847d10cb68c9b511439ba2274 Mon Sep 17 00:00:00 2001 From: HF Date: Wed, 8 Jan 2020 20:59:24 +0100 Subject: [PATCH] add COMMAND option to backup script fix directory date name --- README.md | 3 ++- src/backup.js | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bbead050..1978647f 100644 --- a/README.md +++ b/README.md @@ -193,10 +193,11 @@ It requires a [second running redis instance](https://www.digitalocean.com/commu The backup script gets built when building pixelplanet and also gets copied to build/ directory. You can run it with: ``` -node backup.js REDIS_URL_CANVAS REDIS_URL_BACKUP BACKUP_DIRECTORY [INTERVAL] +node backup.js REDIS_URL_CANVAS REDIS_URL_BACKUP BACKUP_DIRECTORY [INTERVAL] [COMMAND] ``` Make sure to get the order right, because the backup redis instance will be overwritten every hour. Interval is the time in minutes between incremential backups. If interval is undefined, it will just make one backup and then exit. +If command is defined, it will be executed after every backup (just one command, with no arguments, like "dosomething.sh"), this is useful for synchronisation with a storage server i.e.. Alternatively you can run it with pm2, just like pixelplanet. An example ecosystem-backup.example.yml file will be located in the build directory. diff --git a/src/backup.js b/src/backup.js index 0f2f79ca..f036013b 100644 --- a/src/backup.js +++ b/src/backup.js @@ -48,6 +48,7 @@ const [ BACKUP_REDIS_URL, BACKUP_DIR, INTERVAL, + CMD, ] = process.argv.slice(2); if (!CANVAS_REDIS_URL || !BACKUP_REDIS_URL || !BACKUP_DIR) { @@ -62,13 +63,35 @@ const canvasRedis = redis const backupRedis = redis .createClient(BACKUP_REDIS_URL, { return_buffers: true }); canvasRedis.on('error', () => { - throw new Error('Could not connect to canvas redis'); + console.error('Could not connect to canvas redis'); + process.exit(1); }); backupRedis.on('error', () => { - throw new Error('Could not connect to backup redis'); + console.error('Could not connect to backup redis'); + process.exit(1); }); +function runCmd(cmd: string) { + const startTime = Date.now(); + console.log(`Executing ${cmd}`); + const cmdproc = spawn(cmd); + cmdproc.on('exit', (code) => { + if (code !== 0) { + console.log(`${cmd} failed with code ${code}`); + } + const time = Date.now() - startTime; + console.log(`${cmd} done in ${time}ms`); + }); + cmdproc.stdout.on('data', (data) => { + console.log(data); + }); + cmdproc.stderr.on('data', (data) => { + console.log(data); + }); +} + + function getDateFolder() { const dir = path.resolve(__dirname, BACKUP_DIR); if (!fs.existsSync(dir)) { @@ -76,8 +99,11 @@ function getDateFolder() { process.exit(1); } const date = new Date(); - // eslint-disable-next-line max-len - const dayDir = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`; + let month = date.getMonth() + 1; + let day = date.getDate(); + if (month < 10) month = `0${month}`; + if (day < 10) day = `0${day}`; + const dayDir = `${date.getFullYear()}${month}${day}`; const backupDir = `${dir}/${dayDir}`; return backupDir; } @@ -118,6 +144,9 @@ async function trigger() { } else { await incrementialBackup(); } + if (CMD) { + runCmd(CMD); + } if (!INTERVAL) { process.exit(0); }