001: /*
002: * Created on May 17, 2006
003: */
004: package com.openedit.modules.update;
005:
006: import java.io.File;
007: import java.io.FileOutputStream;
008: import java.io.FilenameFilter;
009: import java.io.IOException;
010: import java.text.SimpleDateFormat;
011: import java.util.ArrayList;
012: import java.util.Collections;
013: import java.util.Date;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: import com.openedit.OpenEditException;
021: import com.openedit.page.manage.PageManager;
022: import com.openedit.util.FileUtils;
023: import com.openedit.util.PageZipUtil;
024: import com.openedit.util.ZipUtil;
025:
026: public class Backup {
027: protected PageManager fieldPageManager;
028: protected SimpleDateFormat fieldFormat = new SimpleDateFormat(
029: "yyyy_MM_dd_HH_mm_ss");
030: protected File fieldRoot;
031: protected String fieldIncludePath = "/";
032: protected FileUtils fieldUtils = new FileUtils();
033: protected List fieldExcludes = new ArrayList();
034: private static final Log log = LogFactory.getLog(Backup.class);
035:
036: public PageManager getPageManager() {
037: return fieldPageManager;
038: }
039:
040: public void setPageManager(PageManager inPageManager) {
041: fieldPageManager = inPageManager;
042: }
043:
044: protected File backupCurrentSite(String inName)
045: throws OpenEditException {
046: PageZipUtil zip = new PageZipUtil(getPageManager());
047: zip.setRoot(getRoot());
048: for (Iterator iter = getExcludes().iterator(); iter.hasNext();) {
049: String exclude = (String) iter.next();
050: zip.addExclude(exclude);
051: }
052: // zip.addExclude("*/WEB-INF/*");
053: // zip.addExclude("*/.versions/*");
054: // zip.addExclude("*/WEB-INF/trash/*");
055: // zip.addExclude("*/WEB-INF/tmp/*");
056: // zip.addExclude("*/WEB-INF/log*");
057:
058: inName = inName.replace(" ", "_");
059: inName = inName.replace("/", "_");
060: inName = inName.replace("\\", "_");
061:
062: String id = fieldFormat.format(new Date()) + "_" + inName;
063: String outpath = "/WEB-INF/versions/" + id + ".zip";
064: zip.addExclude(outpath);
065: File out = new File(getRoot(), outpath);
066: log.info("Backing up " + out);
067: try {
068: out.getParentFile().mkdirs();
069: FileOutputStream stream = new FileOutputStream(out);
070: try {
071: zip.zipFile(getIncludePath(), stream);
072: } finally {
073: FileUtils.safeClose(stream);
074: }
075: return out;
076: } catch (IOException ex) {
077: throw new OpenEditException(ex);
078: }
079: }
080:
081: public File getRoot() {
082: return fieldRoot;
083: }
084:
085: public void setRoot(File inRoot) {
086: fieldRoot = inRoot;
087: }
088:
089: public List listSiteVersions() {
090: File verdir = new File(getRoot(), "WEB-INF/versions");
091: verdir.mkdirs();
092:
093: File[] children = verdir.listFiles(new FilenameFilter() {
094: public boolean accept(File inDir, String inName) {
095: return inName.endsWith(".zip");
096: }
097: });
098: List list = new ArrayList();
099: if (children != null) {
100: for (int i = 0; i < children.length; i++) {
101: File child = children[i];
102: list.add(child);
103: }
104: }
105: Collections.sort(list);
106: return list;
107: }
108:
109: public File loadVersion(String inName) {
110: List list = listSiteVersions();
111: for (Iterator iter = list.iterator(); iter.hasNext();) {
112: File version = (File) iter.next();
113: if (version.getName().equals(inName)) {
114: return version;
115: }
116: }
117: return null;
118: }
119:
120: protected void restoreBackup(File inVersion)
121: throws OpenEditException {
122: log.info("Restoring " + inVersion.getName());
123: File tmp = null;
124: try {
125: tmp = new File(getRoot(), "WEB-INF/trash/new"
126: + fieldFormat.format(new Date()));
127: tmp.mkdirs();
128:
129: ZipUtil utils = new ZipUtil();
130: //unzip the zip file in a tmp directory
131: utils.unzip(inVersion, tmp);
132: } catch (IOException ex) {
133: throw new OpenEditException("No harm done", ex);
134: }
135: File old = new File(getRoot(), "WEB-INF/trash/old"
136: + fieldFormat.format(new Date()));
137:
138: try {
139: replaceDirectories(tmp, getRoot(), old);
140: } catch (IOException ex) {
141: throw new OpenEditException(ex);
142: }
143:
144: // try
145: // {
146: // File tmpold = null;
147: // }
148: // finally
149: // {
150: // if( !tmp.renameTo(getRoot()) )
151: // {
152: // //copy it
153: // try
154: // {
155: // log.error("Could not rename");
156: // //org.apache.commons.io.FileUtils.copyFile(tmp, getRoot(), true);
157: // new FileUtils().copyFiles(tmp, getRoot() );
158: // }
159: // catch ( IOException ex)
160: // {
161: // throw new OpenEditException(ex);
162: // }
163: // }
164: // //bring back the versions
165: // File versions = new File( tmpold , "WEB-INF/versions");
166: // File newversions = new File( getRoot(), "WEB-INF/versions");
167: // versions.renameTo(newversions);
168: // }
169: }
170:
171: /**
172: * This method does a replacement of top level directories.
173: * One exception is the WEB-INF directory that it will go into
174: * @param inNewDirs
175: * @param inRoot
176: * @param inSubPath
177: * @param inOldDirectory
178: * @throws IOException
179: */
180: protected void replaceDirectories(File inNewDirs, File inRoot,
181: File inOldDirectory) throws IOException {
182: //move the existing content to tmp2
183: // tmpold = File.createTempFile("upgradeold", "");
184: // tmpold.delete();
185: // tmpold.mkdirs();
186: File[] children = inNewDirs.listFiles();
187: for (int i = 0; i < children.length; i++) {
188: File child = children[i];
189: File existing = new File(inRoot, child.getName());
190: if (existing.exists()) {
191: //Then move it into away
192:
193: if (child.getName().equals("WEB-INF")) {
194: //replaceDirectories(child, existing, inOldDirectory);
195: continue;
196: } else {
197: if (existing.isDirectory()) {
198: fieldUtils.move(existing, new File(
199: inOldDirectory, existing.getName()));
200: } else {
201: //this is an existing file in the inNewDirs directory
202: File backup = new File(inOldDirectory, child
203: .getName());
204: if (!existing.renameTo(backup)) {
205: throw new IOException("Could not move "
206: + existing.getPath() + " to "
207: + backup.getPath());
208: }
209: }
210: }
211: }
212: //Now replace it
213: fieldUtils
214: .move(child, new File(getRoot(), child.getName()));
215: //child.renameTo(new File( getRoot(), child.getName() ));
216:
217: }
218: //TODO: Do this in smaller parts so we can exclude WEB-INF/logs/
219: // if ( !getRoot().renameTo(tmpold) )
220: // {
221: // log.info("Had to copy manually");
222: // FileUtils fu = new FileUtils();
223: // fu.copyFiles(getRoot(), tmpold );
224: // fu.deleteAll(getRoot());
225: // }
226: // File[] all = tmpold.listFiles();
227: // if ( all.length == 0)
228: // {
229: // throw new OpenEditException("Problem: Could not move entire existing site. Some files moved to: " + tmpold.getPath());
230: // }
231:
232: }
233:
234: public String getIncludePath() {
235: return fieldIncludePath;
236: }
237:
238: public void setIncludePath(String inIncludePath) {
239: fieldIncludePath = inIncludePath;
240: }
241:
242: public List getExcludes() {
243: if (fieldExcludes == null) {
244: fieldExcludes = new ArrayList();
245: }
246: return fieldExcludes;
247: }
248:
249: public void setExcludes(List inExcludes) {
250: this .fieldExcludes = inExcludes;
251: }
252:
253: public void addExclude(String inExclude) {
254: getExcludes().add(inExclude);
255: }
256:
257: }
|