![]() | ![]() |
Home |
|
|
|
| Chapter 2: The Embedded SQL Interface |
|
| Library functions |
|
| Backup functions |
The db_backup function provides support for online backup. The Adaptive Server Anywhere backup utility makes use of this function. You should only need to write a program to use this function if your backup requirements are not satisfied by the Adaptive Server Anywhere backup utility.
You can also access the backup utility directly using the Database Tools DBBackup function. For more information on this function, see DBBackup function .
Every database contains one or more files. Normally, a database contains two files: the main database file and the transaction log.
Each file is divided into fixed size pages, and the size of these pages is specified when the database is created.
Backup works by opening a file, and then making a copy of each page in the file. Backup performs a checkpoint on startup, and the database files are backed up as of this checkpoint. Any changes that are made while the backup is running are recorded in the transaction log, and are backed up with the transaction log. This is why the transaction log is always backed up last.
You must be connected to a user ID with DBA authority or REMOTE DBA authority (SQL Remote) to use the backup functions.
void db_backup( struct sqlca * sqlca, int op, int file_num, unsigned long page_num, struct sqlda * sqlda);
Must be connected to a user ID with DBA authority or REMOTE DBA authority (SQL Remote).
The action performed depends on the value of the op parameter:
DB_BACKUP_START
Must be called before a backup can start. Only one backup can be running at one time against any given database server. Database checkpoints are disabled until the backup is complete ( db_backup is called with an op value of DB_BACKUP_END). If the backup cannot start, the SQLCODE is SQLE_BACKUP_NOT_STARTED. Otherwise, the SQLCOUNT field of the sqlca is set to the size of each database page. (Backups are processed one page at a time.)
The file_num , page_num and sqlda parameters are ignored.
DB_BACKUP_OPEN_FILE
Open the database file specified by file_num , which allows pages of the specified file to be backed up using DB_BACKUP_READ_PAGE. Valid file numbers are 0 through DB_BACKUP_MAX_FILE for the main database files, DB_BACKUP_TRANS_LOG_FILE for the transaction log file, and DB_BACKUP_WRITE_FILE for the database write file if it exists. If the specified file does not exist, the SQLCODE is SQLE_NOTFOUND. Otherwise, SQLCOUNT contains the number of pages in the file, SQLIOESTIMATE contains a 32-bit value (POSIX time_t ) which identifies the time that the database file was created, and the operating system file name is in the sqlerrmc field of the SQLCA.
The page_num and sqlda parameters are ignored.
DB_BACKUP_READ_PAGE
Read one page of the database file specified by file_num . The page_num should be a value from 0 to one less than the number of pages returned in SQLCOUNT by a successful call to db_backup with the DB_BACKUP_OPEN_FILE operation. Otherwise, SQLCODE is set to SQLE_NOTFOUND. The sqlda descriptor should be set up with one variable of type DT_BINARY pointing to a buffer. The buffer should be large enough to hold binary data of the size returned in the SQLCOUNT field on the call to db_backup with the DB_BACKUP_START operation.
DT_BINARY data contains a two-byte length followed by the actual binary data, so the buffer must be two bytes longer than the page size.
Note:Application must save buffer
This call makes a copy of the specified database page into the buffer, but it is up to the application to save the buffer on some backup media.
DB_BACKUP_READ_RENAME_LOG
This action is the same as DB_BACKUP_READ_PAGE, except that after the last page of the transaction log has been returned, the database server renames the transaction log and starts a new one.
If the database server is unable to rename the log at the current time (there are incomplete transactions), you will get the SQLE_BACKUP_CANNOT_RENAME_LOG_YET error. In this case, don't use the page returned, but instead reissue the request until you receive SQLE_NOERROR and then write the page. Continue reading the pages until you receive the SQLE_NOTFOUND condition.
The SQLE_BACKUP_CANNOT_RENAME_LOG_YET error may be returned multiple times and on multiple pages. In your retry loop, you should add a delay so as not to slow the server down with too many requests.
When you receive the SQLE_NOTFOUND condition, the transaction log has been backed up successfully and the file has been renamed. The name for the old transaction file is returned in the sqlerrmc field of the SQLCA.
You should check the sqlda->sqlvar[0].sqlind value after a db_backup call. If this value is greater than zero, the last log page has been written and the log file has been renamed. The new name is still in sqlca.sqlerrmc , but the SQLCODE value is SQLE_NOERROR.
You should not call db_backup again after this. If you do, you get a second copy of your backed up log file and you receive SQLE_NOTFOUND.
DB_BACKUP_CLOSE_FILE
Must be called when processing of one file is complete to close the database file specified by file_num .
The page_num and sqlda parameters are ignored.
DB_BACKUP_END
Must be called at the end of the backup. No other backup can start until this backup has ended. Checkpoints are enabled again.
The file_num , page_num and sqlda parameters are ignored.
The dbbackup program uses the following algorithm. Note that this is not C code, and does not include error checking.
db_backup( ... DB_BACKUP_START ... )
allocate page buffer based on page size in SQLCODE
sqlda = alloc_sqlda( 1 )
sqlda->sqld = 1;
sqlda->sqlvar[0].sqltype = DT_BINARY
sqlda->sqlvar[0].sqldata = allocated buffer
for file_num = 0 to DB_BACKUP_MAX_FILE
db_backup( ... DB_BACKUP_OPEN_FILE, file_num ... )
if SQLCODE == SQLE_NO_ERROR
/* The file exists */
num_pages = SQLCOUNT
file_time = SQLE_IO_ESTIMATE
open backup file with name from sqlca.sqlerrmc
for page_num = 0 to num_pages - 1
db_backup( ... DB_BACKUP_READ_PAGE,
file_num, page_num, sqlda )
write page buffer out to backup file
next page_num
close backup file
db_backup( ... DB_BACKUP_CLOSE_FILE, file_num ... )
end if
next file_num
backup up file DB_BACKUP_WRITE_FILE as above
backup up file DB_BACKUP_TRANS_LOG_FILE as above
free page buffer
db_backup( ... DB_BACKUP_END ... )
void db_delete_file( struct sqlca * sqlca,
char * filename );
Must be connected to a user ID with DBA authority or REMOTE DBA authority (SQL Remote).
The db_delete_file function requests the database server to delete filename . This can be used after backing up and renaming the transaction log (see DB_BACKUP_READ_RENAME_LOG above) to delete the old transaction log. You must be connected to a user ID with DBA authority.
|
|