Last active
December 13, 2024 11:01
-
-
Save enijar/889ce2dbcaefb54da8c52b6d9b63f1ed to your computer and use it in GitHub Desktop.
Revisions
-
James revised this gist
Sep 5, 2016 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,8 +59,13 @@ const backupFilesFromQueue = (backupFilesQueue) => { console.log(JSON.stringify(file, null, 4)); }); let total = backupFilesQueue.length; let fullTotal = backupFilesQueue.filter(file => file.backupType === 'partial').length; let partialTotal = backupFilesQueue.filter(file => file.backupType === 'full').length; console.log(`Files needing backup (${total} total):`); console.log(`Full: ${partialTotal}/${total}`); console.log(`Partial: ${fullTotal}/${total}`); }; let backupFilesQueue = []; -
James revised this gist
Sep 5, 2016 . 1 changed file with 27 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,6 +13,18 @@ const getFileHash = (filePath) => { return hash.digest('hex') }; const addFileToBackupsQueue = (directory, file, type, hashes) => { hashes = hashes || null; backupFilesQueue.push({ path: directory, file: file, hashes: hashes, filePath: `${directory}/${file}`, backupType: type }); }; const getFilesNeedingBackup = (currentDirectory, backupDirectory) => { let files = fs.readdirSync(currentDirectory); @@ -27,7 +39,7 @@ const getFilesNeedingBackup = (currentDirectory, backupDirectory) => { if (!fs.existsSync(`${backupDirectory}/${file}`)) { // File is not backed up, therefor file has to be copied // to the backups directory. addFileToBackupsQueue(currentDirectory, file, 'full'); return; } @@ -37,13 +49,24 @@ const getFilesNeedingBackup = (currentDirectory, backupDirectory) => { if (currentHash !== backupHash) { // Hashes are different, therefor file has been // modified and needs backing up. addFileToBackupsQueue(currentDirectory, file, 'partial', {current: currentHash, backup: backupHash}); } }); }; const backupFilesFromQueue = (backupFilesQueue) => { backupFilesQueue.forEach((file) => { console.log(JSON.stringify(file, null, 4)); }); console.log(`${backupFilesQueue.length} files to be backed up:`); console.log(`${backupFilesQueue.filter(file => file.backupType === 'full').length} files need a full backup.`); }; let backupFilesQueue = []; // This method is recursive and synchronous getFilesNeedingBackup(currentDirectory, backupDirectory); // Perform backups from queue backupFilesFromQueue(backupFilesQueue); -
James revised this gist
Sep 5, 2016 . 1 changed file with 29 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,34 +14,36 @@ const getFileHash = (filePath) => { }; const getFilesNeedingBackup = (currentDirectory, backupDirectory) => { let files = fs.readdirSync(currentDirectory); files.forEach((file) => { let stats = fs.lstatSync(`${currentDirectory}/${file}`); if (stats.isDirectory()) { getFilesNeedingBackup(`${currentDirectory}/${file}`, `${backupDirectory}/${file}`); return; } if (!fs.existsSync(`${backupDirectory}/${file}`)) { // File is not backed up, therefor file has to be copied // to the backups directory. filesNeedingBackup.push(`${currentDirectory}/${file}`); return; } const currentHash = getFileHash(`${currentDirectory}/${file}`); const backupHash = getFileHash(`${backupDirectory}/${file}`); if (currentHash !== backupHash) { // Hashes are different, therefor file has been // modified and needs backing up. filesNeedingBackup.push(`${currentDirectory}/${file}`); } }); }; let filesNeedingBackup = []; getFilesNeedingBackup(currentDirectory, backupDirectory); console.log(filesNeedingBackup); -
James revised this gist
Sep 5, 2016 . No changes.There are no files selected for viewing
-
James created this gist
Sep 5, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ 'use strict'; const fs = require('fs'); const crypto = require('crypto'); const currentDirectory = `${__dirname}/backup-tests/current`; const backupDirectory = `${__dirname}/backup-tests/backup`; const getFileHash = (filePath) => { const contents = fs.readFileSync(filePath); const hash = crypto.createHash('sha1'); hash.update(contents); return hash.digest('hex') }; const getFilesNeedingBackup = (currentDirectory, backupDirectory) => { fs.readdir(currentDirectory, (err, files) => { files.forEach((file) => { let stats = fs.lstatSync(`${currentDirectory}/${file}`); if (stats.isDirectory()) { getFilesNeedingBackup(`${currentDirectory}/${file}`, `${backupDirectory}/${file}`); return; } if (!fs.existsSync(`${backupDirectory}/${file}`)) { // File is not backed up, therefor file has to be copied // to the backups directory. console.log(`File '${currentDirectory}/${file}' has not been backed up. A backup is required`); return; } const currentHash = getFileHash(`${currentDirectory}/${file}`); const backupHash = getFileHash(`${backupDirectory}/${file}`); if (currentHash !== backupHash) { // Hashes are different, therefor file has been // modified and needs backing up. console.log(`File '${currentDirectory}/${file}' has been modified. A backup is required`); } }); }); }; getFilesNeedingBackup(currentDirectory, backupDirectory);