Top Banner
ORACLE BACKUP AND RECOVERY
55
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

ORACLE BACKUP AND RECOVERY

Configuring the database environment for backup and recoveryPlanning and testing responses to different kinds of failures

Purpose of Backup and RecoverySetting up a backup schedule Troubleshooting backup problems

Monitoring the backup and recovery environment

Purpose of Backup and RecoveryRecovering from data loss if the need arises

Purpose of Backup and RecoveryData preservation, which involves creatinga database copy for long-term storage

Data transfer, which involves moving data from one database or one host to anotherData Protection

Logical backups

Physical backups

A backup is a copyData ProtectionMedia FailuresUser ErrorsApplication Errors

Data Protection

Data PreservationData preservation is related to data protection, but serves a different purpose. DATA

FromIn some situations you may need to take a backup of a database or database component and move it to another location.Data TransferTo

Recovery Manager (RMAN)User-manageRecovery SolutionsRecovery Manager Database Backups

Recovery Manager (RMAN) l tin ch c th qun l tt c cc hot ng sao lu v phc hi Oracle database.Gii thiu RMANRMAN executableServer processesChannelsTarget databaseRevover catalog database (optional)Media management layer (optional)Backups, backup sets, and backup piecesCu trc chnhL chng trnh qun l tt c ton t sao lu v phc hi.RMAN executableRMAN server processes l nhng x l nn, bt u trn server, s dng lin lc gia RMAN v database.Server processesKiu thit b vo/ra ang c hay ghi l a cng hay bng tS tin trnh ang ng thi truy cp vo mt thit b vo/raKch c ln nht ca file c to trn thit b vo/raTc ln nht v cc file c s d liu c cS lng ln nht cc file c m ti mt thi imChannelsL database m RMAN thc hin vic sao lu, phc hi v recovery.S hu cc datafiles, control files v archived redo files cn sao lu, phc hi hay recovered.Target databaseRMAN backup : bn sao lu ton b hay mt phn database, cha mt hoc nhiu backup setBackup set : mt nhm cc file sao lu cc mu sao lu (backup pieces) khi lnh sao lu RMAN c a ra, cha mt hoc nhiu backup pieces.Backup piece : file vt l nh phn to bi RMAN, c ghi nhn vo mi trng sao lu c th l a cng hay bng tBackups, backup sets, and backup piecesBc 1: Shutdown databaseSQL> connect /as sysdbaSQL> shutdown immediateBc 2: Start instance trong mode mount v exclusiveSQL> startup mount exclusive;SQL> ALTER SYSTEM SET log_archive_dest_1= location=E:\qldt\dbbk\archive\ SCOPE=spfile;SQL> ALTER SYSTEM SET log_archive_format=QLDT_arch_%s.arc SCOPE=spfile;SQL> ALTER SYSTEM SET log_archive_start=TRUE SCOPE=spfile;

Thit lp mode archivelogBc 3: Cho php mode archive logSQL> alter database archivelogBc 4: i database v mode openSQL> alter database open

Thit lp mode archivelogBc 1: To tablespace RMAN v user RMAN trong databaseng nhp vo Oracle database vi c quyn user sysSQL> connect sys/sys_password@tns_connection as sysdbaSQL> create tablespace RMANTAB datafile E:\Oracle\oradata\qldtdb\RMANTAB01.DBF size 200M;SQL> create user RMAN identified by rman_password default tablespace RMANTAB temporary tablespace TEMP quota unlimited on RMANTAB;SQL> grant dba, connect, resource, sysdba to RMAN;SQL> grant recovery_catalog_owner to RMAN;

To gin RMAN v catalogBc 2: ng k target database vi catalog database RMAN> connect catalog rman/rman_password@tns_catalog_connectionRMAN> connect target sys/sys_password@tns_target_connectionRMAN> create catalogRMAN> register database

To gin RMAN v catalogKt ni vo RMAN v chy nhng lnh sau thit lp cu hnh sao lu:Bc 1: Cu hnh t ng sao lu control file:RMAN> CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO E:\qldt\dbbk\datafile\CONTROL_%Y_%M_%D_%F.DBF;Bc 2: Cu hnh retention policyRMAN> CONFIGURE RETENTION POLICY TO REDUNDANCY 2;Bc 3: Cu hnh kch c ln nht ca backup set.RMAN> CONFIGURE MAXSETSIZE TO 2500M;

Thit lp cu hnh sao luChy cu lnh sau to full backup set:RMAN> BACKUP DATABASE FORMATE:\qldt\dbbk\datafile\QLDT_%Y%M%D_%U.DBF;Sao lu ton b archivelog file hin hnh:RMAN> BACKUP ARCHIVELOG ALL FORMAT E:\qldt\dbbk\archive\ QLDT _%Y%M%D_%U.ARC DELETE INPUT;

Full Backup DatabaseSao lu control file hin hnh:RMAN> BACKUP CURRENT CONTROLFILE;Kim tra backup set:RMAN> list backupset;

Full Backup DatabaseChy lnh sau lit k tt c cc backupset li thi:RMAN> REPORT OBSOLETE; xa backupset li thi dung lnh sau:RMAN> DELTE OBSOLETE;

Xa b backup li thiBc 1: Shutdown database RMAN> shutdown immediateBc 2: Start database trong mode nomountRMAN> startup nomountBc 3: Khi phc control file t backup set trong trng hp khng mun thay i v tr t control fileRMAN> restore controlfile from autobackup;Trong trng hp mun thay i v tr t control file, dung lnhRMAN> restore controlfile to /new_control_file_locationnew_control_file_location: ng dn ch n th mc cha cc control file mi c khi phc

Mt file controlBc 4: Hiu chnh file initSID.ora tham s control_files ch n v tr mi ca control file.Bc 5: i database v mode mountRMAN> sql alter database mount;Bc 7: Khi phc li databaseRMAN> recover database;Bc 8: i database v mode openRMAN> sql alter database open resetlogs;

Mt file controlBc 1: Start database nhn bit data file b hng.Bc 2: Shutdown database v start database li trong mode momountRMAN> shutdown immediate;RMAN> startup nomount;Bc 3: Phc hi data file b hng t backup setRMAN> restore datafile corrupted_data_file_name_path;corrupted_data_file_name_path: ng dn ch n file data file b hng.

Mt data fileBc 4: i database v mode mountRMAN> sql alter database mount;Bc 5: Khi phc tt c cc data file b hng v s dng tt c archivelog file.RMAN> recover database;Bc 6: i database v mode openRMAN> sql alter database open resetlogs;

Mt data fileBc 1: Start database nhn bit nhng data file b hng.Bc 2: Shutdown database v start database li trong mode nomount.RMAN> shutdown immediate;RMAN> startup nomount;Bc 3: Phc hi tt c data file b hng t backup set.RMAN> restore database;Bc 4: i database v mode mountRMAN> sql alter database mount;Bc 5: Khi phc tt c cc data file b hng v s dng tt c archivelog file.RMAN> recover database;Bc 6: i database v mode openRMAN> sql alter database open resetlogs;

Mt tt c data fileUser-Managed Database Backups

Ni dung :Truy vn V$ ly thng tin sao luSao lu tablespace v data fileSao lu control fileSao lu Archive Redo LogLu tr trn cc Raw DeviceTruy vn ly thng tin sao lu

- S dng cc th tc V$datafile v v$controlfile hin th cc datafile v controlfile ang c. V dSELECT NAME FROM V$DATAFILE;S dng th tc v$backup xem datafile c trn mt bn sao lu trc tuyn hin ti khng.V d sau truy vn tp tin d liu c trong 1 tablespace v ang trong ch sao lu (backup mode)

Kt qu :

Nu ct Status hin th tnh trng l NOT ACTIVE tc l file ang khng trong backup mode, ta cn phi chy cc cu lnh ALTER TABLESPACE BEGIN BACKUP hoc ALTER DATABASE BEGIN BACKUP ACTIVE n.

Xc nh data file ca tablespace bng truy vnDBA_DATA_FILES. Cu truy vn sau chn ra tablespace user :

Trong v d ny,/oracle/oradata/trgt/users01.dbfl ng dn ca file d liu users tablespace.Backup offline Datafile v Tablespace

t tablespace offline s dng quyn u tin normal nu c th, bi v n m bo rng bn c th cho tablespace online sau m khng phi khi phc li n : SQL> ALTER TABLESPACE users OFFLINE NORMAL;Back up data d liu offline % cp /oracle/oradata/trgt/users01.dbf /d2/users01_'date "+%m_%d_%y"'.dbfCho tablespace online : ALTER TABLESPACE users ONLINE;

Backup offline Datafile v TablespaceTruy vn ra tt c cc tablespace SELECT TABLESPACE_NAME, FILE_NAME FROM SYS.DBA_DATA_FILES WHERE TABLESPACE_NAME = 'USERS';

nh du cho vic bt u ca online tablespace backup (nu backup nhiu d liu cng lc th khng tn tablespace) SQL> ALTER TABLESPACE users BEGIN BACKUP;Sao lu tp tin d liu online ca tablespace online bng cu lnh ca H iu Hnh. Linux and UNIX :% cp /oracle/oradata/trgt/users01.dbf /d2/users01_'date "+%m_%d_%y"'.dbf% cp /oracle/oradata/trgt/users02.dbf /d2/users02_'date "+%m_%d_%y"'.dbfSau khi backing up tp tin d liu ca tablespace, chy lnh SQLALTERTABLESPACEviENDBACKUP :SQL> ALTER TABLESPACE users END BACKUP;Lu tr li cc bn ghi cha c lu tr :SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;

Backup online Datafile v Tablespace

Sao lu control file ca database thnh file nh phn(binary file): ta xc nh ng dn cho file output nh phn. V d sau sao lu control file vo /disk1/backup/cf.bak:ALTER DATABASE BACKUP CONTROLFILE TO '/disk1/backup/cf.bak';Sao lu control file thnh mt dng file theo du (trace file): 1) Mount hoc open the database. Startup Mount 2) thc thi cu lnh sau:ALTER DATABASE BACKUP CONTROLFILE TO TRACE;

Backing Up the Control FileCu trc quan trng nht cho cc hot ng phc hi l redo log, trong bao gm hai hay nhiu tp tin cp sn lu tr tt c cc thay i trn c s d liu khi chng xy ra. Mi th hin ca mt c s d liu Oracle c redo log lin quan n bo v c s d liu trong trng hp tht bi.Redo log files ghi li tt c cc thay i trong database buffer cache tr mt vi ngoi l ghi d liu trc tip.Back up archived redo logsS dng truy vnV$ARCHIVED_LOG xc nh redo log mun backup. V d :SELECT THREAD#,SEQUENCE#,NAME FROM V$ARCHIVED_LOG;Back up mt bn copy ca mi chui log s dng cc ng dng ca H iu Hnh . V d sau backs up tt c cc log vo a:% cp $ORACLE_HOME/oracle/trgt/arch/* /disk2/backup/archBack up archived redo logsMt raw device l mt a hoc phn vng m khng c tp tin h thng. Mt raw device c th cha ch mt tp tin duy nht.Nh Linux v Unix, Windows h tr phn vng a liu, trong c s d liu c th lu tr cc tp tin d liu, cc bn ghi trc tuyn, v cc tp tin kim sot. Mi phn vng nguyn liu c giao mt k t a hoc s a vt l v khng cha mt h thng tp tin. Nh trong Linux v UNIX, mi phn vng nguyn liu trn Windows c nh x vo mt tp tin duy nht.Trn Windows, tn ca raw data file c dng sau:\\.\drive_letter hoc \\.\PHYSICALDRIVEdrive_numberV d : \\.\G:

Backing Up to Raw Devices on WindowsCc th tc thc hin sao lu user-manage cc raw data file c bn ging nh cho vic sao chp cc tp tin trn mt h thng tp tin Windows, ngoi tr vic bn nn s dng tin ch Oracle OCOPY hn l copy.exe Windows cung cp hay cc tin ch ntbackup.exe ocopy from_file [to_file [a | size_1 [size_n]]]ocopy -b from_file to_driveocopy -r from_drive to_dir

V d :Data file12c mount trn\\.\G:raw partition.a C mount a h thng.database ang m.OCOPY "\\.G:" C:\backup\datafile12.bak

Backing Up to Raw Devices on Windows

Oracle FlashbackCng ngh Oracle Flashback b sung cho vic sao lu vt l v phc hi . iu ny thit lp cc tnh nng cung cp thm mt lp bo v d liu . C th, bn c th s dng cc tnh nng khc nhau ca Oracle Flashback xem trng thi qu kh ca d liu v tua li c s d liu ca bn m khng cn phi khi phc li bn sao lu hoc thc hin phc hi point-in-time(s phc hi y ca cc tp tin c s d liu) . Ni chung, tnh nng flashback hiu qu hn v t gy ri hn phc hi phng tin truyn thng trong hu ht cc tnh hung m h p dng .

Cng ngh flashbackHu ht cc tnh nng Flashback ca Oracle hot ng mc logic, cho php bn xem v thao tc cc i tng c s d liu . Cc tnh nng flashback logic cp ca Oracle khng ph thuc vo RMAN(Recovery Manager) v c sn. Ngoi tr Oracle Flashback Drop , cc tnh nng flashback logic da trn d liu hon tc , trong c cc bn ghi v tc ng ca mi ln cp nht c s d liu v cc gi tr ghi trong bn cp nht .

Logical Flashback FeaturesOracle Flashback QueryOracle Flashback Version QueryOracle Flashback Transaction Query (giao dch truy vn)Oracle Flashback Transaction(giao dch)Oracle Flashback TableOracle Flashback Drop

Tnh nngBn c th ch nh mt thi gian ch v chy truy vn i vi mt c s d liu , xem kt qu nh n c th xut hin vo thi im ch. phc hi t mt s thay i khng mong mun nh mt cp nht cho mt bng, bn c th chn mt thi gian ch trc khi bo li v chy mt truy vn ly ni dung ca cc hng b mt.

Oracle Flashback QueryBn c th xem tt c cc phin bn ca tt c cc dng tng tn ti trong mt hoc nhiu bng trong mt khong thi gian quy nh . ngoi ra, cng c th ly siu d liu v cc phin bn khc nhau ca cc hng , bao gm thi gian bt u v kt thc, cc thao tc, v ID m to ra cc phin bn . Bn c th s dng tnh nng ny phc hi d liu b mt gi tr v kim ton thay i cc bng truy vn.

Oracle Flashback Version QueryBn c th xem cc thay i c thc hin bi mt giao dch duy nht , hoc tt c cc giao dch trong mt khong thi gian c th.

Oracle Flashback Transaction QueryBn c th o ngc mt giao dch. Oracle Database xc nh s ph thuc gia cc giao dch v c hiu lc to ra mt giao dch b o ngc nhng thay i khng mong mun. C s d liu s tua li, v bt k giao dch no c th ph thuc vo n , cha bao gi xy ra.

Oracle Flashback TransactionBn c th phc hi mt bng hoc tp hp cc bng vi mt im quy nh trong thi gian trong qu kh m khng cn dng bt k phn no ca c s d liu offline(ngoi tuyn). Trong nhiu trng hp , Flashback Table gip loi b s cn thit phi thc hin cc hot ng phc hi im trong thi gian phc tp hn. Flashback Table khi phc bng trong khi t ng duy tr cc thuc tnh lin quan nh indexes, triggers, and constraints v theo cch ny cho php bn trnh c vic tm kim v khi phc li cc thuc tnh c s d liu c th.

Oracle Flashback TableBn c th o ngc tc dng ca mt cu lnh DROP TABLE.

Oracle Flashback DropA flashback data archive cho php bn s dng mt s tnh nng flashback logic truy cp d liu t xa tr li trong qu kh. Mt flashback data archive bao gm mt hoc nhiu bng hoc cc b phn ca bng. Khi bn to mt flashback data archive(on hi tng lu tr d liu), bn ch nh tn , thi gian lu gi , v bng. Bn cng c th ch nh mt on hi tng lu tr d liu mc nh. C s d liu t ng thanh trng d liu lch s c mt ngy sau khi ht hn thi gian lu gi .Bn c th bt v tt flashback archive cho cc bng c nhn. Theo mc nh, lu tr hi tng tt cho mi bng .

Flashback data archive mc vt l , c s d liu Oracle Flashback cung cp bo v d liu thay th hiu qu hn database point-in-time recovery ( DBPITR ) . Nu cc tp tin d liu hin ti c nhng thay i khng mong mun , sau bn c th s dng RMAN lnh FLASHBACK DATABASE phc hi cc tp tin d liu ni dung ca h ti mt thi im trong qu kh . Sn phm cui cng l ging nh kt qu ca mt DBPITR , nhng ni chung l nhanh hn nhiu bi v n khng yu cu khi phc li cc tp tin d liu t bn sao lu v yu cu lm li t hn so vi media recovery .

Flashback DatabaseFlashback Database s dngflashback logs truy cp vo phin bn trc y ca cc khi d liu v mt s thng tin t cc bn ghi lu tr. Flashback Database yu cu bn cu hnh mt khu vc phc hi nhanh chng cho mt c s d liu v cc flashback logs (bn ghi hi tng) ch c th c lu tr . Flashback logging khng c kch hot theo mc nh. Khng gian s dng cho cc flashback logs c qun l t ng bi cc c s d liu v cn i vi khng gian cn thit cho cc tp tin khc trong khu vc phc hi nhanh chng .

Flashback DatabaseOracle Database cng h tr cc im khi phc cng vi Flashback Database sao lu v phc hi. Mt im khi phc l mt b danh tng ng vi mt s thay i h thng (system change number - SCN) . Bn c th to mt im khi phc bt c lc no nu bn d on cn phi tr li mt phn hoc ton b c s d liu ni dung ca n ti thi im . Aguaranteed restore point m bo rng bn c th s dng Flashback Database tr v mt c s d liu thi im cc im khi phc.

Flashback Database