forked from ppfun/pixelplanet
add COMMAND option to backup script
fix directory date name
This commit is contained in:
parent
c33c532e2b
commit
64c809cdac
|
@ -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:
|
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.
|
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.
|
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.
|
Alternatively you can run it with pm2, just like pixelplanet. An example ecosystem-backup.example.yml file will be located in the build directory.
|
||||||
|
|
|
@ -48,6 +48,7 @@ const [
|
||||||
BACKUP_REDIS_URL,
|
BACKUP_REDIS_URL,
|
||||||
BACKUP_DIR,
|
BACKUP_DIR,
|
||||||
INTERVAL,
|
INTERVAL,
|
||||||
|
CMD,
|
||||||
] = process.argv.slice(2);
|
] = process.argv.slice(2);
|
||||||
|
|
||||||
if (!CANVAS_REDIS_URL || !BACKUP_REDIS_URL || !BACKUP_DIR) {
|
if (!CANVAS_REDIS_URL || !BACKUP_REDIS_URL || !BACKUP_DIR) {
|
||||||
|
@ -62,13 +63,35 @@ const canvasRedis = redis
|
||||||
const backupRedis = redis
|
const backupRedis = redis
|
||||||
.createClient(BACKUP_REDIS_URL, { return_buffers: true });
|
.createClient(BACKUP_REDIS_URL, { return_buffers: true });
|
||||||
canvasRedis.on('error', () => {
|
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', () => {
|
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() {
|
function getDateFolder() {
|
||||||
const dir = path.resolve(__dirname, BACKUP_DIR);
|
const dir = path.resolve(__dirname, BACKUP_DIR);
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
|
@ -76,8 +99,11 @@ function getDateFolder() {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
// eslint-disable-next-line max-len
|
let month = date.getMonth() + 1;
|
||||||
const dayDir = `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`;
|
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}`;
|
const backupDir = `${dir}/${dayDir}`;
|
||||||
return backupDir;
|
return backupDir;
|
||||||
}
|
}
|
||||||
|
@ -118,6 +144,9 @@ async function trigger() {
|
||||||
} else {
|
} else {
|
||||||
await incrementialBackup();
|
await incrementialBackup();
|
||||||
}
|
}
|
||||||
|
if (CMD) {
|
||||||
|
runCmd(CMD);
|
||||||
|
}
|
||||||
if (!INTERVAL) {
|
if (!INTERVAL) {
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user