Skip to content

Creating backup script

Daisho Komiyama edited this page Jun 19, 2020 · 1 revision

We all should be given permission to use any version control under any circumstances,but the reality is harsh sometimes. When that happens you need to backup manually, but at least we should write a script for it. I use Nodejs.

const fs = require("fs");
const path = require("path");

const now = new Date();
const year = now.getFullYear();
const month =
    String(now.getMonth() + 1).length <= 1
        ? 0 + String(now.getMonth() + 1)
        : String(now.getMonth() + 1);
const day = String(now.getDate()).length <= 1 ? 0 + String(now.getDate()) : String(now.getDate());
const hours =
    String(now.getHours()).length <= 1 ? 0 + String(now.getHours()) : String(now.getHours());
const minutes =
    String(now.getMinutes()).length <= 1 ? 0 + String(now.getMinutes()) : String(now.getMinutes());

const foldername = `${year}${month}${day}-${hours}${minutes}-copy`;

const pathToHtml = path.join(__dirname, "./Documents/projects/applications/conetnt/");
const pathToCss = path.join(__dirname, "./Documents/projects/applications/conetnt/css");

const pathToDest = path.join(__dirname, "./bkup/");

const htmlFiles = ["file1.html", "file2.html"];
const cssFiles = ["file1.css", "file2.css"];

fs.mkdir(`${pathToDest}${foldername}`, { recursive: true }, (err) => {
    if (err) throw err;

    for (let i = 0; i < htmlFiles.length; i++) {
        fs.copyFileSync(
            `${pathToHtml}${htmlFiles[i]}`,
            `${pathToDest}${foldername}/${htmlFiles[i]}`
        );
    }

    for (let i = 0; i < cssFiles.length; i++) {
        fs.copyFileSync(`${pathToCss}${cssFiles[i]}`, `${pathToDest}${foldername}/${cssFiles[i]}`);
    }

    console.log(`Copi done under ${foldername}`);
});
Clone this wiki locally