backup.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="icon" href="favicon.ico" type="image/x-icon">
  5. <title>Backup/Restore Configuration</title>
  6. <meta charset="utf-8">
  7. <style>
  8. h1 {font-size: 2em;}
  9. h2 {font-size: 1.5em;}
  10. h3 {font-size: 1.2em;}
  11. p {font-size: 1em;}
  12. input[type=number] {
  13. width: 138px;
  14. padding: 10px 5px;
  15. display: inline-block;
  16. border: 1px solid #ccc;
  17. font-size: 16px;
  18. }
  19. .button {
  20. padding: 10px 20px;
  21. width: 211px;
  22. font-size: 16px;
  23. }
  24. </style>
  25. </head>
  26. <body style="font-family: arial; padding: 0px 10px;">
  27. <h2>Backup Configuration</h2>
  28. <p>With the following action the <a href="/fileserver/config/" target="_self">config</a> folder on the SD-card gets zipped and provided as a download.</p>
  29. <button class="button" id="startBackup" type="button" onclick="startBackup()">Create Config backup</button>
  30. <p id=progress></p>
  31. <hr>
  32. <h2>Restore Configuration</h2>
  33. <p>Use the <a href="/fileserver/config/" target="_self">File Server</a> to upload individual files.</p>
  34. </body>
  35. <script src="jszip.min.js"></script>
  36. <script src="FileSaver.min.js"></script>
  37. <script>
  38. //var domain = "http://192.168.1.153"; // Testing
  39. var domain = "";
  40. function startBackup() {
  41. document.getElementById("progress").innerHTML = "Creating backup...<br>\n";
  42. // Get hostname
  43. try {
  44. var xhttp = new XMLHttpRequest();
  45. xhttp.open("GET", domain + "/info?type=Hostname", false);
  46. xhttp.send();
  47. hostname = xhttp.responseText;
  48. }
  49. catch(err) {
  50. setStatus("<span style=\"color: red\">Failed to fetch hostname: " + err.message + "!</span>");
  51. return;
  52. }
  53. // get date/time
  54. var dateTime = new Date().toJSON().slice(0,10) + "_" + new Date().toJSON().slice(11,19).replaceAll(":", "-");
  55. zipFilename = hostname + "_" + dateTime + ".zip";
  56. console.log(zipFilename);
  57. // Get files list
  58. setStatus("Fetching File List...");
  59. try {
  60. var xhttp = new XMLHttpRequest();
  61. xhttp.open("GET", domain + "/fileserver/config/", false);
  62. xhttp.send();
  63. var parser = new DOMParser();
  64. var content = parser.parseFromString(xhttp.responseText, 'text/html'); }
  65. catch(err) {
  66. setStatus("Failed to fetch files list: " + err.message);
  67. return;
  68. }
  69. const list = content.querySelectorAll("a");
  70. var urls = [];
  71. for (a of list) {
  72. url = a.getAttribute("href");
  73. urls.push(domain + url);
  74. }
  75. // Pack as zip and download
  76. try {
  77. backup(urls, zipFilename);
  78. }
  79. catch(err) {
  80. setStatus("<span style=\"color: red\">Failed to zip files: " + err.message + "!</span>");
  81. return;
  82. }
  83. }
  84. function fetchFiles(urls, filesData, index, retry, zipFilename) {
  85. url = urls[index];
  86. // console.log(url + " started (" + index + "/" + urls.length + ")");
  87. if (retry == 0) {
  88. setStatus("&nbsp;- " + getFilenameFromUrl(urls[index]) + " (" + index + "/" + urls.length + ")...");
  89. }
  90. else {
  91. setStatus("<span style=\"color: gray\">&nbsp;&nbsp;&nbsp;Retrying (" + retry + ")...</span>");
  92. }
  93. const xhr = new XMLHttpRequest();
  94. xhr.open('GET', url, true);
  95. xhr.responseType = "blob";
  96. if (retry == 0) { // Short timeout on first retry
  97. xhr.timeout = 2000; // time in milliseconds
  98. }
  99. else if (retry == 1) { // longer timeout
  100. xhr.timeout = 5000; // time in milliseconds
  101. }
  102. else { // very long timeout
  103. xhr.timeout = 20000; // time in milliseconds
  104. }
  105. xhr.onload = () => { // Request finished
  106. //console.log(url + " done");
  107. filesData[index] = xhr.response;
  108. if (index == urls.length - 1) {
  109. setStatus("Fetched all files");
  110. generateZipFile(urls, filesData, zipFilename);
  111. return;
  112. }
  113. else { // Next file
  114. fetchFiles(urls, filesData, index+1, 0, zipFilename);
  115. }
  116. };
  117. xhr.ontimeout = (e) => { // XMLHttpRequest timed out
  118. console.log("Timeout on fetching " + url + "!");
  119. if (retry > 5) {
  120. setStatus("<span style=\"color: red\">Backup failed, please restart the device and try again!</span>");
  121. }
  122. else {
  123. fetchFiles(urls, filesData, index, retry+1, zipFilename);
  124. }
  125. };
  126. xhr.send(null);
  127. }
  128. function generateZipFile(urls, filesData, zipFilename) {
  129. setStatus("Creating Zip File...");
  130. var zip = new JSZip();
  131. for (var i = 0; i < urls.length; i++) {
  132. zip.file(getFilenameFromUrl(urls[i]), filesData[i]);
  133. }
  134. zip.generateAsync({type:"blob"})
  135. .then(function(content) {
  136. saveAs(content, zipFilename);
  137. });
  138. setStatus("Backup completed");
  139. }
  140. const backup = (urls, zipFilename) => {
  141. if(!urls) return;
  142. /* Testing */
  143. /*len = urls.length;
  144. for (i = 0; i < len - 3; i++) {
  145. urls.pop();
  146. }*/
  147. console.log(urls);
  148. urlIndex = 0;
  149. setStatus("Fetching files...");
  150. fetchFiles(urls, [], 0, 0, zipFilename);
  151. };
  152. function setStatus(status) {
  153. console.log(status);
  154. document.getElementById("progress").innerHTML += status + "<br>\n";
  155. }
  156. function getFilenameFromUrl(url) {
  157. return filename = url.substring(url.lastIndexOf('/')+1);
  158. }
  159. </script>
  160. </html>