Backup Manager
These scripts allow you to create a handy interface for managing Confluence nightly backups via the web browser...
These three scripts work together to provide:
- A macro which lists available backups and any associated options
- A servlet to download backup files
- A servlet to delete backup files
The scripts also act as a really useful tutorial because the show a number of extremely useful tricks that can be done with Scriptix. For example, the "Download" and "Delete" links only appear in the list of backups if the corresponding servlet scripts are installed.
Requirements
None at present.
Script: backupList
This script lists available backups and provides links to the servlet scripts for downloading or deleting backups.
Name: [backupList|^export-backupList.xml]
Author(s): Dan Hardiker
Scriptix Version: 1.1.2 or above
Scripting Engine: Mozilla Rhino
Components: bootstrapManager
Vectors: Wiki Macro
View source...
/** Convert an epoch time in miliseconds to a java Date, and format */
var timeToDate = function (time) {
return scriptix.formatDate(new java.util.Date(time), "dd-MM-yyyy [hh:mm]");
};
/** A list of byte-based units */
var byteUnits = ["b","k","m","g","t","p"];
/** Find the next unit */
var nextUnit = function (unit) {
for (var i=0; i<byteUnits.length; i++) {
if (byteUnits[i] == unit && i+1 < byteUnits.length) return byteUnits[i+1];
}
return "?";
};
/** Convert a size in bytes to a human readable figure */
var convertByteSize = function (size, unit) {
if (size < 1024) {
return (Math.round(size*10)/10) + unit;
} else {
return convertByteSize(size/1024, nextUnit(unit));
}
};
var runScript = function () {
var homeDir = scriptix.getComponent("bootstrapManager").getConfluenceHome();
var backupFile = new java.io.File(homeDir + java.io.File.separator + "backups");
var downloadInstalled = scriptix.isConfigUsed("backupDownloadServlet");
var downloadPath = "/plugins/servlet/scriptix/simple/backupDownloadServlet/";
var deleteInstalled = scriptix.isConfigUsed("backupDeleteServlet");
var deletePath = "/plugins/servlet/scriptix/simple/backupDeleteServlet/";
if (!backupFile.exists() || !backupFile.isDirectory()) {
return "Unable to find the backup directory!";
}
var fileList = backupFile.listFiles();
var actions = (downloadInstalled || deleteInstalled) ? "<th class='confluenceTh'>Actions</th>" : "";
var table = "<table class='confluenceTable'>";
table += "<tr><th class='confluenceTh'>Filename</th><th class='confluenceTh'>Date</th><th class='confluenceTh'>Size</th>"+ actions +"</tr>";
for (var i=0; i<fileList.length; i++) {
var eachFile = fileList[i];
table += "<tr>";
table += "<td class='confluenceTd'>"+ eachFile.getName() +"</td>";
table += "<td class='confluenceTd'>"+ timeToDate( eachFile.lastModified() ) +"</td>";
table += "<td class='confluenceTd'>"+ convertByteSize(eachFile.length(), "b") +"</td>";
if (actions) {
var actionCount = 0;
table += "<td class='confluenceTd'>";
if (downloadInstalled) {
table += "<a href='"+ downloadPath + eachFile.getName() +"'>download</a>";
actionCount++;
}
if (deleteInstalled) {
if (actionCount++ > 0) table += " | ";
table += "<a href='"+ deletePath + eachFile.getName() +"' onclick='if (!confirm(\'Are you sure you want to delete "+ eachFile.getName() +"?\') return false;'>delete</a>";
}
table += "</td>";
}
table += "</tr>";
}
return table += "</table>";
}
// Execute
output = runScript();
To display the list of backups, just add this to a page in your wiki:
{scriptix:id=backupList}{scriptix}
Script: backupDownloadServlet
This simple servlet streams the requested backup file. If you don't install this script the "Download" option will not be shown in the output of the backupList script.
Name: [backupDownloadServlet|^export-backupDownloadServlet.xml]
Author(s): Dan Hardiker
Scriptix Version: 1.1.2 or above
Scripting Engine: Mozilla Rhino
Components: bootstrapManager
Vectors: Simple Servlet
View source...
var runScript = function () {
if (!scriptix.isGlobalAdminOrSuperUser(user)) {
return "You must be an admin to access this page.";
}
var filename = servlet.request.getPathInfo().substring( servlet.request.getPathInfo().lastIndexOf("/") + 1 );
var homeDir = scriptix.getComponent("bootstrapManager").getConfluenceHome();
var backupFile = new java.io.File(homeDir + java.io.File.separator + "backups" + java.io.File.separator + filename);
if (!backupFile.exists() || !backupFile.isFile()) {
return "Unable to find the backup file to download!";
}
servlet.setSuppressingOutput(true);
servlet.response.setContentType( scriptix.getContentType( backupFile.getName() ) );
scriptix.streamFileContents(backupFile, servlet.response.getOutputStream());
}
runScript();
Script: backupDeleteServlet
This simple servlet deletes the specified backup file. If you don't install this script the "Delete" option will not be shown in the output of the backupList script.
Name: [backupDeleteServlet|^export-backupDeleteServlet.xml]
Author(s): Dan Hardiker
Scriptix Version: 1.1.2 or above
Scripting Engine: Mozilla Rhino
Components: bootstrapManager
Vectors: Simple Servlet
View source...
var runScript = function () {
if (!scriptix.isGlobalAdminOrSuperUser(user)) {
return "You must be an admin to access this page.";
}
var filename = servlet.request.getPathInfo().substring( servlet.request.getPathInfo().lastIndexOf("/") + 1 );
var homeDir = scriptix.getComponent("bootstrapManager").getConfluenceHome();
var backupFile = new java.io.File(homeDir + java.io.File.separator + "backups" + java.io.File.separator + filename);
if (!backupFile.exists() || !backupFile.isFile()) {
return "Unable to find the backup file to delete!";
}
backupFile["delete"]();
servlet.setSuppressingOutput(true);
servlet.response.sendRedirect( servlet.request.getHeader("Referer") );
}
runScript();
Known issues
- The backupList script makes the assumption that Confluence is running as root. If you run Confluence in a subfolder, the URL paths will need changing.
To-Do
- Use more dynamic path so if Confluence is not root the extra folder path elements get added to the URL.
Hi, it seems that the links to the xml files are broken. I just see text like this, which I can't click on to download anything:
[backupDeleteServlet|^export-backupDeleteServlet.xml]
Also, is there some Java source code that we're not seeing for the servlet plugins?
Thanks