![]() |
Backing Up and Restoring Databases
|
|
Backing Up a Database
Offline BackupsTo perform an offline backup of a database, simply use operating system commands to copy the database directory. The database must be shut down prior to performing an offline backup. For example, on Windows NT, the following operating system command would back up a (closed) database named toursDB located in d:\mydatabases by copying it to the directory d:\mybackups\1999-01-01: xcopy d:\mydatabases\toursDB c:\mybackups\1999-01-01\toursDB /s /i (If you are not using Windows NT, substitute the appropriate operating system command for copying a directory and all contents to a new location.) NOTE: On Windows NT, do not attempt to update a database while it is being backed up in this way. Attempting to update a database during an off-line backup will generate a java.io.IOException. Using on-line backups prevent this from occurring. For large systems, shutting down the database may not be convenient. To back up a database without having to take it off line, system administrators should use on-line backups, described next. On-line BackupsUse online backups to back up a database while it is booted. During the interval the backup is running, the database can be read, but writes to the database are blocked. There are two ways to perform on-line backups: Using the backup MethodThe backup method locks the database and performs the copy operation. The backup method is located in the COM.cloudscape.database.Database class. When connected to the database you want to back up, use the static method getDatabaseOfConnection in COM.cloudscape.database.Factory (aliased as Factory), to get a Database object for calling this method. The backup method takes a string argument representing the location in which to back up the database. Typically you provide the full path to the backup directory. (Relative paths are interpreted as relative to the current directory, not to the cloudscape.system.home directory.) For example, to specify a backup location of c:/mybackups/1999-01-01 for the currently-open database, you would use the following statement:
CALL Factory.getDatabaseOfConnection().backup( NOTE: Use forward slashes as the path separator in SQL-J commands. The backup method puts the database into a state in which it can be safely copied, then copies the entire original database directory (including data files, on-line transaction log files, and jar files) to the specified backup directory. Files not within the original database directory (for example, cloudscape.properties) are not copied. Here is an excerpt from JBMSTours.AdminHelper, which backs up a database to a directory with a name that reflects the current date: public static void backUpDatabase(Connection conn) throws SQLException { String backupdirectory = "c:/mybackups/" + JCalendar.getToday(); PreparedStatement ps = conn.prepareStatement( "CALL Factory.getDatabaseOfConnection().backup(CAST (? AS java.lang.String))"); ps.setString(1, backupdirectory); ps.executeUpdate(); ps.close(); System.out.println("backed up database to " + backupdirectory); } For a database backed up on 1999-01-01, the above commands copy the current database to a directory of the same name in c:/mybackups/1999-01-01. Uncommitted transactions do not appear in the backed-up database. NOTE: Do not back up different databases with the same name to the same backup directory. If a database of the same name already exists in the backup directory, it is assumed to be an older version and is overwritten. Using Operating System Commands with the freeze and unfreeze MethodsTypically, this procedure is used to speed up the copy operation involved in the on-line backup. In this scenario, Cloudscape does not perform the copy operation for you. You use the freeze method to lock the database, and then explicitly copy the database directory using operating system commands. For example, because the UNIX tar command uses the JDK file-copying routines, it may provide faster backups than the backup method. To use operating system commands for on-line database backups, first use the freeze method located in the COM.cloudscape.database.Database class. The freeze method puts the database into a state in which it can be safely copied. Once the copy is complete, use the unfreeze method to continue working with the database. Only after unfreeze has been specified can transactions once again write to the database. Read operations can proceed while the database is "frozen". NOTE: Since Cloudscape does not use the lock manager for freeze operations, it is imperative that no applications attempt to write to a `frozen' database. If writes are attempted, the requesting application may lock up. As with the backup method, use the static method getDatabaseOfConnection in COM.cloudscape.database.Factory (aliased as Factory), to get a Database object for calling these methods. The following example demonstrates using freeze and unfreeze surrounding an operating system copy command: String backupdirectory = "c:/mybackups/" + JCalendar.getToday(); Statement s = conn.createStatement(); s.executeUpdate( "CALL Factory.getDatabaseOfConnection().freeze()"); //copy the database directory during this interval s.executeUpdate( "CALL Factory.getDatabaseOfConnection().unfreeze()"); s.close(); When the Log is in a non-Default LocationNOTE: Read Logging on a Separate Device to find out about the default location of the database log. If you put the database log in a non-default location prior to backing up the database, note the following:
NOTE: If you are not using Windows NT, substitute the appropriate operating system command for copying a directory and all contents to a new location.
See Chapter 13, "Logging on a Separate Device" for information about putting the log in a non-default location. Backing Up Encrypted DatabasesWhen you back up an encrypted database, both the backup and the log files remain encrypted. To restore an encrypted database, you must know the boot password. NOTE: See the sample program JBMSTours.AdminHelper for an example of backing up a database. |
|
![]() Cloudscape Version 3.6 For information about Cloudscape technical support, go to: www.cloudscape.com/support/.Copyright © 1998, 1999, 2000 Informix Software, Inc. All rights reserved. |