Source Code Cross Referenced for Backup.java in  » Database-DBMS » h2database » org » h2 » tools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » h2database » org.h2.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003:         * (http://h2database.com/html/license.html).
004:         * Initial Developer: H2 Group
005:         */
006:        package org.h2.tools;
007:
008:        import java.io.FileNotFoundException;
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.io.OutputStream;
012:        import java.sql.SQLException;
013:        import java.util.ArrayList;
014:        import java.util.zip.ZipEntry;
015:        import java.util.zip.ZipOutputStream;
016:
017:        import org.h2.command.dml.BackupCommand;
018:        import org.h2.engine.Constants;
019:        import org.h2.message.Message;
020:        import org.h2.store.FileLister;
021:        import org.h2.util.FileUtils;
022:        import org.h2.util.IOUtils;
023:
024:        /**
025:         * Backs up a H2 database by creating a .zip file from the database files.
026:         */
027:        public class Backup {
028:
029:            private void showUsage() {
030:                System.out
031:                        .println("java "
032:                                + getClass().getName()
033:                                + " [-file <filename>] [-dir <dir>] [-db <database>] [-quiet]");
034:                System.out
035:                        .println("See also http://h2database.com/javadoc/org/h2/tools/Backup.html");
036:            }
037:
038:            /**
039:             * The command line interface for this tool.
040:             * The options must be split into strings like this: "-db", "test",...
041:             * Options are case sensitive. The following options are supported:
042:             * <ul>
043:             * <li>-help or -? (print the list of options)
044:             * </li><li>-file filename (the default is backup.zip)
045:             * </li><li>-dir database directory (the default is the current directory)
046:             * </li><li>-db database name (not required if there is only one database)
047:             * </li><li>-quiet does not print progress information
048:             * </li></ul>
049:             *
050:             * @param args the command line arguments
051:             * @throws SQLException
052:             */
053:            public static void main(String[] args) throws SQLException {
054:                new Backup().run(args);
055:            }
056:
057:            private void run(String[] args) throws SQLException {
058:                String zipFileName = "backup.zip";
059:                String dir = ".";
060:                String db = null;
061:                boolean quiet = false;
062:                for (int i = 0; args != null && i < args.length; i++) {
063:                    if (args[i].equals("-dir")) {
064:                        dir = args[++i];
065:                    } else if (args[i].equals("-db")) {
066:                        db = args[++i];
067:                    } else if (args[i].equals("-quiet")) {
068:                        quiet = true;
069:                    } else if (args[i].equals("-file")) {
070:                        zipFileName = args[++i];
071:                    } else {
072:                        showUsage();
073:                        return;
074:                    }
075:                }
076:                Backup.execute(zipFileName, dir, db, quiet);
077:            }
078:
079:            /**
080:             * Backs up database files.
081:             *
082:             * @param zipFileName the name of the backup file
083:             * @param directory the directory name
084:             * @param db the database name (null if there is only one database)
085:             * @param quiet don't print progress information
086:             * @throws SQLException
087:             */
088:            public static void execute(String zipFileName, String directory,
089:                    String db, boolean quiet) throws SQLException {
090:                ArrayList list = FileLister.getDatabaseFiles(directory, db,
091:                        true);
092:                if (list.size() == 0) {
093:                    if (!quiet) {
094:                        System.out.println("No database files found");
095:                    }
096:                    return;
097:                }
098:                zipFileName = FileUtils.normalize(zipFileName);
099:                if (FileUtils.exists(zipFileName)) {
100:                    FileUtils.delete(zipFileName);
101:                }
102:                OutputStream out = null;
103:                try {
104:                    out = FileUtils.openFileOutputStream(zipFileName, false);
105:                    ZipOutputStream zipOut = new ZipOutputStream(out);
106:                    String base = "";
107:                    for (int i = 0; i < list.size(); i++) {
108:                        String fileName = (String) list.get(i);
109:                        if (fileName.endsWith(Constants.SUFFIX_DATA_FILE)) {
110:                            base = FileUtils.getParent(fileName);
111:                        }
112:                    }
113:                    for (int i = 0; i < list.size(); i++) {
114:                        String fileName = (String) list.get(i);
115:                        String f = FileUtils.getAbsolutePath(fileName);
116:                        if (!f.startsWith(base)) {
117:                            throw Message.getInternalError(f
118:                                    + " does not start with " + base);
119:                        }
120:                        f = f.substring(base.length());
121:                        f = BackupCommand.correctFileName(f);
122:                        ZipEntry entry = new ZipEntry(f);
123:                        zipOut.putNextEntry(entry);
124:                        InputStream in = null;
125:                        try {
126:                            in = FileUtils.openFileInputStream(fileName);
127:                            IOUtils.copyAndCloseInput(in, zipOut);
128:                        } catch (FileNotFoundException e) {
129:                            // the file could have been deleted in the meantime
130:                            // ignore this (in this case an empty file is created)
131:                        } finally {
132:                            IOUtils.closeSilently(in);
133:                        }
134:                        zipOut.closeEntry();
135:                        if (!quiet) {
136:                            System.out.println("processed: " + fileName);
137:                        }
138:                    }
139:                    zipOut.closeEntry();
140:                    zipOut.close();
141:                } catch (IOException e) {
142:                    throw Message.convertIOException(e, zipFileName);
143:                } finally {
144:                    IOUtils.closeSilently(out);
145:                }
146:            }
147:
148:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.