1 4 package com.openedit.modules.update; 5 6 import java.io.File ; 7 import java.io.FileOutputStream ; 8 import java.io.FilenameFilter ; 9 import java.io.IOException ; 10 import java.text.SimpleDateFormat ; 11 import java.util.ArrayList ; 12 import java.util.Collections ; 13 import java.util.Date ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 20 import com.openedit.OpenEditException; 21 import com.openedit.page.manage.PageManager; 22 import com.openedit.util.FileUtils; 23 import com.openedit.util.PageZipUtil; 24 import com.openedit.util.ZipUtil; 25 26 public class Backup 27 { 28 protected PageManager fieldPageManager; 29 protected SimpleDateFormat fieldFormat = new SimpleDateFormat ("yyyy_MM_dd_HH_mm_ss"); 30 protected File fieldRoot; 31 protected String fieldIncludePath = "/"; 32 protected FileUtils fieldUtils = new FileUtils(); 33 private static final Log log = LogFactory.getLog(Backup.class); 34 35 public PageManager getPageManager() 36 { 37 return fieldPageManager; 38 } 39 public void setPageManager(PageManager inPageManager) 40 { 41 fieldPageManager = inPageManager; 42 } 43 44 protected File backupCurrentSite(String inName) throws OpenEditException 45 { 46 PageZipUtil zip = new PageZipUtil(getPageManager()); 47 zip.setRoot(getRoot()); 48 zip.addExclude("*/WEB-INF/*"); 49 zip.addExclude("*/.versions/*"); 50 54 inName = inName.replace(" ", "_"); 55 inName = inName.replace("/", "_"); 56 inName = inName.replace("\\", "_"); 57 58 String id = fieldFormat.format(new Date () ) + "_" + inName; 59 File out = new File ( getRoot() , "WEB-INF/versions/" + id + ".zip" ); 60 log.info("Backing up " + out); 61 try 62 { 63 out.getParentFile().mkdirs(); 64 FileOutputStream stream = new FileOutputStream (out); 65 try 66 { 67 zip.zipFile(getIncludePath(), stream ); 68 } 69 finally 70 { 71 FileUtils.safeClose(stream); 72 } 73 return out; 74 } 75 catch ( IOException ex) 76 { 77 throw new OpenEditException(ex); 78 } 79 } 80 public File getRoot() 81 { 82 return fieldRoot; 83 } 84 public void setRoot(File inRoot) 85 { 86 fieldRoot = inRoot; 87 } 88 public List listSiteVersions() 89 { 90 File verdir = new File ( getRoot() , "WEB-INF/versions"); 91 verdir.mkdirs(); 92 93 File [] children = verdir.listFiles(new FilenameFilter () { 94 public boolean accept(File inDir, String inName) 95 { 96 return inName.endsWith(".zip"); 97 } 98 }); 99 List list = new ArrayList (); 100 if ( children != null) 101 { 102 for (int i = 0; i < children.length; i++) 103 { 104 File child = children[i]; 105 list.add(child); 106 } 107 } 108 Collections.sort(list); 109 return list; 110 } 111 public File loadVersion(String inName) 112 { 113 List list = listSiteVersions(); 114 for (Iterator iter = list.iterator(); iter.hasNext();) 115 { 116 File version= (File ) iter.next(); 117 if ( version.getName().equals( inName )) 118 { 119 return version; 120 } 121 } 122 return null; 123 } 124 protected void restoreBackup(File inVersion) throws OpenEditException 125 { 126 log.info("Restoring " + inVersion.getName() ); 127 File tmp = null; 128 try 129 { 130 tmp = File.createTempFile("upgrade", ""); 131 tmp.delete(); 132 tmp.mkdirs(); 133 134 ZipUtil utils = new ZipUtil(); 135 utils.unzip(inVersion,tmp); 137 } 138 catch (IOException ex ) 139 { 140 throw new OpenEditException("No harm done", ex); 141 } 142 File old = new File ( getRoot(),"WEB-INF/trash/" + fieldFormat.format(new Date () )); 143 144 try 145 { 146 replaceDirectories(tmp,getRoot(),old); 147 } 148 catch (IOException ex ) 149 { 150 throw new OpenEditException("Some files could not be moved. Backup located in " + old.getPath() ); 151 } 152 153 154 155 } 181 190 protected void replaceDirectories(File inNewDirs, File inRoot, File inOldDirectory ) throws IOException 191 { 192 File [] children = inNewDirs.listFiles(); 197 for (int i = 0; i < children.length; i++) 198 { 199 File child = children[i]; 200 File existing = new File ( inRoot, child.getName() ); 201 if( existing.exists() ) 202 { 203 205 if( child.getName().equals("WEB-INF")) 206 { 207 continue; 209 } 210 else 211 { 212 if( existing.isDirectory() ) 213 { 214 fieldUtils.move(existing, new File ( inOldDirectory, existing.getName() )); 215 } 216 else 217 { 218 File backup = new File ( inOldDirectory, child.getName() ); 220 existing.renameTo(backup); 221 } 222 } 223 } 224 fieldUtils.move(child, new File ( getRoot(), child.getName() ) ); 226 228 } 229 243 } 244 public String getIncludePath() 245 { 246 return fieldIncludePath; 247 } 248 public void setIncludePath(String inIncludePath) 249 { 250 fieldIncludePath = inIncludePath; 251 } 252 } 253 | Popular Tags |