add COMMAND option to backup script

fix directory date name
This commit is contained in:
HF 2020-01-08 20:59:24 +01:00
parent c33c532e2b
commit 64c809cdac
2 changed files with 35 additions and 5 deletions

View File

@ -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.

View File

@ -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);
}