Skip to content

Instantly share code, notes, and snippets.

@joshstrange
Created June 25, 2019 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshstrange/8b32a7653114460c7554eaf9b631d808 to your computer and use it in GitHub Desktop.
Save joshstrange/8b32a7653114460c7554eaf9b631d808 to your computer and use it in GitHub Desktop.
Backup your Unraid USB remotely. Just replace the hostname, username, password at the top of the file with your info, make sure `request` and `request-promise-native` are installed and then just run it with `node index.js`
const request = require('request-promise-native');
const fs = require('fs');
let hostname = 'http://unraid-url.com';
let username = 'root';
let password = 'mypassword';
(async () => {
// Get CSRF Token
process.stdout.write("Getting CRSF Token...");
let csrfRegex = /name:'csrf_token', value:'(.*?)'/;
let csrfResponse = await request({
method: 'GET',
url: hostname,
auth: {
user: username,
pass: password
}
});
let token = csrfRegex.exec(csrfResponse)[1];
console.log(" Done! (" + token + ")");
// Generate backup
let timer = setInterval(() => {
process.stdout.write(".")
}, 1000);
process.stdout.write("Generating backup Zip...");
let zipPath = await request({
method: 'POST',
url: hostname + '/webGui/include/Download.php',
form: {
cmd: 'backup',
csrf_token: token
},
auth: {
user: username,
pass: password
}
});
clearInterval(timer);
console.log(" Done! (" + zipPath + ")");
// Download backup
let totalBytes = 0;
let receivedBytes = 0;
let lastPercent = 0;
process.stdout.write("Downloading... ");
request({
method: 'GET',
url: hostname + '/' + zipPath,
auth: {
user: username,
pass: password
},
})
.on('error', (err) => {
console.error(err);
})
.on('response', (data) => {
totalBytes = parseInt(data.headers['content-length']);
})
.on('data', (chunk) => {
receivedBytes += chunk.length;
var percentage = Math.round((receivedBytes * 100) / totalBytes);
if(percentage > lastPercent) {
lastPercent = percentage;
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write("Downloading... (" + Math.round( totalBytes / (1024 * 1024)) + "MB) " + percentage + "%");
}
})
.on('end', async () => {
console.log(" Done!");
// Delete backup
process.stdout.write("Deleting zip off server...");
await request({
method: 'POST',
url: hostname + '/webGui/include/Download.php',
form: {
cmd: 'unlink',
file: zipPath,
csrf_token: token
},
auth: {
user: username,
pass: password
}
});
console.log("Done!");
})
.pipe(fs.createWriteStream(zipPath));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment