how to exclude current log file while archiving log files inside a directory

513 views Asked by At

I have to archive a directory in which log file are present(File names are in a format as i.e 2017-12-06.log).I have to archive all files except 2017-12-06.log.

I have a logs directory where all log files are present and a script create-zip.js. When I run following script with node create-zip.js, It creates directories as /archive/<year>/<month_name> and saves here logs.zip folder.

Everything is fine except when I extract logs.zip, my archived log files are found inside /archive/<year>/<month_name>/home/hotam/nodejs_archive_example/logs but I want these files inside /archive/<year>/<month_name>. I googled a lot but couldn't find solution. Thanks in advance.

I have following script(create-zip.js):

    'use strict';
    var fs = require('fs'),
    path = require('path'),
    archiver = require('archiver'),
    currDate = new Date(),
    year = currDate.getFullYear(),
    month = currDate.toLocaleString("en-us", { month: "long" }),
    dir = path.join(__dirname + '/archive/' + year + '/' + month),
    ignoredFile = currDate.getFullYear()+'-'+('0' + (currDate.getMonth() + 1)).slice(-2)+'-'+('0' + currDate.getDate()).slice(-2)+'.log';




//Function to create directories recursively
exports.createDir = function (dir) {
    const splitPath = dir.split('/');
    splitPath.reduce(function (dirPath, subPath) {
        let currentPath;
        if (subPath != '.') {
            currentPath = dirPath + '/' + subPath;
            if (!fs.existsSync(currentPath)) {
                fs.mkdirSync(currentPath);
            }
        } else {
            currentPath = subPath;
        }
        return currentPath
    }, '');
};
exports.createDir(dir);

var output = fs.createWriteStream(path.join(dir, 'logs.zip'));
var archive = archiver('zip', {});
var logPath = __dirname + '/logs';


output.on('close', function () {
    if (fs.existsSync(logPath)) {
        fs.readdirSync(logPath).forEach(function (file, index) {
            var curPath = logPath + "/" + file;
            if (!fs.lstatSync(logPath).isFile()) {
                // delete file
                if(!(file == ignoredFile)) {
                    fs.unlinkSync(curPath);
                }
            }
        });
    }
});

output.on('end', function () {
    console.log('Data has been drained');
});

archive.on('warning', function (err) {
    if (err.code === 'ENOENT') {
        console.log(err);
    } else {
        // throw error
        console.log(err);
        throw err;
    }
});

archive.on('error', function (err) {
    logger.error(err);
    throw err;
});

archive.pipe(output);

//ignoring 2017-12-06.log
archive
    .glob(__dirname + '/logs/**', {
        ignore: [__dirname + '/logs/'+ignoredFile]
    })
    .finalize();
1

There are 1 answers

0
HOTAM SINGH On BEST ANSWER

I got the solution of this scenario. I changed archive.glob() and it worked for me.

`//ignoring 2017-12-06.log
archive
    .glob('./logs/**/*', {
        ignore: ['./logs/**/*' + ignoredFile]
    })
    .finalize();`