Skip to content

Latest commit

 

History

History
146 lines (101 loc) · 4.65 KB

1.Scriptv1.0.md

File metadata and controls

146 lines (101 loc) · 4.65 KB

Script v1.0 : Typescript, OOP and crontasks

In the last page we created a simple script to handle our Github backups on a regular basis, but this script was written in Javascript really fast. I use the script without any problem but I want it to be more readable, so I decided to remake the entire script in Typescript with an Object Oriented Programming (OOP) approach.

Setting up Typescript

I you’re familiar with Typescript, feel free to skip this explanation, for those new on this language, Typescript compiles the source code to plain Javascript, so we need a compiler. Let’s install it on node using npm.

npm install typescript

Then to make sure that node types are inside our typescript, we will install them

npm install --save-dev @types/node

Okay, we have typescript compiler installed, let’s configure how we want it to compile our code on our tsconfig.json file.

{
    "compilerOptions": {
      "target": "ES2021",
      "module": "commonjs",
      "outDir": "./out/",
      "rootDir": "./src/",
      "strict": true,
      "esModuleInterop": true,
  
      /* Experimental Options */
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true ,
  
      /* Advanced Options */
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true
    },
    "files": [
      "./src/configuration.ts",
      "./src/logger.ts",
      "./src/commands.ts",
      "./src/service.ts",
    ]
  }

We are telling Typescript that our source code has 4 files to compile, where to place the output and to do it in commonjs format, that is the common format for nodejs apps.

Little explanation of the code.

The logic inside is pretty much the same, the service.ts is our main class, with has the following methods:

export class Service {

		private cloned  : number;   // Cloned repositories
		private updated : number;   // Updated repositories
		private failed  : number;   // Failed repositories

		constructor();              // Here we check the params and make the base directory  
		start();                    // Start the service
		checkParams();              // Check the compulsory parameters
		backup();                   // Start making the backup
		cloneRepository()           // Clone a repository
		updateRepository()          // Update a repository

}

const service = new Service();
service.start();

The other classes are pretty simple:

Commands: Executes commands

Logger: Log things in a readable way.

Configuration: Gets the configuration objects from the files.

There is so much to improve in this script, but it is still an early open source project so you can contribute or watch it here.

Creating a cron task in Linux

Okay so I decided that the infrastructure (OS) must be the one to execute the script in a given interval. I’m using Ubuntu in my NAS, so let’s create a cron task, the way Linux handles the script execution on a given interval.

First of all I create a shell script to wrap the update of the service itself, npm package update, Typescript compilation and execution.

#!/bin/sh
echo Starting github-backup service. 
date -I

echo --------------------------------------------------------------
echo "   Updating the service"
echo --------------------------------------------------------------
git pull

echo --------------------------------------------------------------
echo "   Updating npm packages"
echo --------------------------------------------------------------
npm i

echo --------------------------------------------------------------
echo "   Compiling typescript code" 
echo --------------------------------------------------------------
npm run compile

echo --------------------------------------------------------------
echo "   Starting backup service"
echo --------------------------------------------------------------
npm run start 

echo service finished.

Then I edit the cron file to execute the script every 6 hours using

crontab -e

And add the following line that executes our script every 6h and logs the info and error into logs/github-backup.log

* */6 * * *  cd /home/nas-admin/script/github-backup-script/ && sh linux.service.sh > /home/nas-admin/script/logs/github-backup.log 2>&1

That’s it! we have our service running and auto-updating automatically every 6h.

Using the SSD to store the backups

I ended up using the SSD to store the backups so I change the params file to use the SSD.

{
  "directory" : "/media/internal/github-backup"
}

Back