UNPKG

localbackup

Version:

Utility to make local backups easily and without the hassle.

114 lines 6.02 kB
#!/usr/bin/env node "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_path_1 = __importDefault(require("node:path")); const clear_1 = __importDefault(require("clear")); const chalk_1 = __importDefault(require("chalk")); const boxen_1 = __importDefault(require("boxen")); const yesno_1 = __importDefault(require("yesno")); const dayjs_1 = __importDefault(require("dayjs")); const fs_extra_1 = __importDefault(require("fs-extra")); const commander_1 = require("commander"); const constants_1 = require("./constants"); const createTempBackup_1 = require("./createTempBackup"); const deleteOldBackups_1 = require("./deleteOldBackups"); const storageProviders_1 = require("./storageProviders"); const uploadBackup_1 = require("./uploadBackup"); const deleteTempBackup_1 = require("./deleteTempBackup"); (0, clear_1.default)(); console.log(chalk_1.default.green((0, boxen_1.default)(chalk_1.default.bold('Local Backup 🦄'), { padding: 1, borderStyle: 'double' }))); console.log(''); commander_1.program .option('-y, --yes', 'answer yes to all questions', false) .requiredOption('-s, --source <string>', 'full path of the file or directory to be backed up') .requiredOption('-d, --destdir <string>', 'full path of the destination directory where the backup file will be stored') .option('-t, --filetype <string>', 'file type of the backup file (zip or tar)', 'zip') .option('-p, --prefix <string>', 'prefix to be added to the backup file name', 'backup') .option('-k, --keeplast <number>', 'number of backups to keep (olders with the same prefix will be deleted)') .option('-sp, --storageprovider <string>', `storage provider to use (${storageProviders_1.storageProvidersNames.join(', ')})`, 'local'); // Show help if no arguments are passed if (process.argv.length < 3) { commander_1.program.help(); } commander_1.program.parse(); const options = commander_1.program.opts(); function main() { return __awaiter(this, void 0, void 0, function* () { // Check if the file type is valid if (!['zip', 'tar'].includes(options.filetype)) { console.log(chalk_1.default.red('\nInvalid file type, please use zip or tar\n')); return; } // Check if the storage provider is valid if (!storageProviders_1.storageProvidersNames.includes(options.storageprovider)) { console.log(chalk_1.default.red(`\nInvalid storage provider, please use one of the following: ${storageProviders_1.storageProvidersNames.join(', ')}\n`)); return; } const source = node_path_1.default.normalize(options.source); const targetDir = node_path_1.default.normalize(options.destdir); const destinationFileName = `${options.prefix}-${(0, dayjs_1.default)().format(constants_1.DATE_FORMAT)}.${options.filetype}`; const destinationFilePath = node_path_1.default.join(targetDir, destinationFileName); // Check if the source file/dir exists if (!fs_extra_1.default.existsSync(source)) { console.log(chalk_1.default.red('\nThe source file or directory does not exist\n')); return; } console.log(chalk_1.default.bold.yellow('You are about to make a backup with the following parameters:')); console.log(''); console.log(chalk_1.default.bold('Source file/dir:'), source); console.log(chalk_1.default.bold('Target directory:'), targetDir); console.log(chalk_1.default.bold('Target file path:'), destinationFilePath); console.log(''); if (!options.yes) { const ok = yield (0, yesno_1.default)({ question: 'Are you sure you want to continue? [y/n]:' }); console.log(''); if (!ok) { console.log(chalk_1.default.red('Aborted!\n')); return; } } // Create local temporal backup const compressedFilePath = yield (0, createTempBackup_1.createTempBackup)({ source, destinationFileName, fileType: options.filetype }); // Upload backup to provider yield (0, uploadBackup_1.uploadBackup)({ storageProvider: options.storageprovider, localFilePath: compressedFilePath, destinationFilePath }); // Delete temporal backup yield (0, deleteTempBackup_1.deleteTempBackup)({ localFilePath: compressedFilePath }); // Delete old backups from provider yield (0, deleteOldBackups_1.deleteOldBackups)({ storageProvider: options.storageprovider, prefix: options.prefix, targetDirectory: targetDir, fileType: options.filetype, keepLast: options.keeplast }); console.log(chalk_1.default.green('\nBackup completed successfully!\n')); console.log(chalk_1.default.bold('Please give this project a star on GitHub if you like it:')); console.log(chalk_1.default.bgBlue.bold('⭐️ https://github.com/eduardolat/localbackup\n')); }); } main().catch(console.error); //# sourceMappingURL=index.js.map