ASA Programming Guide
Embedded SQL Programming
Library function reference
void db_backup(
SQLCA * sqlca,
int op,
int file_num,
unsigned long page_num,
SQLDA * sqlda );
Must be connected to a user ID with DBA authority or REMOTE DBA authority (SQL Remote).
BACKUP statement is recommendedAlthough this function provides one way to add backup features to an application, the recommended way to accomplish this task is to use the BACKUP statement. For more information, see BACKUP statement. |
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 root 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.
Application must save bufferThis 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 (for example in version 7.x or earlier databases there may be incomplete transactions), the SQLE_BACKUP_CANNOT_RENAME_LOG_YET error is set. In this case, do not 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, except to close files and finish the backup. 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 ... )