Created
March 11, 2013 09:15
-
-
Save siygle/5132977 to your computer and use it in GitHub Desktop.
Simple script to backup all my stuff
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 characters
| #!/usr/bin/env node | |
| var fs = require('fs'); | |
| function help() { | |
| var output = [ | |
| 'servant - help you handle those tiny little things', | |
| '', | |
| 'Usage:', | |
| ' servant config - Display servant config files', | |
| ' servant backup - Backup your basic config files', | |
| ' servant recover - Recover your config files' | |
| ].join("\n"); | |
| console.log(output); | |
| } | |
| function getConfig() { | |
| var home = process.env.HOME || '/tmp'; | |
| var target = home + '/Dropbox/Backup'; | |
| var setting = { | |
| "backup": [ | |
| {from: home + '/.bashrc', to: target + '/bashrc'}, | |
| {from: home + '/.bash_profile', to: target + '/bash_profile'} | |
| ] | |
| }; | |
| return setting; | |
| } | |
| function backup(type) { | |
| if (type != 'backup' && type != 'recover') return; | |
| var config = getConfig(); | |
| config.backup.forEach(function(c) { | |
| var from, to; | |
| if (type == 'backup') { | |
| from = c.from; | |
| to = c.to; | |
| } else { | |
| from = c.to; | |
| to = c.from; | |
| } | |
| var rStream = fs.createReadStream(c.from); | |
| var wStream = fs.createWriteStream(c.to); | |
| rStream.pipe(wStream); | |
| rStream.on('error', function(e) { | |
| console.error(c.from + " does not exist!"); | |
| process.exit(1); | |
| }); | |
| wStream.on('error', function(e) { | |
| console.error("Backup to " + c.to + " fail!"); | |
| process.exit(1); | |
| }); | |
| }); | |
| } | |
| if (process.argv.length < 3) { | |
| help(); | |
| process.exit(1); | |
| } else { | |
| switch(process.argv[2]) { | |
| case 'config': | |
| break; | |
| case 'backup': | |
| backup('backup'); | |
| break; | |
| case 'recover': | |
| backup('recover'); | |
| break; | |
| default: | |
| console.error('Not support this action!'); | |
| help(); | |
| process.exit(1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment