Skip to content

Instantly share code, notes, and snippets.

@typcn
Created February 18, 2018 11:39
Show Gist options
  • Save typcn/9cb8da3fc97cabb1692f1938e777d8f3 to your computer and use it in GitHub Desktop.
Save typcn/9cb8da3fc97cabb1692f1938e777d8f3 to your computer and use it in GitHub Desktop.
Restore file from nginx cache
const fs = require('fs');
let text = fs.readFileSync('listcachefile.txt').toString();
let a = text.split(' TTL:');
for(let i = 0;i < a.length;i++){
let t = a[i].split('\n');
let file = t[0].replace('file:','');
let url = t[1].split('http://cache-i/');
if(file.indexOf('EXPIRE') > -1){
if(t[1] == ''){
continue;
}
file = t[1].replace('file:','');
url = t[2].split('http://cache-i/');
}
url = url[1];
if(url.indexOf('?') > -1){
continue;
}
if(url.length != 36){
continue;
}
console.log('Restore File: ',url,file);
let cacheContent = fs.readFileSync(file.split('/var')[1].replace('/cdn','/var/cdn'))
cacheContent = cacheContent.slice(cacheContent.indexOf('\r\n\r\n')+4);
fs.writeFileSync('tmp/' + url,cacheContent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment