Skip to content

Instantly share code, notes, and snippets.

@sbrow
Last active May 20, 2019 20:46
Show Gist options
  • Save sbrow/c1b370c99799d31e9a11d9edc9b41f5c to your computer and use it in GitHub Desktop.
Save sbrow/c1b370c99799d31e9a11d9edc9b41f5c to your computer and use it in GitHub Desktop.
rsync-backup
node_modules
config.json
declare type EnvVar = string | string[] | StringMap
declare interface StringMap {
[key: string]: string | string[] | StringMap
}
#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var rsync_1 = __importDefault(require("rsync"));
function getEnv(env) {
var prefixes = ["npm_config", "npm_package_config"];
for (var _i = 0, prefixes_1 = prefixes; _i < prefixes_1.length; _i++) {
var prefix = prefixes_1[_i];
/** @returns {string} */
if (process.env[prefix + "_" + env] !== undefined) {
return process.env[prefix + "_" + env];
}
var ret = [];
// Proccess arrays.
var excludes = process.env[prefix + "_" + env + "_" + 0];
for (var i = 1; excludes !== undefined; i++) {
ret.push(excludes);
excludes = process.env[prefix + "_" + env + "_" + i];
}
/** @returns {string[]} */
if (ret.length > 0) {
return ret;
}
}
return undefined;
}
var config = getEnv('config') || "./config.json";
;
if (typeof config === "string") {
var _a = require(config), exclude = _a.exclude, options = _a.options, source = _a.source, destination = _a.destination;
var shell = getEnv('shell') || "bash";
// const exclude = getEnv('exclude');
// const options = getEnv('options')
// const source = getEnv('source');
// const destination = getEnv('destination');
var rsync_2 = new rsync_1.default();
if (typeof shell === "string") {
rsync_2.shell(shell);
}
if (options instanceof Array) {
options.forEach(function (opt) {
rsync_2.set(opt);
});
}
if (typeof exclude === "string" || exclude instanceof Array) {
rsync_2.exclude(exclude);
}
if (typeof source === "string" || source instanceof Array) {
rsync_2.source(source);
}
if (typeof destination === "string") {
rsync_2.destination(destination);
}
console.log("Executing \"" + rsync_2.command() + "\"");
rsync_2.execute(function (error, code, cmd) {
console.log("\"" + cmd + "\" exited with code " + code + " and error: \"" + error + "\"");
}, function (data) {
console.log(data.toString());
}, function (data) {
console.error(data.toString());
});
}
#!/usr/bin/env node
import Rsync from "rsync";
function getEnv(env: string): EnvVar | undefined {
const prefixes = ["npm_config", "npm_package_config"];
for (const prefix of prefixes) {
/** @returns {string} */
if (process.env[`${prefix}_${env}`] !== undefined) {
return process.env[`${prefix}_${env}`];
}
const ret = [];
// Proccess arrays.
let excludes = process.env[`${prefix}_${env}_${0}`];
for (let i = 1; excludes !== undefined; i++) {
ret.push(excludes)
excludes = process.env[`${prefix}_${env}_${i}`];
}
/** @returns {string[]} */
if (ret.length > 0) {
return ret;
}
}
return undefined;
}
const config = getEnv('config') || "./config.json";;
if (typeof config === "string") {
let { exclude, options, source, destination } = require(config);
const shell = getEnv('shell') || "bash";
// const exclude = getEnv('exclude');
// const options = getEnv('options')
// const source = getEnv('source');
// const destination = getEnv('destination');
const rsync = new Rsync()
if (typeof shell === "string") {
rsync.shell(shell);
}
if (options instanceof Array) {
options.forEach((opt) => {
rsync.set(opt);
});
}
if (typeof exclude === "string" || exclude instanceof Array) {
rsync.exclude(exclude);
}
if (typeof source === "string" || source instanceof Array) {
rsync.source(source)
}
if (typeof destination === "string") {
rsync.destination(destination);
}
console.log(`Executing "${rsync.command()}"`)
rsync.execute(function(error: Error, code: number, cmd: string) {
console.log(`"${cmd}" exited with code ${code} and error: "${error}"`);
}, function(data: any) {
console.log(data.toString());
}, function(data: any) {
console.error(data.toString());
});
}
{
"name": "backup",
"version": "0.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/node": {
"version": "11.13.4",
"resolved": "https://registry.npmjs.org/@types/node/-/node-11.13.4.tgz",
"integrity": "sha512-+rabAZZ3Yn7tF/XPGHupKIL5EcAbrLxnTr/hgQICxbeuAfWtT0UZSfULE+ndusckBItcv4o6ZeOJplQikVcLvQ==",
"dev": true
},
"@types/rsync": {
"version": "0.4.30",
"resolved": "https://registry.npmjs.org/@types/rsync/-/rsync-0.4.30.tgz",
"integrity": "sha512-3z4Ee8rQyw0PD2+XpPhJn09oLsSY56AcOgHiwuZxWkjPDHab+bP/WhH2YbdqYYX9eflzb7pZpwVdEp7ZCyDlGw==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"arg": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/arg/-/arg-4.1.0.tgz",
"integrity": "sha512-ZWc51jO3qegGkVh8Hwpv636EkbesNV5ZNQPCtRa+0qytRYPEs9IYT9qITY9buezqUH5uqyzlWLcufrzU2rffdg==",
"dev": true
},
"buffer-from": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
"dev": true
},
"diff": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz",
"integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==",
"dev": true
},
"make-error": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.5.tgz",
"integrity": "sha512-c3sIjNUow0+8swNwVpqoH4YCShKNFkMaw6oH1mNS2haDZQqkeZFlHS3dhoeEbKKmJB4vXpJucU6oH75aDYeE9g==",
"dev": true
},
"rsync": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/rsync/-/rsync-0.6.1.tgz",
"integrity": "sha1-NoGgCYvYdQRI+L+dof7gn3djdCs="
},
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
"dev": true
},
"source-map-support": {
"version": "0.5.12",
"resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.12.tgz",
"integrity": "sha512-4h2Pbvyy15EE02G+JOZpUCmqWJuqrs+sEkzewTm++BPi7Hvn/HwcqLAcNxYAyI0x13CpPPn+kMjl+hplXMHITQ==",
"dev": true,
"requires": {
"buffer-from": "^1.0.0",
"source-map": "^0.6.0"
}
},
"ts-node": {
"version": "8.0.3",
"resolved": "https://registry.npmjs.org/ts-node/-/ts-node-8.0.3.tgz",
"integrity": "sha512-2qayBA4vdtVRuDo11DEFSsD/SFsBXQBRZZhbRGSIkmYmVkWjULn/GGMdG10KVqkaGndljfaTD8dKjWgcejO8YA==",
"dev": true,
"requires": {
"arg": "^4.1.0",
"diff": "^3.1.0",
"make-error": "^1.1.1",
"source-map-support": "^0.5.6",
"yn": "^3.0.0"
}
},
"typescript": {
"version": "3.4.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.3.tgz",
"integrity": "sha512-FFgHdPt4T/duxx6Ndf7hwgMZZjZpB+U0nMNGVCYPq0rEzWKjEDobm4J6yb3CS7naZ0yURFqdw9Gwc7UOh/P9oQ==",
"dev": true
},
"yn": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/yn/-/yn-3.1.0.tgz",
"integrity": "sha512-kKfnnYkbTfrAdd0xICNFw7Atm8nKpLcLv9AZGEt+kczL/WQVai4e2V6ZN8U/O+iI6WrNuJjNNOyu4zfhl9D3Hg==",
"dev": true
}
}
}
{
"name": "backup",
"version": "0.0.0",
"description": "Helps backup files using rsync.",
"author": "Spencer Brower",
"license": "GPL-3.0+",
"bin": "./cli.js",
"types": "./backup.d.ts",
"scripts": {
"start:dev": "ts-node cli.ts",
"start": "node cli.js",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"lint": "tslint",
"prepare": "npm run lint && npm run build"
},
"dependencies": {
"rsync": "^0.6.1"
},
"devDependencies": {
"@types/node": "^11.13.4",
"@types/rsync": "^0.4.30",
"ts-node": "^8.0.3",
"typescript": "^3.4.3"
}
}
{
"compilerOptions": {
"allowJs": false,
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true
},
}
{
"extends": "tslint:reccomended"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment