[opencms-dev] backup-edits.jsp
Marko Riedel
markoriedelde at yahoo.de
Sat May 28 02:42:57 CEST 2011
Hello there,
I'd like to share a script and ask for your opinion. Since it's only a couple of weeks that I've been working with OpenCMS I am very slowly mastering its intricacies and learning its structures and ways. It happens to me quite often that I feel the need of having a backup of my files in the file system in addition to the data in the OpenCMS database, just to be on the safe side. We corrupted our MySQL installation some weeks ago and had to redo a lot of work that was lost because there was no other backup. With this in mind I wrote a JSP that takes a directory and some files given by their VFS paths and saves the files to that directory in a folder labeled with the current date. The JSP is included with this message. Feedback is welcome, including of course suggestions of more elegant backup solutions, which I suspect are already out there, like "mysqldump".
Best regards,
Marko Riedel
+---------------------------------------------------+
| Marko Riedel, markoriedelde at yahoo.de |
| http://www.mathematik.uni-stuttgart.de/~riedelmo/ |
+---------------------------------------------------+
<%@ page session="false" %>
<%@ page import="org.apache.commons.io.FileUtils"%>
<%@ page import="org.opencms.frontend.templateone.CmsTemplateBean"%>
<%@ page import="org.opencms.i18n.CmsMessages"%>
<%@ page import="java.util.*,
java.io.*,
java.text.SimpleDateFormat,
org.opencms.jsp.*,
org.opencms.file.*,
org.opencms.util.*"
%>
<HTML>
<HEAD><TITLE>Backup edits</TITLE></HEAD>
<BODY>
<%
String backupDir = "/home/marko/backup-opencms";
String files[] = {"/sites/default/demo_en/index.html",
"/sites/default/backup-edits.jsp",
null};
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();
cmso.getRequestContext().setSiteRoot("/");
Locale locale = cmso.getRequestContext().getLocale();
final String DATE_FORMAT = "yyyy-MM-dd";
Date today = new Date();
SimpleDateFormat dFormat = new SimpleDateFormat(DATE_FORMAT, locale);
String backupRoot = backupDir + "/" + dFormat.format(today);
try {
File dirinf = new File(backupRoot);
if(!dirinf.exists() && !dirinf.mkdirs()){
throw new Exception("failed to create " + backupRoot);
}
%>
<H1>List of files backed up to <% out.print(backupRoot); %></H1>
<%
int i = 0;
while(files[i] != null){
out.println("<H2>" + files[i] + "</H2>\n");
File file = new File(backupRoot + files[i]);
dirinf = new File(file.getParent());
if(!dirinf.exists() && !dirinf.mkdirs()){
throw new Exception("failed to create " + dirinf);
}
String data = new String(cmso.readFile(files[i]).getContents());
FileUtils.writeStringToFile(file, data);
i++;
}
}
catch (Exception ex){
out.println(ex.getMessage());
}
%>
</BODY>
</HTML>
More information about the opencms-dev
mailing list