/* * CHCSII-T Oracle Database Backup to Zip Utility * * Description: This utility backs up the TMIP Oracle database files in the following directories to a zip file. * * D:\oracle\ora81\database\PWDLDB.ORA * E:\oracle\oradata\LDB * E:\oracle\admin\LDB\pfile * * NOTE: THESE ARE THE SAME FILES/DIRS THAT THE TMIP BACKUP AND RESTORE UTILITY BACKS UP * * Creates a logfile in the form of OracleBackup_Aug_12_2006.log in the C:\logs\backup folder * * Usage: java OracleBackup [-outputdir C:\dir\to\write\zip\backup | -version | -usage] * * Purpose: To be used with the OracleBackupToZip.bat file so that a scheduled unattended backup of the TMIP Oracle * database can be created. This utility is compatible with the TMIP Backup and Restore Utility. If you wish * to restore a TMIP Oracle Database from the ensuing zip file you can do so with the TMIP Backup and Restore Utility * This utility does not password protect the resulting zip file (as there is no method within the Java zip library to do so). * * NOTE: I hope you have as much fun reading this as I've had writing it! * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * A full copy of this license is at: http://www.gnu.org/licenses/gpl.txt * * Born on : 12 August 2006 * Last Update : 4 November 2006 * * Frank Hale * Sr Network Administrator * General Dynamics Information Technology * Medical Communications for Combat Casualty Care (MC4) * 14th Combat Support Hospital, Bagram Air Field, Afghanistan * */ import java.util.*; import java.util.zip.*; import java.io.*; import java.text.*; public class OracleBackup { // Plain file list with only file names; private ArrayList fileList; // List with path and file names private ArrayList absFileList; // Being a good program we should output some log information to C:\Logs\Backup to record what we've done private BufferedWriter logFile; // To make our zip file compatible with the TMIP Backup and Restore utility we need to create a manifest file private BufferedWriter manifestFile; private String usageString = "Oracle Backup Utility: This utility backs up the TMIP Oracle Database to zip file.\n\nusage:\tjava OracleBackup [-outputdir | -version | -usage]"; private String versionString = "TMIP Oracle Backup - Version 0.6 (4 Nov 2006)\nDescription: This utility backs up the TMIP Oracle database to zip file.\nBy: Frank Hale "; private String logFilePath = "C:\\logs\\backup"; // The manifest file contains the path and file information of the files in the zip. This is necessary to remain // compatible with the TMIP Backup and Restore Utility. private StringBuffer manifestPath = new StringBuffer("C:\\temp"); // I: drive is the default directory to store the Oracle backup zip files private StringBuffer zipRootDir = new StringBuffer("I:"); private Date now = new Date(); // This gives us the date format we use for our filenames which is like: FEB_3_1975 private SimpleDateFormat sdf = new SimpleDateFormat("MMM_d_yyyy"); public OracleBackup(String args[]) { // Stores filenames plus path information for files to be included into zip file fileList = new ArrayList(); // stores absolute name (ie. no path information) absFileList = new ArrayList(); try { // Let's check to see if the manifest path and log path exist, if not create them File mPath = new File(manifestPath.toString()); File lPath = new File(logFilePath.toString()); // We'll use this to check and see if our safe default exists on the filesystem before // we blindly try to use it to write files too. File zPath = new File(zipRootDir.toString()); // If our default output directory is not found then we will default to the C: drive. // // Since MC4 systems are prone to have small C: drive partitions this should probably // check to see if the size of the C: drive is large enough. if(! (zPath.exists())) { zipRootDir = new StringBuffer("C:"); } // Does the manifest path exist? If not create it then check to see if we were successful. if(! (mPath.exists())) { if(! (mPath.mkdir())) { // If we cannot create this directory let's provide a default // The I: drive on MC4 systems is the backup drive. manifestPath = new StringBuffer(zipRootDir.toString()); } } // Does the log path exist on this filesystem? If not let's go make it, we should // probably check to see if making the directory worked before we proceed. // // This needs FIXED. Fortunately that's why GOD invented tomorrow, so we can refresh // our brains and fix the problems we created yesterday! if(! (lPath.exists())) { lPath.mkdir(); } // create our manifest path manifestPath = new StringBuffer(manifestPath.toString() + File.separator + "Manifest.txt"); // Create the log file and the manifest file. logFile = new BufferedWriter(new FileWriter(processFileName(".log"))); manifestFile = new BufferedWriter(new FileWriter(manifestPath.toString())); // Parse the command line arguments and see if we got anything we can use parseCommandLineArgs(args); // Directories and files that the TMIP Backup and Restore utility captures in its // backup of the II-T Oracle database. processFiles(new File("D:\\oracle\\ora81\\database\\PWDLDB.ORA")); processFiles(new File("E:\\oracle\\oradata\\ldb")); processFiles(new File("E:\\oracle\\admin\\ldb\\pfile")); // Include the manifest file into the zip backup processFiles(new File(manifestPath.toString())); // convert the ArrayList containers to a standard Java Array String files[] = (String[]) fileList.toArray(new String [fileList.size()]); String absFiles[] = (String[]) absFileList.toArray(new String [absFileList.size()]); // Write out the manifest file with the contents of our zip with path information for(int i=0;i 1) { tempOutputDir = new StringBuffer(args[++i]); } File outputDir = new File(tempOutputDir.toString()); // The way we are using this utility is to backup to NAS which is connected via a shared mapped drive // sometimes this mapped drive fails to reconnect after rebooting the server. In the event that we cannot // connect to the NAS we'd like to backup to the known safe backup directory which is present on all // MC4 images. This location is the I: drive. // // So here we check to see if the output directory passed as an argument exists, if it does exist we // do further testing to see if it's a directory or if it's a file. if(! (outputDir.exists())) { String err = "Warning: Command line argument passed for the output directory does not exist, falling back to " + zipRootDir; System.out.println(err + "\n"); logFileWrite(err); logFileWriteNewLine(); logFileWriteNewLine(); } else { // verify that this is a valid directory if (outputDir.isDirectory()) { zipRootDir = new StringBuffer(tempOutputDir.toString()); } else { // Let's make sure this isn't a file if(outputDir.isFile()) { String err = "Warning: Directory passed as argument is a file and cannot be used for saving backups to, falling back to " + zipRootDir; // Directory passed as arg is a file, use default directory instead System.out.println(err + "\n"); logFileWrite(err); logFileWriteNewLine(); logFileWriteNewLine(); } } } } if((args[i].equals("-usage")) || (args[i].equals("--usage"))) { System.out.println(usageString.toString()); System.exit(0); } // -version represents the version string associated with this program if ((args[i].equals("-version")) || (args[i].equals("--version"))) { System.out.println(versionString.toString()); System.exit(0); } } } // The two methods below pertaining to log writing are convenience methods for easier // writing of information to the log files. public void logFileWriteNewLine() { try { logFile.newLine(); } catch (IOException ioe) { System.out.println(ioe); } } public void logFileWrite(String msg) { try { logFile.write(msg); } catch (IOException ioe) { System.out.println(ioe); } } // The manifest file just lists what the zip file contains, it's a temporary file that we create on the // C drive then after we add it to the zip file we can delete it safely from the file system. public void deleteTempManifestFile() { boolean success = (new File(manifestPath.toString())).delete(); if (!success) { System.out.println("Deletion of the temporary manifest file (" + manifestPath.toString() + ") failed."); } } // Gets a list of files from a directory and adds it to the file lists. public void processFiles(File dir) { if (dir.isDirectory()) { String children[] = dir.list(); for (int i=0; i 0) { zout.write(buf, 0, len); } zout.closeEntry(); in.close(); } zout.close(); } catch (IOException ioe) { System.out.println("Oracle Backup (zip file creation): " + ioe); } } // Return the current date using our date format. public String getDateString() { return new StringBuffer("Date: " + sdf.format(now)).toString(); } // Build us a filename plus path information for either a log file or a zip file. public String processFileName(String ext) { if(ext.equals(".zip")) { return new StringBuffer(zipRootDir + File.separator + "OracleBackup_" + sdf.format(now)).toString() + ext; } else if(ext.equals(".log")) { return new StringBuffer(logFilePath + File.separator + "OracleBackup_" + sdf.format(now)).toString() + ext; } return null; } public static void main(String args[]) { // This creates a new instance of this program and passes it the command line arguments // that may or may not have been provided when running the application. new OracleBackup(args); } } // WOW, if you made it this far then you must really be interested in this...